2015-03-24 29 views
0

我想在桌面上繪製一些文本和線條。如何使用GDIPLUS繪製第二臺顯示器

我使用gdiplus.h打印文本與DrawString函數。

但它在主屏幕監視器上唯一的打印文本。

如果在演示模式下,使用2個顯示器,我需要在第二個顯示器中打印文本。

#define _WIN32_WINNT 0x500 
#include <windows.h> 
#include <gdiplus.h> 

using namespace Gdiplus; 

#pragma comment (lib,"Gdiplus.lib") 

int main() { 

    HWND desktop = GetDesktopWindow(); 
    HDC hdc = GetWindowDC(desktop); 

    ULONG_PTR m_gdiplusToken; 
    // Initialize GDI+ 
    GdiplusStartupInput gdiplusStartupInput; 
    GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); 

    while (true) 
    { 

     HPEN newpen; 
     LPPOINT point = NULL; 
     newpen = CreatePen(PS_COSMETIC, 20, RGB(255, 0, 0)); 
     SelectObject(hdc, newpen); 
     MoveToEx(hdc, 1500, 500, point); 
     LineTo(hdc, 1600, 550); 
     //this block works for draw line in second monitor 

     TextOut(hdc, 1500, 300, TEXT("Text of text out"), 17); // this works too 


    //But if I'm use gdiplus only print things on the primary screen 
    Gdiplus::Graphics g(hdc); 

    Pen  pen(Gdiplus::Color(0, 0, 255), 2); 

    g.DrawLine(&pen, 1500, 0, 1700, 600);// dont work 

    g.DrawLine(&pen, 0, 0, 1200, 600);// work, but is in the primary screen 

    FontFamily fontFamily(L"Times New Roman"); 
    Font  font(&fontFamily, 24, FontStyleRegular, UnitPixel); 
    SolidBrush brush(Color(255, 0, 0, 255)); 

    g.DrawString(TEXT("test of GDI+"), 13, &font, PointF(1600.0f, 300.0f), &brush); // dont work 

    g.DrawString(TEXT("test of GDI+"), 13, &font, PointF(500.0f, 300.0f), &brush); // work, but is in the primary screen 
    } 

    Gdiplus::GdiplusShutdown(m_gdiplusToken); 

    return 0; 
} 

回答

2

是的,這是非常困難的。對於GetWindowDC幫助並提醒我們它僅適用於主顯示屏(這顯然是隻對了一半,因爲在你的榜樣常規GDI位做的工作):

要獲取其他顯示監視器設備上下文,請使用EnumDisplayMonitors和CreateDC函數 。

這不是太大的暗示,但我想沿着這個工作示例東西線(根據您的代碼)的目的是:

#define _WIN32_WINNT 0x500 
#include <windows.h> 
#include <gdiplus.h> 

using namespace Gdiplus; 

#pragma comment (lib,"Gdiplus.lib") 

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) 
{ 
    MONITORINFOEXW info; 
    info.cbSize = sizeof(info); 
    ::GetMonitorInfoW(hMonitor, &info); 

    HDC hdc = ::CreateDCW(NULL, info.szDevice, NULL, NULL); 

    { 
     Gdiplus::Graphics graphics(hdc); 

     FontFamily fontFamily(L"Times New Roman"); 
     Font  font(&fontFamily, 24, FontStyleRegular, UnitPixel); 
     SolidBrush brush(Color(255, 0, 0, 255)); 

     graphics.DrawString(
        info.szDevice, 
        wcslen(info.szDevice), 
        &font, 
        PointF(0.0f, 0.0f), 
        &brush); 
    } 

    ::DeleteDC(hdc); 

    return TRUE; 
} 

int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
{ 
    ULONG_PTR m_gdiplusToken; 
    GdiplusStartupInput gdiplusStartupInput; 
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); 

    while (true) 
    { 
     ::EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, NULL); 
    } 

    Gdiplus::GdiplusShutdown(m_gdiplusToken); 

    return 0; 
} 
相關問題