急求高手看看我的截图程序 为什么运行后没效果
BOOL CScreenDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_bDraw=false;
//使窗口变成透明
this->ShowWindow(SW_MAXIMIZE);
//加入WS_EX_LAYERED扩展属性,必须要加上这个属性
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
//调用User32.DLL中的函数
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(fun)fun(this->GetSafeHwnd(),0,100,2);
FreeLibrary(hInst);
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CScreenDlg::OnLButtonDown(UINT nFlags, CPoint point) //鼠标左键按下消息处理
{
// TODO: Add your message handler code here and/or call default
m_bDraw=true;
GetCursorPos(&(this->startPt));
GetCursorPos(&(this->endPt));
CDialog::OnLButtonDown(nFlags, point);
}
void CScreenDlg::OnLButtonUp(UINT nFlags, CPoint point) //鼠标左键弹起消息处理
{
// TODO: Add your message handler code here and/or call default
m_bDraw=false;
GetCursorPos(&(this->endPt));
CRect rect(this->startPt.x,this->startPt.y,this->endPt.x,this->endPt.y);
CClientDC dc(this);
CDialog::OnLButtonUp(nFlags, point);
}
void CScreenDlg::OnMouseMove(UINT nFlags, CPoint point) //鼠标移动消息处理
{
// TODO: Add your message handler code here and/or call default
if(m_bDraw)
{
HDC hdc=::GetDC(this->GetSafeHwnd());
SetROP2(hdc,R2_NOTXORPEN);
HPEN hpen;
hpen=CreatePen(PS_SOLID,2,RGB(255,0,0));
SelectObject(hdc,hpen);
SelectObject(hdc,GetStockObject(NULL_BRUSH));
::Rectangle(hdc,this->startPt.x,this->startPt.y,this->endPt.x,this->endPt.y);
// endPt=point;
GetCursorPos(&(this->endPt));
::Rectangle(hdc,this->startPt.x,this->startPt.y,this->endPt.x,this->endPt.y);
::ReleaseDC(this->GetSafeHwnd(),hdc);
::DeleteObject(hpen);
}
CDialog::OnMouseMove(nFlags, point);
}
HBITMAP CScreenDlg::CopyScreenToBitmap(LPRECT lpRect) // 将屏幕上的截取区域转化至位图
{
HDC hScrDC, hMemDC;
HBITMAP hOldBitmap,hBitmap;
int nX, nY, nX2, nY2;
int nWidth, nHeight;
int xScrn, yScrn;
if (IsRectEmpty(lpRect))
return NULL;
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
if (nX < 0) nX = 0;
if (nY < 0) nY = 0;
if (nX2 > xScrn) nX2 = xScrn;
if (nY2 > yScrn) nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// 创建一个与屏幕设备描述表兼容的位图
hBitmap=CreateCompatibleBitmap(hScrDC,nWidth,nHeight);
// 把新位图选到内存设备描述表中
hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);
// 把屏幕设备描述表拷贝到内存设备描述表中
BitBlt(hMemDC,0,0, nWidth,nHeight,hScrDC, nX, nY, SRCCOPY);
//得到屏幕位图的句柄
hBitmap=(HBITMAP)SelectObject(hMemDC,hOldBitmap);
//清除
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// 返回位图句柄
return hBitmap;
}
void CScreenDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// ShowWindow(SW_HIDE);
CRect rect;
rect.bottom=(this->endPt.y>this->startPt.y)?this->endPt.y:this->startPt.y;
rect.left=(this->startPt.x<this->endPt.x)?this->startPt.x:this->endPt.x;
rect.right=(this->endPt.x>this->startPt.x)?this->endPt.x:this->startPt.x;
rect.top=(this->startPt.y<this->endPt.y)?this->startPt.y:this->endPt.y;
HBITMAP hBitmap=CopyScreenToBitmap(&rect);
if (OpenClipboard()) //将截取的图像放在剪切板上
{
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
}
ShowWindow(SW_SHOW);
this->EndDialog(0);
HBITMAP handle=(HBITMAP)GetClipboardData(CF_BITMAP); //将剪切板上的位图显示在客户区
CBitmap* bm=CBitmap::FromHandle(hBitmap);
CClientDC cdc(this);
CDC dc;
dc.CreateCompatibleDC(&cdc);
dc.SelectObject(bm);
cdc.BitBlt(0,0,200,200,&dc,0,0,SRCCOPY);
CDialog::OnLButtonDblClk(nFlags, point);
}
本程序最后实现的功能是将截取的图显示在客户区,其实我想做的是图像显示在一个新建的文档里的,但在客户区都无法显示,我不知该怎么办啊? 我想问题可能出在 矩形区域的图像到底被截取没有
剪切板上到底有没有位图
麻烦哪位看一下 我都整了几天了 谢谢各位了!
[解决办法]
hBitmap=CreateCompatibleBitmap(hScrDC,nWidth,nHeight);
这句改为:
hBitmap=CreateCompatibleBitmap(hMemDC,nWidth,nHeight);
[解决办法]
if (!OpenClipboard()) return;
// if nothing
if (!(pbmpInfo = (BITMAPINFO*)GetClipboardData(CF_DIB)))
{
goto exit;
}
//不是CF_BITMAP !!!
[解决办法]
hBitmap=CreateCompatibleBitmap(hScrDC,nWidth,nHeight);
这句改为:
hBitmap=CreateCompatibleBitmap(hMemDC,nWidth,nHeight);
[解决办法]