c++ - 如何使用 GDIPLUS 绘制第二台显示器

标签 c++ visual-c++ drawing gdi+ gdi

我想在桌面上画一些文字和线条。

我正在使用 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;
}

最佳答案

是的,这出奇地困难。 GetWindowDC 的帮助确实警告我们它只适用于主显示器(这显然只对了一半,因为你的例子中的常规 GDI 位确实有效):

To get the device context for other display monitors, use the EnumDisplayMonitors and CreateDC functions.

这不是什么暗示,但我想这个工作示例(基于您的代码)中的某些内容是有意的:

#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;
}

关于c++ - 如何使用 GDIPLUS 绘制第二台显示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243278/

相关文章:

c++ - 在编译时获取当前月份索引

java - 生成的积分偏向

WPF,C# : Draw a line onto existing bitmap in image control

c# - Onpaint 事件(无效)在一段时间正常运行(运行时)后更改执行顺序

java - 动态更改Windows保存目录?

c++ - 从速度值中滤除噪声和变化

c++ - 为什么 10^1 是 11?

c++ - 模板化函数 编译时返回类型 无参数

windows - 剪辑光标不工作

c++ - 将 MinGW64 库与 MSVC 2010 链接