本文介绍了使用EnumDisplayMonitors的屏幕保护程序仅在第二个监视器上绘制黑色矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下代码,但除了第二个监视器上的黑色矩形(使用GDI +)外,我什么都没得到.有人听说过这个问题吗?我在第二个监视器上所做的任何绘图(位图或文本)都呈现为黑色矩形.谢谢!

I tried the following code but I am getting nothing but black rectangles where my drawing images should be (using GDI+) on my 2nd monitor. Has anyone heard of this issue? Any drawing I do (bitmap or text) on the second monitor renders as a black rectangle. Thanks!

LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)  
{
   static UINT         uTimer;   // timer identifier

   // Handles screen saver messages  
   switch(message)  
   { 
      // <snip>
      case WM_PAINT:   //  Sent when the system or another application makes a request to paint a portion of an application's window (e.g., UpdateWindow/RedrawWindow)
      {
         PAINTSTRUCT lpPaint = {0};
         HDC hdc = BeginPaint(hwnd, &lpPaint );
            EnumDisplayMonitors(hdc, NULL, MonitorNumProcPaint, 0);  // For every monitor attached, call the passed in function
         EndPaint(hwnd, &lpPaint);
         return true;
      }
      // <snip>
   }
}
 
BOOL CALLBACK MonitorNumProcPaint(HMONITOR hMonitor, HDC hdc, LPRECT lprcMonitor, LPARAM data)
{
   // If the coordinates of the top left corner are 0,0, then we're rendering on the primary monitor, otherwise we're not.
   if (lprcMonitor->left == 0 && lprcMonitor->top == 0)
      return (PaintPrimaryDisplay(hMonitor, hdc, lprcMonitor, data));
   else
      return (PaintNonPrimaryDisplay(hMonitor, hdc, lprcMonitor, data));
}
 
bool PaintNonPrimaryDisplay(HMONITOR hMonitor, HDC hdc, LPRECT lprcMonitor, LPARAM data)
{
   Gdiplus::Graphics * pGraphics = Gdiplus::Graphics::FromHDC(hdc);
 
   if (gpBitmapMotivation2)
   {
      pGraphics->DrawImage(gpBitmapMotivation2, lprcMonitor->left, lprcMonitor->top, lprcMonitor->right - lprcMonitor->left, lprcMonitor->bottom - lprcMonitor->top);
   }
 
   int sizeX = (int) ceil( (lprcMonitor->right - lprcMonitor->left) * SS_SCREEN_PERCENTAGE);
   int sizeY = (int) ceil( (lprcMonitor->bottom- lprcMonitor->top)  * SS_SCREEN_PERCENTAGE);
   DrawMyText(lprcMonitor, sizeX, sizeY, pGraphics, "MyText"); // This custom function sets up the font, etc and renders the text.

   BitBlt(hdc, lprcMonitor->left, lprcMonitor->top, lprcMonitor->right - lprcMonitor->left, lprcMonitor->bottom - lprcMonitor->top, hdc, lprcMonitor->left, lprcMonitor->top, SRCCOPY);
 
   return true;
}

推荐答案



gHdc = BeginPaint(hwnd, &gPaintStruct);   // Get Device Context handle from canvas
   // For every monitor attached, call the passed in function
   EnumDisplayMonitors(gHdc, NULL, MonitorNumProcPaint, (LPARAM)&gHdc); 
EndPaint(hwnd, &gPaintStruct);            // Painting is done

BOOL PaintNonPrimaryDisplay(HMONITOR hMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
   HDC *pdc = (HDC*)dwData;
//From http://www.realtimesoft.com/multimon/forum/messages.asp?Topic=3760&tmpl=UltraMon)

   // Create an "off screen" DC (double buffer) to avoid flickering
   HDC hdcMem = CreateCompatibleDC(*pdc);
   HBITMAP hbmMem = CreateCompatibleBitmap(*pdc, lprcMonitor->right - lprcMonitor->left, lprcMonitor->bottom - lprcMonitor->top);
   HGDIOBJ hOld = SelectObject(hdcMem, hbmMem);

   // Paint everything into the double buffer
   Gdiplus::Graphics  *gpGraphics = Gdiplus::Graphics::FromHDC(hdcMem);
   if (gpGraphics == NULL)
      return false;

   if (gpBitmapMotivations[1])
   {
      gpGraphics->DrawImage(gpBitmapMotivations[1], 0, 0, lprcMonitor->right - lprcMonitor->left, lprcMonitor->bottom - lprcMonitor->top);
   }

   DrawOverlay(0, 0, lprcMonitor->right - lprcMonitor->left, lprcMonitor->bottom - lprcMonitor->top, gpGraphics);

   // Transfer the "off screen" (double buffer) DC (Device Context) to the screen
   // BitBlt Specs: http://msdn.microsoft.com/en-us/library/dd183370(v=VS.85).aspx
   BitBlt(*pdc, lprcMonitor->left, lprcMonitor->top, lprcMonitor->right - lprcMonitor->left, lprcMonitor->bottom - lprcMonitor->top, hdcMem, 0, 0, SRCCOPY);
   
   // Free the "off screen" DC
   delete gpGraphics;
   SelectObject(hdcMem, hOld);
   DeleteObject(hbmMem);
   DeleteDC(hdcMem);

   return TRUE;
}


这篇关于使用EnumDisplayMonitors的屏幕保护程序仅在第二个监视器上绘制黑色矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 04:39