Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm want to draw some text and lines over the desktop.

I'm using gdiplus.h for print texts with the function DrawString.

But its only print text on a primary screen monitor.

If has in presentation mode, with 2 monitors I need to print text in a second monitor.

C++
#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;
}
Posted

1 solution

 
Share this answer
 
Comments
Paulo Filipe Dantas 25-Mar-15 1:40am    
@barneyman
Thanks for help.
I'm try with this, HDC hdc = GetDC(NULL); , that documentation say get the virtual screen. But doesnt work too.

if I'm change
"
HWND desktop = GetDesktopWindow();
HDC hdc = GetWindowDC(desktop);
"
for
"
HDC hdc = GetDC(NULL);
"
the result of the code above is the same.

You can help-me showing the code to draw on the second monitor??
barneyman 25-Mar-15 6:58am    
did you look at that link? it explains that all DCs you get are normally for the primary monitor - it suggests using EnumDisplayMonitors/GetMonitorInfo/CreateDC
Paulo Filipe Dantas 25-Mar-15 13:46pm    
@barneyman
Yes, I'm tryied with this way

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

using namespace Gdiplus;

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

HDC hdc;

BOOL CALLBACK MonitorEnumProcCallback(_In_ HMONITOR hMonitor, _In_ HDC DevC, _In_ LPRECT lprcMonitor, _In_ LPARAM dwData) {

MONITORINFOEX info;
info.cbSize = sizeof(MONITORINFO);

BOOL monitorInfo = GetMonitorInfo(hMonitor, &info);

cout << "info szDevice" << info.szDevice << endl;

if (monitorInfo) {
hdc = CreateDC(NULL, info.szDevice, NULL, NULL);
}

return TRUE;
}

int main() {

HWND desktop = GetDesktopWindow();
HDC DevC = GetDC(desktop);
BOOL b = EnumDisplayMonitors(DevC, NULL, MonitorEnumProcCallback, 0);

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

and I'm tryied too with

BOOL b = EnumDisplayMonitors(NULL, NULL, MonitorEnumProcCallback, 0);


and dosent work for any case, dont print anything in the any monitors
barneyman 25-Mar-15 17:20pm    
sorry - no idea - a quick google, and you're not the first to find that problem with GDI+
Paulo Filipe Dantas 25-Mar-15 18:17pm    
Yes, I'm put this question in other places.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900