手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>C/C++>列表

用Visual C 设计屏幕抓图程序

来源:互联网 作者:西部数码 时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

}

void CCaptureDlg::OnCancel()
{
 if(bTray)
  DeleteIcon();
  CDialog::OnCancel();
}

void CCaptureDlg::OnAbout()
{
 CAboutDlg dlg;
 dlg.DoModal();
}

void CCaptureDlg::OnBrowse()
{
 CString str;
 BROWSEINFO bi;
 char name[MAX_PATH];
 ZeroMemory(&bi,sizeof(BROWSEINFO));
 bi.hwndOwner=GetSafeHwnd();
 bi.pszDisplayName=name;
 bi.lpszTitle="Select folder";
 bi.ulFlags=BIF_RETURNONLYFSDIRS;
 LPITEMIDLIST idl=SHBrowseForFolder(&bi);
 if(idl==NULL)
  return;
 SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
 str.ReleaseBuffer();
 m_Path=str;
 if(str.GetAt(str.GetLength()-1)!='')
  m_Path ="";
 UpdateData(FALSE);
}

void CCaptureDlg::SaveBmp()
{
 CDC dc;
 dc.CreateDC("DISPLAY",NULL,NULL,NULL);
 CBitmap bm;
 int Width=GetSystemMetrics(SM_CXSCREEN);
 int Height=GetSystemMetrics(SM_CYSCREEN);
 bm.CreateCompatibleBitmap(&dc,Width,Height);
 CDC tdc;
 tdc.CreateCompatibleDC(&dc);
 CBitmap*pOld=tdc.SelectObject(&bm);
 tdc.BitBlt(0,0,Width,Height,&dc,0,0,SRCCOPY);
 tdc.SelectObject(pOld);
 BITMAP btm;
 bm.GetBitmap(&btm);
 DWORD size=btm.bmWidthBytes*btm.bmHeight;
 LPSTR lpData=(LPSTR)GlobalAllocPtr(GPTR,size);
 BITMAPINFOHEADER bih;
 bih.biBitCount=btm.bmBitsPixel;
 bih.biClrImportant=0;
 bih.biClrUsed=0;
 bih.biCompression=0;
 bih.biHeight=btm.bmHeight;
 bih.biPlanes=1;
 bih.biSize=sizeof(BITMAPINFOHEADER);
 bih.biSizeImage=size;
 bih.biWidth=btm.bmWidth;
 bih.biXPelsPerMeter=0;
 bih.biYPelsPerMeter=0;
 GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
 static int filecount=0;
 CString name;
 name.Format("pictd.bmp",filecount );
 name=m_Path name;
 BITMAPFILEHEADER bfh;
 bfh.bfReserved1=bfh.bfReserved2=0;
 bfh.bfType=((WORD)('M'<< 8)|'B');
 bfh.bfSize=54 size;
 bfh.bfOffBits=54;
 CFile bf;
 if(bf.Open(name,CFile::modeCreate|CFile::modeWrite)){
  bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
  bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
  bf.WriteHuge(lpData,size);
  bf.Close();
  nCount ;
 }
 GlobalFreePtr(lpData);
 if(nCount==1)
  m_Number.Format("%d picture captured.",nCount);
 else
  m_Number.Format("%d pictures captured.",nCount);
 UpdateData(FALSE);
}

BOOL CCaptureDlg::PreTranslateMessage(MSG* pMsg)
{
 if(pMsg -> message == WM_KEYDOWN)
 {
  if(pMsg -> wParam == VK_ESCAPE)
   return TRUE;
  if(pMsg -> wParam == VK_RETURN)
   return TRUE;
 }
 return CDialog::PreTranslateMessage(pMsg);
}

LRESULT CCaptureDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
 if(message==WM_HOTKEY&&lParam==WM_KEYDOWN){
  SaveBmp();
 return FALSE;
}
if(message==IDM_SHELL&&lParam==WM_RBUTTONUP){
 CMenu pop;
 pop.LoadMenu(MAKEINTRESOURCE(IDR_MENU1));
 CMenu*pMenu=pop.GetSubMenu(0);
 pMenu->SetDefaultItem(ID_EXITICON);
 CPoint pt;
 GetCursorPos(&pt);
 int id=pMenu->TrackPopupMenu(TPM_RIGHTALIGN|TPM_NONOTIFY|TPM_RETURNCMD|TPM_LEFTBUTTON,pt.x,pt.y,this);
 if(id==ID_EXITICON)
  DeleteIcon();
 else if(id==ID_EXIT)
  OnCancel();
 return FALSE;
}
LRESULT res= CDialog::WindowProc(message, wParam, lParam);
if(message==WM_SYSCOMMAND&&wParam==SC_MINIMIZE)
AddIcon();
return res;
}

void CCaptureDlg::AddIcon()
{
 NOTIFYICONDATA data;
 data.cbSize=sizeof(NOTIFYICONDATA);
 CString tip;
 tip.LoadString(IDS_ICONTIP);
 data.hIcon=GetIcon(0);
 data.hWnd=GetSafeHwnd();
 strcpy(data.szTip,tip);
 data.uCallbackMessage=IDM_SHELL;
 data.uFlags=NIF_ICON|NIF_MESSAGE |NIF_TIP ;
 data.uID=98;
 Shell_NotifyIcon(NIM_ADD,&data);
 ShowWindow(SW_HIDE);
 bTray=TRUE;
}

void CCaptureDlg::DeleteIcon()
{
 NOTIFYICONDATA data;
 data.cbSize=sizeof(NOTIFYICONDATA);
 data.hWnd=GetSafeHwnd();
 data.uID=98;
 Shell_NotifyIcon(NIM_DELETE,&data);
 ShowWindow(SW_SHOW);
 SetForegroundWindow();
 ShowWindow(SW_SHOWNORMAL);
 bTray=FALSE;
}

void CCaptureDlg::OnChange()
{
 RegisterHotkey();
}

BOOL CCaptureDlg::RegisterHotkey()
{
 UpdateData();
 UCHAR mask=0;
 UCHAR key=0;
 if(m_bControl)
  mask|=4;
 if(m_bAlt)
  mask|=2;
 if(m_bShift)
  mask|=1;
 key=Key_Table[m_Key.GetCurSel()];
 if(bRegistered){
  DeleteHotkey(GetSafeHwnd(),cKey,cMask);
  bRegistered=FALSE;
 }
 cMask=mask;
 cKey=key;
 bRegistered=AddHotkey(GetSafeHwnd(),cKey,cMask);
 return bRegistered;
}
  四、小结

  本实例通过一个钩子DLL实现了屏幕抓图的功能,它有一个缺陷,那就是只能保存整个屏幕图像,不过读者朋友可以在此基础上,实现屏幕任意区域的抓图功能,具体方法不再赘述,实现起来很容易的了,呵呵。

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!