fixed GDI handle leaks
authorMartin Fuchs <fuchs.martin@gmail.com>
Sat, 23 Aug 2003 09:27:26 +0000 (09:27 +0000)
committerMartin Fuchs <fuchs.martin@gmail.com>
Sat, 23 Aug 2003 09:27:26 +0000 (09:27 +0000)
svn path=/trunk/; revision=5790

reactos/subsys/system/explorer/desktop/desktop.cpp
reactos/subsys/system/explorer/taskbar/desktopbar.cpp
reactos/subsys/system/explorer/taskbar/desktopbar.h
reactos/subsys/system/explorer/taskbar/traynotify.cpp
reactos/subsys/system/explorer/utility/utility.h
reactos/subsys/system/explorer/utility/window.cpp

index 997b111..41ea473 100644 (file)
@@ -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:
index ac76714..c56e7fc 100644 (file)
@@ -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;}
 
index de0450b..e617fc3 100644 (file)
@@ -65,6 +65,7 @@ struct DesktopBar : public OwnerDrawParent<Window>
 
 protected:
        int             WM_TASKBARCREATED;
+       RECT    _work_area_org;
 
        LRESULT Init(LPCREATESTRUCT pcs);
        LRESULT WndProc(UINT nmsg, WPARAM wparam, LPARAM lparam);
index a408a9e..4bdcde2 100644 (file)
@@ -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);
 }
index 1468699..c19c925 100644 (file)
@@ -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;
index 8c456c1..7332231 100644 (file)
@@ -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: