前一篇文章详述了利用VC++实现Windows下的“屏幕分辨率”下的设置主屏、设置分辨率、设置显示方向以及屏幕复制功能。但是在有些情况下这样是不够的,比如:显示器与第二显示设备率时显示方向不一致的情况;两个显示器分辨率不一致的情况;某一屏幕不显示标题栏的情况等。
基于以上需求,开发了如下代码:
DWORD WINAPI ThreadFunc(HANDLE Thread)
{
while(true)
{
::Sleep(5);
HWND hWnd;
//hWnd = AfxGetMainWnd()->m_hWnd; //获取自身窗口句柄
//AfxGetMainWnd()->SetWindowText (_T("Application title"));
hWnd = ::GetDesktopWindow();//获得屏幕的HWND
//hWnd = this->m_hWnd; //获取自身窗口句柄
//this->SetWindowTextW(_T("多屏显示例程"));
//pCWnd=FromHandle(hWnd);
HDC hScreenDC = ::GetDC(hWnd); //获得屏幕的HDC
HDC MemDC = ::CreateCompatibleDC(hScreenDC);
RECT rect; //要截屏的矩形
::GetWindowRect(hWnd, &rect); //获得当前活动窗口的大小(截取的大小位置)
SIZE screensize;
screensize.cx = rect.right - rect.left;
screensize.cy = rect.bottom - rect.top;
HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC,screensize.cx,screensize.cy);
HGDIOBJ hOldBMP = ::SelectObject(MemDC,hBitmap);
::BitBlt(MemDC, 0, 0, screensize.cx, screensize.cy, hScreenDC, 0, 0, SRCCOPY);
::SelectObject(MemDC,hOldBMP);
::DeleteObject(MemDC);
::ReleaseDC(hWnd, hScreenDC);
//为屏幕创建设备描述表
HDC hExtDC = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//CDC *pDC=GetDC();//获取窗体的cdc
//HDC hExtDC=pDC->m_hDC;//获取窗体的hdc
HDC MemDC1 = CreateCompatibleDC(hExtDC); //创建画布
HGDIOBJ hOldBMP1 = ::SelectObject(MemDC1,hBitmap); //选入画布
::StretchBlt(hExtDC,1920,0,screensize.cx,screensize.cy,MemDC1,0,0,screensize.cx,screensize.cy,SRCCOPY); //将内存画布缩略显示到窗体中
::SelectObject(MemDC1,hOldBMP1); //选出画布
::DeleteObject(MemDC1); //删除内存hdc
::ReleaseDC(hWnd,hExtDC); //释放实际窗体的hdc
::DeleteObject(hOldBMP);
::DeleteObject(hOldBMP1);
::DeleteObject(hBitmap);
}
return 0;
}
HANDLE th;
DWORD dwThreadId;
void CMulti_DisplayDlg::OnBnClickedButton3()
{
th = ::CreateThread(NULL,0,ThreadFunc,NULL,0,&dwThreadId);
}