Home > Enterprise >  Calling GetDesktopWindow() function in winuser.h instead of CWnd::GetDesktopWindow() in an MFC OnBut
Calling GetDesktopWindow() function in winuser.h instead of CWnd::GetDesktopWindow() in an MFC OnBut

Time:09-21

For my own education, I am playing around with the code snippets of some on-line examples.

Early in these examples, there are these lines:

HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);

For convenience reasons, I added a new button to my dialog-based demo MFC project, and the code I expect to test gets written into the ::OnBnClickedRuntest() function.

This was working well in the past, with various other code snippets I studied, modified, and tested this way. For these lines, however, I get a E0144 compile-time error:

a value of type "CWnd *" cannot be used to initialize an entity of type "HWND"

I figured that I run into some sort of name-matching/visibility issue. As I am hoping to call GetDesktopWindow() and GetDC() defined in winuser.h, but apparently the CWnd class also has methods with these exact same names and different return types.

https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdesktopwindow

https://docs.microsoft.com/en-us/cpp/mfc/reference/cwnd-class?view=msvc-160#getdesktopwindow

Obviously, my test code being placed in the OnButtonClicked function of an MFC dialog, GetDesktopWindow() and GetDC() prefers to call the CWnd::GetDesktopWindow() and CWnd::GetDC() methods, instead of the intended winuser.h functions.

How do I clarify to the compiler that instead of the CWnd methods, I want to use the winuser.h functions of the same names?

CodePudding user response:

Use the scope resolution operator on the functions:

::GetDesktopWindow();
::GetDC();
  • Related