Identify display orientation (flipped or not) in Win32 app

Shyam Butani 330 Reputation points
2024-05-21T11:57:06.57+00:00

Hello,

In Win32 app, I want to capture event when display orientation is changed. below is the sample code snippet to get an idea:

LRESULT CALLBACK WindowProc(HWND hWnd, UINT messageCode, WPARAM wParam, LPARAM lParam)
{
    switch (messageCode)
    {

    case WM_DISPLAYCHANGE: {
        // Handle display orientation change
        int width = LOWORD(lParam);
        int height = HIWORD(lParam);

        if (width > height) {
            // ORIENTATION_LANDSCAPE
        }
        else {
            // ORIENTATION_PORTRAIT
        }

        break;
    }

    default:
        return DefWindowProc(hWnd, messageCode, wParam, lParam);
    }

    return 0;
}

Using this way, I'm able to identify whether new orientation is landscape or portrait. That is fine and working.

I wanted to know if there is any way to find out that current display orientation is 'landscape' or 'landscape_flipped'?

Similar for 'portrait' and 'portrait_flipped'.

Thanks.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,680 questions
{count} votes

2 additional answers

Sort by: Most helpful
  1. RLWA32 45,941 Reputation points
    2024-05-21T12:34:36.9566667+00:00
    0 comments No comments

  2. Shyam Butani 330 Reputation points
    2024-05-22T06:06:01.7766667+00:00

    Following worked for me.

    DEVMODEW dm;
    
    // Initialize the DEVMODE structure
    ZeroMemory(&dm, sizeof(dm));
    dm.dmSize = sizeof(dm);
    
    // Retrieve display settings
    if (EnumDisplaySettingsExW(NULL, ENUM_CURRENT_SETTINGS, &dm, 0)) {
    
        // Check the display orientation
        if (dm.dmDisplayOrientation == DMDO_DEFAULT) {
            printf("Display Orientation: Default\n");
        }
        else if (dm.dmDisplayOrientation == DMDO_90) {
            printf("Display Orientation: 90 degrees\n");
        }
        else if (dm.dmDisplayOrientation == DMDO_180) {
            printf("Display Orientation: 180 degrees\n");
        }
        else if (dm.dmDisplayOrientation == DMDO_270) {
            printf("Display Orientation: 270 degrees\n");
        }
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.