Detect resolution / screen orientation change without a window?

Hello,

I'm writing a console application and I need to detect when the screen resolution changes, or (if running on a computer with dual monitors) when a screen changes orientation from another.

I would like to avoid using any sort of polling method. I've heard of WM_DISPLAYCHANGE, but I can't figure out how to actually capture it without creating a window. It's important that I don't create a window as this application has to run in the background and can't interfere with any active windows.

The application itself makes use of mouse and keyboard low level hooks. It allows you to extend your desktop to another computer (that can be of a different OS). When the application starts I'm going to have it check for specific coordinates to "warp" the cursor to the other machine. Having the screen resolution change can screw this up, thus is the reason for this question.

Luke
Okay, using the WM_DISPLAYCHANGE message only works with top level windows, that having been said, one can just make a top level window that never gets displayed.
The following code makes a simple window (no height, width, menus, icons, cursor et cetera) & then sets up a message loop, handles the WM_DISPLAYCHANGE message with a MessageBox (put your code there).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <windows.h>

char szClassName[]="WindowsApp";

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch(message)
  {
    case WM_DISPLAYCHANGE:
      MessageBox(0,"Resolution has been changed!","Information",0);
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    default:
      return DefWindowProc(hwnd, message, wParam, lParam);
  }
  return 0;
}

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
  WNDCLASSEX wc={sizeof(WNDCLASSEX),0,WindowProcedure,0,0,hThisInstance,NULL,NULL,0,NULL,szClassName,NULL};
  if(!RegisterClassEx(&wc))
  {
    return 0;
  }

  CreateWindowEx(0, szClassName, 0, WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, HWND_DESKTOP, NULL, hThisInstance, NULL);

  MSG messages;
  while(GetMessage(&messages, NULL, 0, 0))
  {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
  }

  return messages.wParam;
}


Hope this helps.
Works perfectly, thanks!
Topic archived. No new replies allowed.