The following code is adapted from the "Global Titlebar Hook" example I found at http://www.catch22.net/content/snippets. I modified the example to make it MFC-friendly. It returns the X-coordinate of the leftmost titlebar button but it could easily be modified to find the position of any of the buttons.
#define B_EDGE 2
int CMyWindow::CalcRightEdge()
{
if(GetStyle() & WS_THICKFRAME)
return GetSystemMetrics(SM_CXSIZEFRAME);
else
return GetSystemMetrics(SM_CXFIXEDFRAME);
}
int CMyWindow::findNewButtonPosition()
{
int nButSize = 0;
DWORD dwStyle = GetStyle();
DWORD dwExStyle = GetExStyle();
if(dwExStyle & WS_EX_TOOLWINDOW)
{
int nSysButSize = GetSystemMetrics(SM_CXSMSIZE) - B_EDGE;
if(GetStyle() & WS_SYSMENU)
nButSize += nSysButSize + B_EDGE;
return nButSize + CalcRightEdge();
}
else
{
int nSysButSize = GetSystemMetrics(SM_CXSIZE) - B_EDGE;
// Window has Close [X] button. This button has a 2-pixel
// border on either size
if(dwStyle & WS_SYSMENU)
nButSize += nSysButSize + B_EDGE;
// If either of the minimize or maximize buttons are shown,
// Then both will appear (but may be disabled)
// This button pair has a 2 pixel border on the left
if(dwStyle & (WS_MINIMIZEBOX | WS_MAXIMIZEBOX) )
nButSize += B_EDGE + nSysButSize * 2;
// A window can have a question-mark button, but only
// if it doesn't have any min/max buttons
else if(dwExStyle & WS_EX_CONTEXTHELP)
nButSize += B_EDGE + nSysButSize;
// Now calculate the size of the border...aggghh!
return nButSize + CalcRightEdge();
}
}