自定义函数一
BOOL CENTER_WINDOW(HWND hWnd, HWND hParent) // 自定义的窗口居中函数 { if (!IsWindow(hWnd)) return FALSE; if (!IsWindow(hParent) || 0 == hParent) hParent = GetDesktopWindow(); RECT rcWnd; GetWindowRect(hWnd, &rcWnd); RECT rcParent; GetWindowRect(hParent, &rcParent); POINT ptNew; int nWidth; int nHeight; int nParentWidth; int nParentHeight; 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; // Find how large the desktop work area is SystemParametersInfo(SPI_GETWORKAREA, 0, &workrect, 0); int workwidth = workrect.right - workrect.left; int workheight = workrect.bottom - workrect.top; // And how big the window is GetWindowRect(hwnd, &winrect); int winwidth = winrect.right - winrect.left; int winheight = winrect.bottom - winrect.top; // Make sure it's not bigger than the work area winwidth = min(winwidth, workwidth); winheight = min(winheight, workheight); // Now centre it SetWindowPos(hwnd, HWND_TOP, workrect.left + (workwidth-winwidth) / 2, workrect.top + (workheight-winheight) / 2, winwidth, winheight, SWP_SHOWWINDOW); SetForegroundWindow(hwnd); }