标题: SDK编程中的窗口居中
作者: Demon
链接: https://demon.tw/programming/sdk-center-window.html
版权: 本博客的所有文章,都遵守“署名-非商业性使用-相同方式共享 2.5 中国大陆”协议条款。
MFC程序中调用CWnd::CenterWindow就可以实现窗口居中,但是纯SDK编程中没有CenterWindow这个函数,需要自定义一个。Google到一个比较好的实现。
自定义函数一:
BOOL CENTER_WINDOW(HWND hWnd, HWND hParent) { RECT rcWnd, rcParent; POINT ptNew; int nWidth; int nHeight; int nParentWidth; int nParentHeight; if (!IsWindow(hWnd)) return FALSE; if (!IsWindow(hParent) || 0 == hParent) hParent = GetDesktopWindow(); GetWindowRect(hWnd, &rcWnd); GetWindowRect(hParent, &rcParent); nWidth = rcWnd.right - rcWnd.left; nHeight = rcWnd.bottom - rcWnd.top; nParentWidth = rcParent.right - rcParent.left; nParentHeight = rcParent.bottom - rcParent.top; ptNew.x = rcParent.left + (nParentWidth - nWidth) / 2; ptNew.y = rcParent.top + (nParentHeight - nHeight) / 2; return MoveWindow(hWnd, ptNew.x, ptNew.y, nWidth, nHeight, TRUE); }
自定义函数二:
赞赏void CentreWindow(HWND hwnd) { RECT winrect, workrect; int workwidth, workheight, winwidth, winheight; SystemParametersInfo(SPI_GETWORKAREA, 0, &workrect, 0); workwidth = workrect.right - workrect.left; workheight = workrect.bottom - workrect.top; GetWindowRect(hwnd, &winrect); winwidth = winrect.right - winrect.left; winheight = winrect.bottom - winrect.top; winwidth = min(winwidth, workwidth); winheight = min(winheight, workheight); SetWindowPos(hwnd, HWND_TOP, workrect.left + (workwidth-winwidth) / 2, workrect.top + (workheight-winheight) / 2, winwidth, winheight, SWP_SHOWWINDOW); SetForegroundWindow(hwnd); }
微信赞赏支付宝赞赏
随机文章: