I am using plain C++ (without MFC) to implement an application.
I wanted to add custom Min/Max/Close button
The code below is what I used.
For HTCAPTION and other border related definition works like a charm,
but I could not get HTMINBUTTON, HTMINBUTTON, HTCLOSE to work in the same way.
Is anything else needed to be implemented for the NCHITTEST to take effect?
// Defining min/max/close
if ((p.x > rt.right - 130) && (p.x < rt.right - 104) &&
(p.y > 41) && (p.y < 67))
return HTMINBUTTON;
else if ((p.x > rt.right - 100) && (p.x < rt.right - 74) &&
(p.y > 41) && (p.y < 67))
return HTMAXBUTTON;
else if ((p.x > rt.right - 70) && (p.x < rt.right - 44) &&
(p.y > 41) && (p.y < 67))
return HTCLOSE;
// Defining window border and caption
else if ((p.x > EDGE) && (p.x < rt.right-EDGE) &&
(p.y > EDGE) && (p.y < rt.bottom-EDGE))
return HTCAPTION;
else if (p.x <= EDGE && p.y <= EDGE)
return HTTOPLEFT;
else if (p.x <= EDGE && p.y >= rt.bottom - EDGE)
return HTBOTTOMLEFT;
else if (p.x >= rt.right - EDGE && p.y <= EDGE)
return HTTOPRIGHT;
else if (p.x >= rt.right - EDGE && p.y >= rt.bottom - EDGE)
return HTBOTTOMRIGHT;
else if (p.x <= EDGE)
return HTLEFT;
else if (p.x >= rt.right - EDGE)
return HTRIGHT;
else if (p.y <= EDGE)
return HTTOP;
else if (p.y >= rt.top - EDGE)
return HTBOTTOM;
else
return DefWindowProc(hWnd, message, wParam, lParam);
If you’re drawing the entire Non-Client area of your program (ie. you have a custom window title bar and border), then your best option would be to remove the default title bar and border and use the entire client area as your window. This way, you can just make a custom button (or owner-drawn button) with the graphics needed for your min/max/exit buttons and place them in the title bar area as a button control. Your program will still act like it has a title bar and border because you are handling the WM_NCHITTEST message. I just did this exact thing with my custom window today and it works very well.
Edit: I forgot to add that returning HTMAX/HTMIN/HTCLOSE will not cause the window to react. The value you return from WM_NCHITTEST is sent in the WPARAM of other messages, notably WM_NCLBUTTONDOWN and WM_NCLBUTTONUP. Unfortunately, the WM_NCLBUTTONUP message does not get sent properly because when you click in the title bar, it captures the mouse.