diff --git a/docs/changes.txt b/docs/changes.txt index a774275fee..fa8a77daa8 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -517,6 +517,7 @@ MSW: - Worked around child window and caret positioning bug (in Windows) when using wxBORDER_THEME in a container window. - Suppressed spurious character event for decimal key in numeric keypad. +- Allow to not create wxPaintDC in EVT_PAINT handler. i18n: diff --git a/interface/wx/event.h b/interface/wx/event.h index dc010cc28d..07f56d82c8 100644 --- a/interface/wx/event.h +++ b/interface/wx/event.h @@ -1547,16 +1547,8 @@ public: A paint event is sent when a window's contents needs to be repainted. - Please notice that in general it is impossible to change the drawing of a - standard control (such as wxButton) and so you shouldn't attempt to handle - paint events for them as even if it might work on some platforms, this is - inherently not portable and won't work everywhere. - - @remarks - Note that in a paint event handler, the application must always create a - wxPaintDC object, even if you do not use it. Otherwise, under MS Windows, - refreshing for this and other windows will go wrong. - For example: + The handler of this event must create a wxPaintDC object and use it for + painting the window contents. For example: @code void MyWindow::OnPaint(wxPaintEvent& event) { @@ -1565,6 +1557,12 @@ public: DrawMyDocument(dc); } @endcode + + Notice that you must @e not create other kinds of wxDC (e.g. wxClientDC or + wxWindowDC) in EVT_PAINT handlers and also don't create wxPaintDC outside + of this event handlers. + + You can optimize painting by retrieving the rectangles that have been damaged and only repainting these. The rectangles are in terms of the client area, and are unscrolled, so you will need to do some calculations using the current @@ -1601,6 +1599,12 @@ public: } @endcode + @remarks + Please notice that in general it is impossible to change the drawing of a + standard control (such as wxButton) and so you shouldn't attempt to handle + paint events for them as even if it might work on some platforms, this is + inherently not portable and won't work everywhere. + @beginEventTable{wxPaintEvent} @event{EVT_PAINT(func)} diff --git a/src/msw/dcclient.cpp b/src/msw/dcclient.cpp index b64aee8c82..bad81c1e72 100644 --- a/src/msw/dcclient.cpp +++ b/src/msw/dcclient.cpp @@ -236,6 +236,11 @@ wxPaintDCImpl::wxPaintDCImpl( wxDC *owner, wxWindow *window ) : } else // not in cache, create a new one { + // see comments in src/msw/window.cpp where this is defined + extern bool wxDidCreatePaintDC; + + wxDidCreatePaintDC = true; + m_hDC = (WXHDC)::BeginPaint(GetHwndOf(m_window), &g_paintStruct); if (m_hDC) ms_cache.Add(new wxPaintDCInfo(m_window, this)); diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 0ebb93aa59..c3c711cb98 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -4796,6 +4796,14 @@ wxColour wxWindowMSW::MSWGetThemeColour(const wchar_t *themeName, // painting // --------------------------------------------------------------------------- +// this variable is used to check that a paint event handler which processed +// the event did create a wxPaintDC inside its code and called BeginPaint() to +// validate the invalidated window area as otherwise we'd keep getting an +// endless stream of WM_PAINT messages for this window resulting in a lot of +// difficult to debug problems (e.g. impossibility to repaint other windows, +// lack of timer and idle events and so on) +extern bool wxDidCreatePaintDC = false; + bool wxWindowMSW::HandlePaint() { HRGN hRegion = ::CreateRectRgn(0, 0, 0, 0); // Dummy call to get a handle @@ -4810,11 +4818,20 @@ bool wxWindowMSW::HandlePaint() m_updateRegion = wxRegion((WXHRGN) hRegion); + wxDidCreatePaintDC = false; + wxPaintEvent event(m_windowId); event.SetEventObject(this); bool processed = HandleWindowEvent(event); + if ( processed && !wxDidCreatePaintDC ) + { + // do call MSWDefWindowProc() to validate the update region to avoid + // the problems mentioned above + processed = false; + } + // note that we must generate NC event after the normal one as otherwise // BeginPaint() will happily overwrite our decorations with the background // colour