diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 982714f7bc..a1cd37ed21 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -471,6 +471,13 @@ private: wxDECLARE_NO_COPY_CLASS(MemoryHDC); }; +// Helper function returning the resolution of the given HDC. +inline wxSize wxGetDPIofHDC(HDC hdc) +{ + return wxSize(::GetDeviceCaps(hdc, LOGPIXELSX), + ::GetDeviceCaps(hdc, LOGPIXELSY)); +} + // a class which selects a GDI object into a DC in its ctor and deselects in // dtor class SelectInHDC diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index 3896ad8b5a..35ce2d0472 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -2648,8 +2648,7 @@ wxSize wxMSWDCImpl::GetPPI() const if ( !ppi.x || !ppi.y ) { - ppi.x = ::GetDeviceCaps(GetHdc(), LOGPIXELSX); - ppi.y = ::GetDeviceCaps(GetHdc(), LOGPIXELSY); + ppi = wxGetDPIofHDC(GetHdc()); } return ppi; diff --git a/src/msw/graphics.cpp b/src/msw/graphics.cpp index cf8fcd488f..21782259c7 100644 --- a/src/msw/graphics.cpp +++ b/src/msw/graphics.cpp @@ -2545,11 +2545,11 @@ wxGDIPlusPrintingContext::wxGDIPlusPrintingContext( wxGraphicsRenderer* renderer void wxGDIPlusPrintingContext::GetDPI(wxDouble* dpiX, wxDouble* dpiY) const { // override to use same scaling as wxWindowsPrintPreview::DetermineScaling - ScreenHDC hdc; + const wxSize dpi = wxGetDPIofHDC(ScreenHDC()); if ( dpiX ) - *dpiX = ::GetDeviceCaps(hdc, LOGPIXELSX); + *dpiX = dpi.x; if ( dpiY ) - *dpiY = ::GetDeviceCaps(hdc, LOGPIXELSY); + *dpiY = dpi.y; } //----------------------------------------------------------------------------- diff --git a/src/msw/graphicsd2d.cpp b/src/msw/graphicsd2d.cpp index d1468f7005..49acd404c1 100644 --- a/src/msw/graphicsd2d.cpp +++ b/src/msw/graphicsd2d.cpp @@ -5316,13 +5316,11 @@ wxGraphicsFont wxD2DRenderer::CreateFont( { // Use the same DPI as wxFont will use in SetPixelSize, so these cancel // each other out and we are left with the actual pixel size. - ScreenHDC hdc; - wxRealPoint dpi(::GetDeviceCaps(hdc, LOGPIXELSX), - ::GetDeviceCaps(hdc, LOGPIXELSY)); + const wxSize dpi = wxGetDPIofHDC(ScreenHDC()); return CreateFontAtDPI( wxFontInfo(wxSize(sizeInPixels, sizeInPixels)).AllFlags(flags).FaceName(facename), - dpi, col); + wxRealPoint(dpi.x, dpi.y), col); } wxGraphicsFont wxD2DRenderer::CreateFontAtDPI(const wxFont& font, diff --git a/src/msw/ole/activex.cpp b/src/msw/ole/activex.cpp index 0543b32201..ef6fda4f3a 100644 --- a/src/msw/ole/activex.cpp +++ b/src/msw/ole/activex.cpp @@ -157,23 +157,13 @@ wxDEFINE_EVENT( wxEVT_ACTIVEX, wxActiveXEvent ); static void PixelsToHimetric(SIZEL &sz) { - static int logX = 0; - static int logY = 0; - - if (logY == 0) - { - // initaliase - HDC dc = GetDC(NULL); - logX = GetDeviceCaps(dc, LOGPIXELSX); - logY = GetDeviceCaps(dc, LOGPIXELSY); - ReleaseDC(NULL, dc); - } + static const wxSize logSz = wxGetDPIofHDC(ScreenHDC()); #define HIMETRIC_INCH 2540 #define CONVERT(x, logpixels) wxMulDivInt32(HIMETRIC_INCH, (x), (logpixels)) - sz.cx = CONVERT(sz.cx, logX); - sz.cy = CONVERT(sz.cy, logY); + sz.cx = CONVERT(sz.cx, logSz.x); + sz.cy = CONVERT(sz.cy, logSz.y); #undef CONVERT #undef HIMETRIC_INCH diff --git a/src/msw/printwin.cpp b/src/msw/printwin.cpp index a43e8bc8cd..051b1f93a1 100644 --- a/src/msw/printwin.cpp +++ b/src/msw/printwin.cpp @@ -310,10 +310,8 @@ bool wxWindowsPrintPreview::Print(bool interactive) void wxWindowsPrintPreview::DetermineScaling() { - ScreenHDC dc; - int logPPIScreenX = ::GetDeviceCaps(dc, LOGPIXELSX); - int logPPIScreenY = ::GetDeviceCaps(dc, LOGPIXELSY); - m_previewPrintout->SetPPIScreen(logPPIScreenX, logPPIScreenY); + const wxSize logPPIScreen = wxGetDPIofHDC(ScreenHDC()); + m_previewPrintout->SetPPIScreen(logPPIScreen); // Get a device context for the currently selected printer wxPrinterDC printerDC(m_printDialogData.GetPrintData()); @@ -322,8 +320,7 @@ void wxWindowsPrintPreview::DetermineScaling() int printerHeightMM; int printerXRes; int printerYRes; - int logPPIPrinterX; - int logPPIPrinterY; + wxSize logPPIPrinter; wxRect paperRect; @@ -335,13 +332,12 @@ void wxWindowsPrintPreview::DetermineScaling() printerHeightMM = ::GetDeviceCaps(hdc, VERTSIZE); printerXRes = ::GetDeviceCaps(hdc, HORZRES); printerYRes = ::GetDeviceCaps(hdc, VERTRES); - logPPIPrinterX = ::GetDeviceCaps(hdc, LOGPIXELSX); - logPPIPrinterY = ::GetDeviceCaps(hdc, LOGPIXELSY); + logPPIPrinter = wxGetDPIofHDC(hdc); paperRect = printerDC.GetPaperRect(); - if ( logPPIPrinterX == 0 || - logPPIPrinterY == 0 || + if ( logPPIPrinter.x == 0 || + logPPIPrinter.y == 0 || printerWidthMM == 0 || printerHeightMM == 0 ) { @@ -355,8 +351,7 @@ void wxWindowsPrintPreview::DetermineScaling() printerHeightMM = 250; printerXRes = 1500; printerYRes = 2500; - logPPIPrinterX = 600; - logPPIPrinterY = 600; + logPPIPrinter = wxSize(600, 600); paperRect = wxRect(0, 0, printerXRes, printerYRes); m_isOk = false; @@ -366,11 +361,11 @@ void wxWindowsPrintPreview::DetermineScaling() m_previewPrintout->SetPageSizePixels(printerXRes, printerYRes); m_previewPrintout->SetPageSizeMM(printerWidthMM, printerHeightMM); m_previewPrintout->SetPaperRectPixels(paperRect); - m_previewPrintout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY); + m_previewPrintout->SetPPIPrinter(logPPIPrinter); // At 100%, the page should look about page-size on the screen. - m_previewScaleX = float(logPPIScreenX) / logPPIPrinterX; - m_previewScaleY = float(logPPIScreenY) / logPPIPrinterY; + m_previewScaleX = float(logPPIScreen.x) / logPPIPrinter.x; + m_previewScaleY = float(logPPIScreen.y) / logPPIPrinter.y; } #if wxUSE_ENH_METAFILE diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 4e1ec7d527..df90f28422 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -4878,9 +4878,7 @@ wxSize wxWindowMSW::GetDPI() const if ( !dpi.x || !dpi.y ) { - WindowHDC hdc(hwnd); - dpi.x = ::GetDeviceCaps(hdc, LOGPIXELSX); - dpi.y = ::GetDeviceCaps(hdc, LOGPIXELSY); + dpi = wxGetDPIofHDC(WindowHDC(hwnd)); } return dpi;