Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetDC(NULL) gets primary monitor or virtual screen?

Tags:

c++

c

winapi

Looking around the net I see that most people think that GetDC(NULL) will get a device context for the entire primary monitor (the one with 0,0 at its top left). However, I get the feeling most people are just saying that because that's what the msdn page for GetDC might be saying.

However, if you look at the following two pages (at least these two) seem to be saying that GetDC(NULL) returns a device context that covers the entire virtual screen (the one that encompasses every monitor attached to the system).

(1) https://www.microsoft.com/msj/0697/monitor/monitor.aspx -> search the page for "This gets the RECT of the virtual desktop" and look at the bits around that statement (particularly the GetDC(NULL) above it).

(2) http://msdn.microsoft.com/en-gb/library/windows/desktop/dd162610%28v=vs.85%29.aspx -> search the page for "GetDC(NULL)"

I have been trying to figure out which it really is, but the multitude of conflicting opinions defeats me.

Does anyone have any real experience of this, and can test it on a multimonitor system? (I only have one monitor so I can't.)

Does it get a DC covering the entire primary monitor OR a DC covering the entire virtual screen?

Edit

For anyone wanting to try it out, on my system, if I create a default project and put the following in WinMain it turns the screen black. If you have multiple monitors and you try it, the question becomes does it turn just your primary monitor black OR all your monitors?

HDC hdc = GetDC(NULL);
RECT r = {LONG_MIN, LONG_MIN, LONG_MAX, LONG_MAX};
FillRect(hdc, &r, (HBRUSH)(COLOR_BTNTEXT + 1));
ReleaseDC(NULL, hdc);
like image 555
user2044467 Avatar asked Feb 05 '13 19:02

user2044467


1 Answers

It gets a DC that covers the entire virtual screen. I just tested with:

#include <windows.h>
#include <conio.h>

int main() {

    HDC screen = GetDC(NULL);

    RECT r = {-1200, 100, -200, 500};
    HBRUSH br = CreateSolidBrush(RGB(0, 255, 0));

    FillRect(screen, &r, br);

    getch();
    InvalidateRect(NULL, &r, true);

    return 0;
}

...and it successfully draws a green rectangle on my secondary screen (positioned to the left of the primary screen so it has negative X coordinates).

like image 133
Jerry Coffin Avatar answered Sep 19 '22 12:09

Jerry Coffin