Deep BlueVBScriptWMIPHPC语言JavaScriptWindows API路由器Windows函数Python | SDK编程中的窗口居中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); } 参考链接:自定义的窗口居中函数–CentreWindow |