Nikita Buyevich
Nikita Buyevich

Reputation: 29

DwmDefWindowProc always returns false

I'm having trouble utilizing Win API's DwmDefWindowProc. It always returning false, even when DWM is enabled.

I'm sending a WM_NCHITTEST message to the window procedure to detect the action of the mouse position but it's not giving me accurate results (due to DWM being enabled and me needing to call DwmDefWindowProc beforehand).

I'm using a Windows 10 (Ver. 2004) machine and I have tested the same behavior on my Windows 7 virtual machine. If I change the combability of the executable to launch as Windows XP (Disabling DWM, I'm getting the expected behavior).

Otherwise, the information I'm getting back from WM_NCHITTEST is incorrect as the right side of the maximize button returns the expected HTMAXBUTTON while the left side of it returns HTREDUCE.

Reproducing the issue with the Windows Desktop Application template (found below). If you have DWM enabled (Most Windows 10 users would), the maximize button no longer works properly. If run the program in Visual Studio and select the Output tab, you'll see that when you hover to the left (green border in the picture below) of the maximize button you will get a HTMINBUTTON message while if you hover to the right (red border in the picture below) you will get the expected HTMAXBUTTON message.

Where to hover

Here's the sample code

// SimpleWindow.cpp : Defines the entry point for the application.
//

#include "framework.h"
#include "SimpleWindow.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

/* New Stuff */
#include <dwmapi.h>
#pragma comment(lib, "Dwmapi.lib")

void DWMExample(HWND hWnd, unsigned int msg, unsigned int wParam, int lParam);
LRESULT ProcessNCHitTest(HWND hWnd, int lParam);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                      _In_opt_ HINSTANCE hPrevInstance,
                      _In_ LPWSTR lpCmdLine,
                      _In_ int nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_SIMPLEWINDOW, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance(hInstance, nCmdShow)) {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SIMPLEWINDOW));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0)) {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int)msg.wParam;
}


//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SIMPLEWINDOW));
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_SIMPLEWINDOW);
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance; // Store instance handle in our global variable

    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd) {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId) {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    /* New Stuff */
    if (message == WM_NCHITTEST) {
        return ProcessNCHitTest(hWnd, lParam);
    }
    DWMExample(hWnd, message, wParam, lParam);

    return DefWindowProc(hWnd, message, wParam, lParam);
}

void DWMExample(const HWND hWnd, const unsigned int msg, const unsigned int wParam, const int lParam)
{
    switch (msg) {
    case WM_NCMOUSEMOVE:
    case WM_MOUSEMOVE:
        {
            const auto ht = SendMessageA(hWnd, WM_NCHITTEST, wParam, lParam);
            switch (ht) {
            case HTBORDER:
                OutputDebugStringA("HTBORDER\n");
                break;
            case HTBOTTOM:
                OutputDebugStringA("HTBOTTOM\n");
                break;
            case HTBOTTOMLEFT:
                OutputDebugStringA("HTBOTTOMLEFT\n");
                break;
            case HTBOTTOMRIGHT:
                OutputDebugStringA("HTBOTTOMRIGHT\n");
                break;
            case HTCAPTION:
                OutputDebugStringA("HTCAPTION\n");
                break;
            case HTCLIENT:
                OutputDebugStringA("HTCLIENT\n");
                break;
            case HTCLOSE:
                OutputDebugStringA("HTCLOSE\n");
                break;
            case HTERROR:
                OutputDebugStringA("HTERROR\n");
                break;
            case HTGROWBOX:
                OutputDebugStringA("HTGROWBOX\n");
                break;
            case HTHELP:
                OutputDebugStringA("HTHELP\n");
                break;
            case HTHSCROLL:
                OutputDebugStringA("HTHSCROLL\n");
                break;
            case HTLEFT:
                OutputDebugStringA("HTLEFT\n");
                break;
            case HTMENU:
                OutputDebugStringA("HTMENU\n");
                break;
            case HTMAXBUTTON:
                OutputDebugStringA("HTMAXBUTTON\n");
                break;
            case HTMINBUTTON:
                OutputDebugStringA("HTMINBUTTON\n");
                break;
            case HTNOWHERE:
                OutputDebugStringA("HTNOWHERE\n");
                break;
                // case HTREDUCE:
                //  OutputDebugStringA("HTREDUCE\n");
                //  break;
            case HTRIGHT:
                OutputDebugStringA("HTRIGHT\n");
                break;
                // case HTSIZE:
                //  OutputDebugStringA("HTSIZE\n");
                //  break;
            case HTSYSMENU:
                OutputDebugStringA("HTSYSMENU\n");
                break;
            case HTTOP:
                OutputDebugStringA("HTTOP\n");
                break;
            case HTTOPLEFT:
                OutputDebugStringA("HTTOPLEFT\n");
                break;
            case HTTOPRIGHT:
                OutputDebugStringA("HTTOPRIGHT\n");
                break;
            case HTTRANSPARENT:
                OutputDebugStringA("HTTRANSPARENT\n");
                break;
            case HTVSCROLL:
                OutputDebugStringA("HTVSCROLL\n");
                break;
                // case HTZOOM:
                //  OutputDebugStringA("HTZOOM\n");
                //  break;
            }
        }
        break;
    }
}

LRESULT ProcessNCHitTest(const HWND hWnd, const int lParam)
{
    BOOL isDWMEnabled = false;
    if (FAILED(DwmIsCompositionEnabled(&isDWMEnabled))) {
        return DefWindowProcA(hWnd, WM_NCHITTEST, 0, lParam);
    }
    if (!isDWMEnabled) {
        return DefWindowProcA(hWnd, WM_NCHITTEST, 0, lParam);
    }
    LRESULT result = 0;
    if (!DwmDefWindowProc(hWnd, WM_NCHITTEST, 0, lParam, &result)) {
        return DefWindowProcA(hWnd, WM_NCHITTEST, 0, lParam);
    }
    return result;
}


// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message) {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

Any idea where I'm going wrong? Any help would be appreciated!

Upvotes: 1

Views: 652

Answers (1)

Emmanuel Ichbiah
Emmanuel Ichbiah

Reputation: 309

I think I have found a workaround. Using the WM_GETTITLEBARINFOEX message, I sort of do my own hit test code computation:

  WINDOWS.SendMessage(
              window,
              WM_GETTITLEBARINFOEX,
              0,
              LPARAM(@titleBarInfo));

  //  Compute hit test code.

  if      WINDOWS.PtInRect( titleBarInfo.rgRect[2], pt) then hitTestCode := HTMINBUTTON
  else if WINDOWS.PtInRect( titleBarInfo.rgRect[3], pt) then hitTestCode := HTMAXBUTTON
  else if WINDOWS.PtInRect( titleBarInfo.rgRect[5], pt) then hitTestCode := HTCLOSE
  else
    begin
      //  Else I rely on WM_NCHITTEST.

      hitTestCode := WINDOWS.SendMessage(
                                 window,
                                 WM_NCHITTEST,
                                 0,
                                 MAKELPARAM(pt.x, pt.y));
    end;

Upvotes: 0

Related Questions