1

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!

  • 1
    I'm not really sure what it is you're trying to do, but `WM_MOUSEMOVE` and `WM_NCMOUSEMOVE` use totally different coordinate systems; you can't pass the coords from a `WM_MOUSEMOVE` message to `WM_NCHITTEST` and expect them to work. – Jonathan Potter Dec 08 '20 at 12:24
  • Calling `DefWindowProcA` from a window procedure of a window class registered using `RegisterClassExW` isn't going to end well. This is [documented](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-defwindowproca), too, for *every* API that isn't encoding-neutral. – IInspectable Dec 08 '20 at 12:45
  • Missing SimpleWindow.h file. The rectangles are in about the right spot for a legacy window, one that doesn't use the Win10-specific theme. Try setting the Linker > System > Minimum required version to 10.0 – Hans Passant Dec 08 '20 at 13:40
  • Hi,I can reproduce this issue now. And I will confirm it with Internal engineer, and response here if there is any update.Thanks for your understanding. – Zeus Dec 09 '20 at 06:56
  • @ZhuSong-MSFT Any updates on the issue? – Nikita Buyevich Dec 15 '20 at 15:24
  • I can reproduce the problem in Win 10, and the same example works normally in Win 8.1. This may be a Win10 issue only. I have submitted it to the Internal engineer, and any updates will be answered here, thank you. – Zeus Dec 16 '20 at 00:41
  • After confirmation, this is indeed a bug that currently exists and has been tracked. It will be fixed in the future. Thank you for raising this issue. – Zeus Jan 25 '21 at 05:50
  • @ZhuSong-MSFT Thanks for the update! How can I be alerted when this issue will be fixed? – Nikita Buyevich Jan 25 '21 at 17:59
  • The current priority of this bug ticket is general, and the plan is to fix it in a future Windows build, but there is currently no clear Estimated timeline. You can check if it is fixed in future Windows updates. – Zeus Jan 26 '21 at 01:40
  • @ZhuSong-MSFT I see, so everyone who doesn't have that update, will continue to experience this bug, correct? – Nikita Buyevich Jan 26 '21 at 17:53
  • Is there NO workaround to this issue? It is very annoying. – Emmanuel Ichbiah Jun 23 '23 at 15:31

1 Answers1

0

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;