支持扩展屏的VC6实现方法

在开发518年会抽奖软件(www.518cj.net)的时候,软件需支持扩展屏,电脑屏后台操作,扩展大屏滚动抽奖。主要包括以下三个功能,主窗口全屏化(主屏内)、主窗口窗口化(主屏内)、主窗口只显示到扩展屏(第二屏)。

一、主窗口全屏化、窗口化(主屏内)

	//创建窗口 - 以 WS_POPUP 样式创建窗口
		m_hMain = CreateWindow (WNDCLASS_NAME, g_title, WS_POPUP, 0, 0, 0, 0, NULL, NULL, m_hInst, NULL);

	//窗口化 - x/y:窗口位于主屏的位置,w/h:窗口的宽高
		MoveWindow (m_hMain, m_wmode.x, m_wmode.y, m_wmode.w, m_wmode.h, TRUE);

	//全屏化 - cx/cy:主屏的大小
		int cx = GetSystemMetrics (SM_CXSCREEN);
		int cy = GetSystemMetrics (SM_CYSCREEN);
		MoveWindow (m_hMain, 0, 0, cx, cy, TRUE);
	

二、主窗口只显示到扩展屏(第二屏)

	//枚举显示屏 - 获得扩展屏(第二屏)的坐标,left/top/right/bottom
		static BOOL CALLBACK Callback_enumMonitors (HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
		{
			RECT* pRect = (RECT*) dwData;
		
			MONITORINFO mi = {0};         
			mi.cbSize = sizeof (MONITORINFO);
			GetMonitorInfo (hMonitor, &mi);
			if (mi.dwFlags != MONITORINFOF_PRIMARY) 
			{
				*pRect = mi.rcMonitor;
				return FALSE;
			}
			return TRUE;
		}

	//显示主窗口到扩展屏
		void Tapp::show_expanscrn ()
		{
			RECT rc = {0};
			EnumDisplayMonitors (NULL, NULL, Callback_enumMonitors, (LPARAM) &rc);
			if (rc.left == 0 && rc.top == 0 && rc.right == 0 && rc.bottom == 0) 
				MessageBox (m_hMain, L"无扩展屏", g_title, MB_OK | MB_ICONWARNING);
			else 
				MoveWindow (m_hMain, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, TRUE);
		}
	

三、对话框位于扩展屏中央

	//对话框默认是位于主屏的中央的,如果想位于扩展屏中央,就要在对话框的 WM_INITDIALOG 消息中,执行下列函数
		Tfuns::center_box (hDlg);

		void Tfuns::center_box (HWND hDlg)
		{
			RECT rc;
			HWND hDesktop = GetDesktopWindow ();
			GetWindowRect (hDesktop, &rc);
			int desk_h = rc.bottom - rc.top;
			int desk_w = rc.right - rc.left;
		
			GetWindowRect (hDlg, &rc);
			int wnd_w = rc.right - rc.left;
			int wnd_h = rc.bottom - rc.top;
		
			int x = (desk_w - wnd_w) / 2;
			if (x < 0) x = 0;
		
			int y = (desk_h - wnd_h) / 2;
			if (y < 0) y = 0;
		
			SetWindowPos (hDlg, NULL, x, y, 0, 0, SWP_NOSIZE);
		}