From 594afa2f550c528be27d0ab9107a9bae73bd6649 Mon Sep 17 00:00:00 2001 From: Martin Fuchs Date: Sat, 23 Aug 2003 09:27:26 +0000 Subject: [PATCH] fixed GDI handle leaks svn path=/trunk/; revision=5790 --- .../subsys/system/explorer/desktop/desktop.cpp | 5 ++--- .../subsys/system/explorer/taskbar/desktopbar.cpp | 7 ++++++- .../subsys/system/explorer/taskbar/desktopbar.h | 1 + .../subsys/system/explorer/taskbar/traynotify.cpp | 6 +++--- reactos/subsys/system/explorer/utility/utility.h | 15 +++++++++------ reactos/subsys/system/explorer/utility/window.cpp | 11 ++++------- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/reactos/subsys/system/explorer/desktop/desktop.cpp b/reactos/subsys/system/explorer/desktop/desktop.cpp index 997b1114c79..41ea473b8ff 100644 --- a/reactos/subsys/system/explorer/desktop/desktop.cpp +++ b/reactos/subsys/system/explorer/desktop/desktop.cpp @@ -197,9 +197,8 @@ LRESULT DesktopWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam) { switch(nmsg) { case WM_PAINT: { - PAINTSTRUCT ps; - draw_desktop_background(_hwnd, BeginPaint(_hwnd, &ps)); - EndPaint(_hwnd, &ps); + PaintCanvas canvas(_hwnd); + draw_desktop_background(_hwnd, canvas); break;} case WM_LBUTTONDBLCLK: diff --git a/reactos/subsys/system/explorer/taskbar/desktopbar.cpp b/reactos/subsys/system/explorer/taskbar/desktopbar.cpp index ac76714513d..c56e7fcffbf 100644 --- a/reactos/subsys/system/explorer/taskbar/desktopbar.cpp +++ b/reactos/subsys/system/explorer/taskbar/desktopbar.cpp @@ -64,10 +64,15 @@ DesktopBar::DesktopBar(HWND hwnd) : super(hwnd), WM_TASKBARCREATED(RegisterWindowMessage(WINMSG_TASKBARCREATED)) { + SystemParametersInfo(SPI_GETWORKAREA, 0, &_work_area_org, 0); } DesktopBar::~DesktopBar() { + // restore work area to the previous size + SystemParametersInfo(SPI_SETWORKAREA, 0, &_work_area_org, 0); + PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETWORKAREA, 0); + // exit application after destroying desktop window PostQuitMessage(0); } @@ -166,7 +171,7 @@ LRESULT DesktopBar::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam) WindowRect rect(_hwnd); RECT work_area = {0, 0, GetSystemMetrics(SM_CXSCREEN), rect.top}; - SystemParametersInfo(SPI_SETWORKAREA, 0, &work_area, 0); + SystemParametersInfo(SPI_SETWORKAREA, 0, &work_area, 0); // don't use SPIF_SENDCHANGE because then we have to wait for any message being delivered PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETWORKAREA, 0); break;} diff --git a/reactos/subsys/system/explorer/taskbar/desktopbar.h b/reactos/subsys/system/explorer/taskbar/desktopbar.h index de0450becbc..e617fc34d3d 100644 --- a/reactos/subsys/system/explorer/taskbar/desktopbar.h +++ b/reactos/subsys/system/explorer/taskbar/desktopbar.h @@ -65,6 +65,7 @@ struct DesktopBar : public OwnerDrawParent protected: int WM_TASKBARCREATED; + RECT _work_area_org; LRESULT Init(LPCREATESTRUCT pcs); LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam); diff --git a/reactos/subsys/system/explorer/taskbar/traynotify.cpp b/reactos/subsys/system/explorer/taskbar/traynotify.cpp index a408a9e35bc..4bdcde23d66 100644 --- a/reactos/subsys/system/explorer/taskbar/traynotify.cpp +++ b/reactos/subsys/system/explorer/taskbar/traynotify.cpp @@ -220,7 +220,7 @@ void NotifyArea::Refresh() void NotifyArea::Paint() { - BufferedPaintCanvas canvas(_hwnd); + PaintCanvas canvas(_hwnd); // first fill with the background color FillRect(canvas, &canvas.rcPaint, GetSysColorBrush(COLOR_BTNFACE)); @@ -324,7 +324,7 @@ bool ClockWindow::FormatTime() if (_tcscmp(buffer, _time)) { _tcscpy(_time, buffer); - return true; + return true; // The text to display has changed. } return false; // no change @@ -335,7 +335,7 @@ void ClockWindow::Paint() PaintCanvas canvas(_hwnd); BkMode bkmode(canvas, TRANSPARENT); - SelectedFont font(canvas, GetStockFont(ANSI_VAR_FONT)); + FontSelection font(canvas, GetStockFont(ANSI_VAR_FONT)); DrawText(canvas, _time, -1, ClientRect(_hwnd), DT_SINGLELINE|DT_VCENTER|DT_NOPREFIX); } diff --git a/reactos/subsys/system/explorer/utility/utility.h b/reactos/subsys/system/explorer/utility/utility.h index 14686993c03..c19c9259169 100644 --- a/reactos/subsys/system/explorer/utility/utility.h +++ b/reactos/subsys/system/explorer/utility/utility.h @@ -175,7 +175,7 @@ struct PaintCanvas : public PAINTSTRUCT BeginPaint(hwnd, this); } - PaintCanvas() + ~PaintCanvas() { EndPaint(_hwnd, this); } @@ -212,7 +212,7 @@ struct SelectedBitmap SelectedBitmap(HDC hdc, HBITMAP hbmp) : _hdc(hdc), _old_hbmp(SelectBitmap(hdc, hbmp)) {} - ~SelectedBitmap() {SelectBitmap(_hdc, _old_hbmp);} + ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc, _old_hbmp));} protected: HDC _hdc; @@ -253,7 +253,10 @@ struct BufferedCanvas : public BufferCanvas struct BufferedPaintCanvas : public PaintCanvas, public BufferedCanvas { BufferedPaintCanvas(HWND hwnd) - : PaintCanvas(hwnd), BufferedCanvas(hdc, 0, 0, rcPaint.right, rcPaint.bottom) {} + : PaintCanvas(hwnd), + BufferedCanvas(PAINTSTRUCT::hdc, 0, 0, rcPaint.right, rcPaint.bottom) + { + } operator HDC() {return BufferedCanvas::_hdc;} }; @@ -283,12 +286,12 @@ protected: COLORREF _old_bkmode; }; -struct SelectedFont +struct FontSelection { - SelectedFont(HDC hdc, HFONT hFont) + FontSelection(HDC hdc, HFONT hFont) : _hdc(hdc), _old_hFont(SelectFont(hdc, hFont)) {} - ~SelectedFont() {SelectFont(_hdc, _old_hFont);} + ~FontSelection() {SelectFont(_hdc, _old_hFont);} protected: HDC _hdc; diff --git a/reactos/subsys/system/explorer/utility/window.cpp b/reactos/subsys/system/explorer/utility/window.cpp index 8c456c13973..733223110ee 100644 --- a/reactos/subsys/system/explorer/utility/window.cpp +++ b/reactos/subsys/system/explorer/utility/window.cpp @@ -264,16 +264,13 @@ LRESULT ChildWindow::WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam) { switch(nmsg) { case WM_PAINT: { - PAINTSTRUCT ps; - HBRUSH lastBrush; + PaintCanvas canvas(_hwnd); ClientRect rt(_hwnd); - BeginPaint(_hwnd, &ps); rt.left = _split_pos-SPLIT_WIDTH/2; rt.right = _split_pos+SPLIT_WIDTH/2+1; - lastBrush = SelectBrush(ps.hdc, GetStockBrush(COLOR_SPLITBAR)); - Rectangle(ps.hdc, rt.left, rt.top-1, rt.right, rt.bottom+1); - SelectObject(ps.hdc, lastBrush); - EndPaint(_hwnd, &ps); + HBRUSH lastBrush = SelectBrush(canvas, GetStockBrush(COLOR_SPLITBAR)); + Rectangle(canvas, rt.left, rt.top-1, rt.right, rt.bottom+1); + SelectObject(canvas, lastBrush); break;} case WM_SETCURSOR: