From 7561aacd5e3bcd5f98f4cdadcec5e94d1550b369 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 21 May 2001 23:10:31 +0000 Subject: [PATCH] fix for releasing the HDC in WM_DRAWITEM handler git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@10261 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/dc.h | 16 ++++++++++++++-- src/msw/listbox.cpp | 7 +++---- src/msw/window.cpp | 23 ++++++++++++----------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/include/wx/msw/dc.h b/include/wx/msw/dc.h index 257f86cd50..4d53cb7205 100644 --- a/include/wx/msw/dc.h +++ b/include/wx/msw/dc.h @@ -73,8 +73,6 @@ class WXDLLEXPORT wxDC : public wxDCBase { - DECLARE_DYNAMIC_CLASS(wxDC) - public: wxDC(); ~wxDC(); @@ -224,6 +222,20 @@ protected: WXHBRUSH m_oldBrush; WXHFONT m_oldFont; WXHPALETTE m_oldPalette; + + DECLARE_DYNAMIC_CLASS(wxDC) +}; + +// ---------------------------------------------------------------------------- +// wxDCTemp: a wxDC which doesn't free the given HDC (used by wxWindows +// only/mainly) +// ---------------------------------------------------------------------------- + +class WXDLLEXPORT wxDCTemp : public wxDC +{ +public: + wxDCTemp(WXHDC hdc) { SetHDC(hdc); } + virtual ~wxDCTemp() { SetHDC((WXHDC)NULL); } }; #endif diff --git a/src/msw/listbox.cpp b/src/msw/listbox.cpp index 462c26bb82..147e73c917 100644 --- a/src/msw/listbox.cpp +++ b/src/msw/listbox.cpp @@ -773,14 +773,13 @@ bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) wxListBoxItem *pItem = (wxListBoxItem *)data; - wxDC dc; - dc.SetHDC((WXHDC)pStruct->hDC, FALSE); + wxDCTemp dc((WXHDC)pStruct->hDC); wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top), wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom)); return pItem->OnDrawItem(dc, rect, - (wxOwnerDrawn::wxODAction)pStruct->itemAction, - (wxOwnerDrawn::wxODStatus)pStruct->itemState); + (wxOwnerDrawn::wxODAction)pStruct->itemAction, + (wxOwnerDrawn::wxODStatus)pStruct->itemState); } #endif diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 11a6da140e..184d7da24b 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -2923,19 +2923,20 @@ bool wxWindow::MSWOnDrawItem(int id, WXDRAWITEMSTRUCT *itemStruct) wxCHECK( pMenuItem->IsKindOf(CLASSINFO(wxMenuItem)), FALSE ); - // prepare to call OnDrawItem() - wxDC dc; - dc.SetHDC((WXHDC)pDrawStruct->hDC, FALSE); + // prepare to call OnDrawItem(): notice using of wxDCTemp to prevent + // the DC from being released + wxDCTemp dc((WXHDC)pDrawStruct->hDC); wxRect rect(pDrawStruct->rcItem.left, pDrawStruct->rcItem.top, pDrawStruct->rcItem.right - pDrawStruct->rcItem.left, pDrawStruct->rcItem.bottom - pDrawStruct->rcItem.top); return pMenuItem->OnDrawItem - ( - dc, rect, - (wxOwnerDrawn::wxODAction)pDrawStruct->itemAction, - (wxOwnerDrawn::wxODStatus)pDrawStruct->itemState - ); + ( + dc, + rect, + (wxOwnerDrawn::wxODAction)pDrawStruct->itemAction, + (wxOwnerDrawn::wxODStatus)pDrawStruct->itemState + ); } wxWindow *item = FindItem(id); @@ -3103,9 +3104,8 @@ bool wxWindow::HandleEraseBkgnd(WXHDC hdc) if ( ::IsIconic(GetHwnd()) ) return TRUE; - wxDC dc; + wxDCTemp dc(hdc); - dc.SetHDC(hdc); dc.SetWindow(this); dc.BeginDrawing(); @@ -3114,8 +3114,9 @@ bool wxWindow::HandleEraseBkgnd(WXHDC hdc) bool rc = GetEventHandler()->ProcessEvent(event); dc.EndDrawing(); + + // must be called manually as ~wxDC doesn't do anything for wxDCTemp dc.SelectOldObjects(hdc); - dc.SetHDC((WXHDC) NULL); return rc; } -- 2.45.2