[win10] The title bar still appears even though I have handled WM_NCCALCSIZE

峻魁 张 20 Reputation points
2024-07-12T07:44:21.48+00:00

I am attempting to create an application with a custom-drawn title bar, similar to Chrome. Therefore, I need to handle WM_NCCALCSIZE. However, I found that the title bar still appears under certain conditions:


let dpi = unsafe { GetDpiForWindow(handle) };
let frame_x = unsafe { GetSystemMetricsForDpi(SM_CXFRAME, dpi) };
let frame_y = unsafe { GetSystemMetricsForDpi(SM_CYFRAME, dpi) };
let padding = unsafe { GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi) };

// wparam is TRUE so lparam points to an NCCALCSIZE_PARAMS structure
let mut params = lparam.0 as *mut NCCALCSIZE_PARAMS;
let mut requested_client_rect = unsafe { &mut ((*params).rgrc) };

requested_client_rect[0].right -= frame_x + padding;
requested_client_rect[0].left += frame_x + padding;
requested_client_rect[0].bottom -= frame_y + padding;
requested_client_rect[0].top += 2; // If you comment out this line, everything is fine

The reason for this line of code requested_client_rect[0].top += 2 is that on Windows 11, there is a 2-pixel border at the top. Without this line, those two pixels would be obscured:

Without requested_client_rect[0].top += 2:

without

With requested_client_rect[0].top += 2:

with the code

However, adding this line causes the title bar to mysteriously reappear on Windows 10:

win10

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,136 questions
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,511 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 83,101 Reputation points
    2024-07-12T07:59:18.6866667+00:00

    I am attempting to create an application with a custom-drawn title bar, similar to Chrome.

    It is done now with DWM : Custom Window Frame Using DWM