From 82112b5de2ba54b08f1ac6f5016a3423e95f2699 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 9 Jun 2014 00:54:12 +0000 Subject: [PATCH] Fix changing wxWindow ID in wxMSW. This must be done both at wxWidgets and MSW level, otherwise changing the ID results in window not recognizing itself as the recipient of the messages sent to it by Windows. Closes #3697. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76672 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/window.h | 2 ++ include/wx/window.h | 2 +- src/msw/window.cpp | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/wx/msw/window.h b/include/wx/msw/window.h index 6146e7e3f4..1ece90cae0 100644 --- a/include/wx/msw/window.h +++ b/include/wx/msw/window.h @@ -115,6 +115,8 @@ public: wxCoord width, wxCoord widthTotal) const; + virtual void SetId(wxWindowID winid); + #if wxUSE_DRAG_AND_DROP virtual void SetDropTarget( wxDropTarget *dropTarget ); #endif // wxUSE_DRAG_AND_DROP diff --git a/include/wx/window.h b/include/wx/window.h index 302924751e..4ceadc2a25 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -236,7 +236,7 @@ public: // window id uniquely identifies the window among its siblings unless // it is wxID_ANY which means "don't care" - void SetId( wxWindowID winid ) { m_windowId = winid; } + virtual void SetId( wxWindowID winid ) { m_windowId = winid; } wxWindowID GetId() const { return m_windowId; } // generate a unique id (or count of them consecutively), returns a diff --git a/src/msw/window.cpp b/src/msw/window.cpp index d186906e6d..72c8d1d093 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -552,6 +552,20 @@ bool wxWindowMSW::Create(wxWindow *parent, return true; } +void wxWindowMSW::SetId(wxWindowID winid) +{ + wxWindowBase::SetId(winid); + + // Also update the ID used at the Windows level to avoid nasty surprises + // when we can't find the control when handling messages for it after + // changing its ID because Windows still uses the old one. + if ( GetHwnd() ) + { + if ( !::SetWindowLong(GetHwnd(), GWL_ID, winid) ) + wxLogLastError(wxT("SetWindowLong(GWL_ID)")); + } +} + // --------------------------------------------------------------------------- // basic operations // ---------------------------------------------------------------------------