Set window always on top and Make center in MFC

Add following code at the end of function OnInitDialog(). This will make window always on top and center on screen.

	GetWindowRect( rect );
	int screenWidth = GetSystemMetrics(SM_CXSCREEN);
	int screenHeight = GetSystemMetrics(SM_CYSCREEN);
	// now change the size, position, and Z order of the window.
	::SetWindowPos(m_hWnd ,       // handle to window
				HWND_TOPMOST,  // placement-order handle
				(screenWidth - rect.Width())/2,
				(screenHeight - rect.Height())/2,     // vertical position
				rect.Width(),  // width
				rect.Height(), // height
				SWP_SHOWWINDOW // window-positioning options

Posted in Uncategorized. Leave a Comment »