Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

c++ - GetDC(NULL) gets primary monitor or virtual screen?

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...