+
+// ----------------------------------------------------------------------------
+// wxDisplayFactoryMSW implementation
+// ----------------------------------------------------------------------------
+
+LRESULT APIENTRY
+wxDisplayWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+ if ( msg == WM_SETTINGCHANGE )
+ {
+ wxDisplayFactoryMSW::RefreshMonitors();
+
+ return 0;
+ }
+
+ return ::DefWindowProc(hwnd, msg, wParam, lParam);
+}
+
+wxDisplayFactoryMSW::wxDisplayFactoryMSW()
+{
+ // This is not supposed to happen with the current code, the factory is
+ // implicitly a singleton.
+ wxASSERT_MSG( !ms_factory, wxS("Using more than one factory?") );
+
+ ms_factory = this;
+
+ m_hiddenHwnd = NULL;
+ m_hiddenClass = NULL;
+
+ if ( gs_MonitorFromPoint==NULL || gs_MonitorFromWindow==NULL
+ || gs_GetMonitorInfo==NULL || gs_EnumDisplayMonitors==NULL )
+ {
+ // First initialization, or last initialization failed.
+ wxDynamicLibrary dllDisplay(displayDllName, wxDL_VERBATIM | wxDL_QUIET);
+
+ wxDL_INIT_FUNC(gs_, MonitorFromPoint, dllDisplay);
+ wxDL_INIT_FUNC(gs_, MonitorFromWindow, dllDisplay);
+ wxDL_INIT_FUNC_AW(gs_, GetMonitorInfo, dllDisplay);
+ wxDL_INIT_FUNC(gs_, EnumDisplayMonitors, dllDisplay);
+
+ // we can safely let dllDisplay go out of scope, the DLL itself will
+ // still remain loaded as all programs link to it statically anyhow
+ }
+
+ if ( gs_MonitorFromPoint==NULL || gs_MonitorFromWindow==NULL
+ || gs_GetMonitorInfo==NULL || gs_EnumDisplayMonitors==NULL )
+ return;
+
+ DoRefreshMonitors();
+
+ // Also create a hidden window to listen for WM_SETTINGCHANGE that we
+ // receive when a monitor is added to or removed from the system as we must
+ // refresh our monitor handles information then.
+ m_hiddenHwnd = wxCreateHiddenWindow
+ (
+ &m_hiddenClass,
+ wxT("wxDisplayHiddenWindow"),
+ wxDisplayWndProc
+ );
+}
+
+wxDisplayFactoryMSW::~wxDisplayFactoryMSW()
+{
+ if ( m_hiddenHwnd )
+ {
+ if ( !::DestroyWindow(m_hiddenHwnd) )
+ {
+ wxLogLastError(wxT("DestroyWindow(wxDisplayHiddenWindow)"));
+ }
+
+ if ( m_hiddenClass )
+ {
+ if ( !::UnregisterClass(m_hiddenClass, wxGetInstance()) )
+ {
+ wxLogLastError(wxT("UnregisterClass(wxDisplayHiddenWindow)"));
+ }
+ }
+ }
+
+ ms_factory = NULL;
+}
+
+void wxDisplayFactoryMSW::DoRefreshMonitors()
+{
+ m_displays.Clear();
+
+ if ( !gs_EnumDisplayMonitors(NULL, NULL, MultimonEnumProc, (LPARAM)this) )
+ {
+ wxLogLastError(wxT("EnumDisplayMonitors"));
+ }
+}
+
+/* static */
+BOOL CALLBACK
+wxDisplayFactoryMSW::MultimonEnumProc(
+ HMONITOR hMonitor, // handle to display monitor
+ HDC WXUNUSED(hdcMonitor), // handle to monitor-appropriate device context
+ LPRECT WXUNUSED(lprcMonitor), // pointer to monitor intersection rectangle
+ LPARAM dwData) // data passed from EnumDisplayMonitors (this)
+{
+ wxDisplayFactoryMSW *const self = (wxDisplayFactoryMSW *)dwData;
+
+ self->m_displays.Add(hMonitor);
+
+ // continue the enumeration
+ return TRUE;
+}
+
+wxDisplayImpl *wxDisplayFactoryMSW::CreateDisplay(unsigned n)
+{
+ wxCHECK_MSG( n < m_displays.size(), NULL, wxT("An invalid index was passed to wxDisplay") );
+
+ return new wxDisplayMSW(n, m_displays[n]);
+}
+
+// helper for GetFromPoint() and GetFromWindow()
+int wxDisplayFactoryMSW::FindDisplayFromHMONITOR(HMONITOR hmon) const
+{
+ if ( hmon )
+ {
+ const size_t count = m_displays.size();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ if ( hmon == m_displays[n] )
+ return n;
+ }
+ }
+
+ return wxNOT_FOUND;
+}
+
+int wxDisplayFactoryMSW::GetFromPoint(const wxPoint& pt)
+{
+ POINT pt2;
+ pt2.x = pt.x;
+ pt2.y = pt.y;
+
+ return FindDisplayFromHMONITOR(gs_MonitorFromPoint(pt2,
+ MONITOR_DEFAULTTONULL));
+}
+
+int wxDisplayFactoryMSW::GetFromWindow(const wxWindow *window)