CaptureDlg.cpp
上传用户:aokegd
上传日期:2009-12-14
资源大小:1276k
文件大小:9k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // CaptureDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Capture.h"
  5. #include "CaptureDlg.h"
  6. #include <windowsx.h>
  7. #pragma comment(lib,"hook.lib")
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #define IDM_SHELL WM_USER+1
  14. BOOL __declspec(dllexport)__stdcall  AddHotkey(HWND,UCHAR key,UCHAR mask);
  15. BOOL __declspec(dllexport)__stdcall  DeleteHotkey(HWND,UCHAR key,UCHAR mask);
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CAboutDlg dialog used for App About
  18. UCHAR Key_Table[]={0x78,0x79,0x7a,0x7b,0x6a,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
  19. class CAboutDlg : public CDialog
  20. {
  21. public:
  22. CAboutDlg();
  23. // Dialog Data
  24. //{{AFX_DATA(CAboutDlg)
  25. enum { IDD = IDD_ABOUTBOX };
  26. //}}AFX_DATA
  27. // ClassWizard generated virtual function overrides
  28. //{{AFX_VIRTUAL(CAboutDlg)
  29. protected:
  30. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  31. //}}AFX_VIRTUAL
  32. // Implementation
  33. protected:
  34. //{{AFX_MSG(CAboutDlg)
  35. //}}AFX_MSG
  36. DECLARE_MESSAGE_MAP()
  37. };
  38. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  39. {
  40. //{{AFX_DATA_INIT(CAboutDlg)
  41. //}}AFX_DATA_INIT
  42. }
  43. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  44. {
  45. CDialog::DoDataExchange(pDX);
  46. //{{AFX_DATA_MAP(CAboutDlg)
  47. //}}AFX_DATA_MAP
  48. }
  49. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  50. //{{AFX_MSG_MAP(CAboutDlg)
  51. // No message handlers
  52. //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CCaptureDlg dialog
  56. CCaptureDlg::CCaptureDlg(CWnd* pParent /*=NULL*/)
  57. : CDialog(CCaptureDlg::IDD, pParent)
  58. {
  59. //{{AFX_DATA_INIT(CCaptureDlg)
  60. m_bControl = FALSE;
  61. m_bAlt = FALSE;
  62. m_bShift = FALSE;
  63. m_Path = _T("c:\");
  64. m_Number = _T("0 picture captured.");
  65. nCount=0;
  66. bRegistered=FALSE;
  67. bTray=FALSE;
  68. //}}AFX_DATA_INIT
  69. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  70. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  71. }
  72. void CCaptureDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74. CDialog::DoDataExchange(pDX);
  75. //{{AFX_DATA_MAP(CCaptureDlg)
  76. DDX_Control(pDX, IDC_KEY, m_Key);
  77. DDX_Check(pDX, IDC_CONTROL, m_bControl);
  78. DDX_Check(pDX, IDC_ALT, m_bAlt);
  79. DDX_Check(pDX, IDC_SHIFT, m_bShift);
  80. DDX_Text(pDX, IDC_PATH, m_Path);
  81. DDX_Text(pDX, IDC_NUMBER, m_Number);
  82. //}}AFX_DATA_MAP
  83. }
  84. BEGIN_MESSAGE_MAP(CCaptureDlg, CDialog)
  85. //{{AFX_MSG_MAP(CCaptureDlg)
  86. ON_WM_SYSCOMMAND()
  87. ON_WM_PAINT()
  88. ON_WM_QUERYDRAGICON()
  89. ON_BN_CLICKED(ID_ABOUT, OnAbout)
  90. ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
  91. ON_BN_CLICKED(ID_CHANGE, OnChange)
  92. //}}AFX_MSG_MAP
  93. END_MESSAGE_MAP()
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CCaptureDlg message handlers
  96. BOOL CCaptureDlg::OnInitDialog()
  97. {
  98. CDialog::OnInitDialog();
  99. // Add "About..." menu item to system menu.
  100. // IDM_ABOUTBOX must be in the system command range.
  101. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  102. ASSERT(IDM_ABOUTBOX < 0xF000);
  103. CMenu* pSysMenu = GetSystemMenu(FALSE);
  104. if (pSysMenu != NULL)
  105. {
  106. CString strAboutMenu;
  107. strAboutMenu.LoadString(IDS_ABOUTBOX);
  108. if (!strAboutMenu.IsEmpty())
  109. {
  110. pSysMenu->AppendMenu(MF_SEPARATOR);
  111. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  112. }
  113. }
  114. // Set the icon for this dialog.  The framework does this automatically
  115. //  when the application's main window is not a dialog
  116. SetIcon(m_hIcon, TRUE); // Set big icon
  117. SetIcon(m_hIcon, FALSE); // Set small icon
  118. m_Key.SetCurSel(0);
  119. RegisterHotkey();
  120. CMenu* pMenu=GetSystemMenu(FALSE);
  121. pMenu->DeleteMenu(SC_MAXIMIZE,MF_BYCOMMAND);
  122. pMenu->DeleteMenu(SC_SIZE,MF_BYCOMMAND);
  123. pMenu->DeleteMenu(SC_RESTORE,MF_BYCOMMAND);
  124. return TRUE;  // return TRUE  unless you set the focus to a control
  125. }
  126. void CCaptureDlg::OnSysCommand(UINT nID, LPARAM lParam)
  127. {
  128. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  129. {
  130. CAboutDlg dlgAbout;
  131. dlgAbout.DoModal();
  132. }
  133. else
  134. {
  135. CDialog::OnSysCommand(nID, lParam);
  136. }
  137. }
  138. // If you add a minimize button to your dialog, you will need the code below
  139. //  to draw the icon.  For MFC applications using the document/view model,
  140. //  this is automatically done for you by the framework.
  141. void CCaptureDlg::OnPaint() 
  142. {
  143. if (IsIconic())
  144. {
  145. CPaintDC dc(this); // device context for painting
  146. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  147. // Center icon in client rectangle
  148. int cxIcon = GetSystemMetrics(SM_CXICON);
  149. int cyIcon = GetSystemMetrics(SM_CYICON);
  150. CRect rect;
  151. GetClientRect(&rect);
  152. int x = (rect.Width() - cxIcon + 1) / 2;
  153. int y = (rect.Height() - cyIcon + 1) / 2;
  154. // Draw the icon
  155. dc.DrawIcon(x, y, m_hIcon);
  156. }
  157. else
  158. {
  159. CDialog::OnPaint();
  160. }
  161. }
  162. // The system calls this to obtain the cursor to display while the user drags
  163. //  the minimized window.
  164. HCURSOR CCaptureDlg::OnQueryDragIcon()
  165. {
  166. return (HCURSOR) m_hIcon;
  167. }
  168. void CCaptureDlg::OnCancel() 
  169. {
  170. if(bTray)
  171. DeleteIcon();
  172. CDialog::OnCancel();
  173. }
  174. void CCaptureDlg::OnAbout() 
  175. {
  176. CAboutDlg dlg;
  177. dlg.DoModal();
  178. }
  179. void CCaptureDlg::OnBrowse() 
  180. {
  181. CString str;
  182. BROWSEINFO bi;
  183. char name[MAX_PATH];
  184. ZeroMemory(&bi,sizeof(BROWSEINFO));
  185. bi.hwndOwner=GetSafeHwnd();
  186. bi.pszDisplayName=name;
  187. bi.lpszTitle="Select folder";
  188. bi.ulFlags=BIF_RETURNONLYFSDIRS;
  189. LPITEMIDLIST idl=SHBrowseForFolder(&bi);
  190. if(idl==NULL)
  191. return;
  192. SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
  193. str.ReleaseBuffer();
  194. m_Path=str;
  195. if(str.GetAt(str.GetLength()-1)!='\')
  196. m_Path+="\";
  197. UpdateData(FALSE);
  198. }
  199. void CCaptureDlg::SaveBmp()
  200. {
  201. CDC dc;
  202. dc.CreateDC("DISPLAY",NULL,NULL,NULL);
  203. CBitmap bm;
  204. int Width=GetSystemMetrics(SM_CXSCREEN);
  205. int Height=GetSystemMetrics(SM_CYSCREEN);
  206. bm.CreateCompatibleBitmap(&dc,Width,Height);
  207. CDC tdc;
  208. tdc.CreateCompatibleDC(&dc);
  209. CBitmap*pOld=tdc.SelectObject(&bm);
  210. tdc.BitBlt(0,0,Width,Height,&dc,0,0,SRCCOPY);
  211. tdc.SelectObject(pOld);
  212. BITMAP btm;
  213. bm.GetBitmap(&btm);
  214. DWORD size=btm.bmWidthBytes*btm.bmHeight;
  215. LPSTR lpData=(LPSTR)GlobalAllocPtr(GPTR,size);
  216. /////////////////////////////////////////////
  217. BITMAPINFOHEADER bih;
  218. bih.biBitCount=btm.bmBitsPixel;
  219. bih.biClrImportant=0;
  220. bih.biClrUsed=0;
  221. bih.biCompression=0;
  222. bih.biHeight=btm.bmHeight;
  223. bih.biPlanes=1;
  224. bih.biSize=sizeof(BITMAPINFOHEADER);
  225. bih.biSizeImage=size;
  226. bih.biWidth=btm.bmWidth;
  227. bih.biXPelsPerMeter=0;
  228. bih.biYPelsPerMeter=0;
  229. ///////////////////////////////////
  230. GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
  231. // bm.GetBitmapBits(size,lpData); //此函数在处理5-5-5模式的16位色下会出现颜色混乱
  232. //////////////////////////////
  233. static int filecount=0;
  234. CString name;
  235. name.Format("pict%04d.bmp",filecount++);
  236. name=m_Path+name;
  237. BITMAPFILEHEADER bfh;
  238. bfh.bfReserved1=bfh.bfReserved2=0;
  239. bfh.bfType=((WORD)('M'<< 8)|'B');
  240. bfh.bfSize=54+size;
  241. bfh.bfOffBits=54;
  242. CFile bf;
  243. if(bf.Open(name,CFile::modeCreate|CFile::modeWrite)){
  244. bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
  245. bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
  246. bf.WriteHuge(lpData,size);
  247. bf.Close();
  248. nCount++;
  249. }
  250. GlobalFreePtr(lpData);
  251. if(nCount==1)
  252. m_Number.Format("%d picture captured.",nCount);
  253. else
  254. m_Number.Format("%d pictures captured.",nCount);
  255. UpdateData(FALSE);
  256. }
  257. BOOL CCaptureDlg::PreTranslateMessage(MSG* pMsg) 
  258. {
  259.     if(pMsg -> message == WM_KEYDOWN)
  260. {
  261.         if(pMsg -> wParam == VK_ESCAPE)
  262. return TRUE;
  263. if(pMsg -> wParam == VK_RETURN)
  264. return TRUE;
  265. }
  266. return CDialog::PreTranslateMessage(pMsg);
  267. }
  268. LRESULT CCaptureDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  269. {
  270. if(message==WM_HOTKEY&&lParam==WM_KEYDOWN){
  271. SaveBmp();
  272. return FALSE;
  273. }
  274. if(message==IDM_SHELL&&lParam==WM_RBUTTONUP){
  275. CMenu pop;
  276. pop.LoadMenu(MAKEINTRESOURCE(IDR_MENU1));
  277. CMenu*pMenu=pop.GetSubMenu(0);
  278. pMenu->SetDefaultItem(ID_EXITICON);
  279. CPoint pt;
  280. GetCursorPos(&pt);
  281. int id=pMenu->TrackPopupMenu(TPM_RIGHTALIGN|TPM_NONOTIFY|TPM_RETURNCMD|TPM_LEFTBUTTON,
  282. pt.x,pt.y,this);
  283. if(id==ID_EXITICON)
  284. DeleteIcon();
  285. else if(id==ID_EXIT)
  286. OnCancel();
  287. return FALSE;
  288. }
  289. LRESULT res= CDialog::WindowProc(message, wParam, lParam);
  290. if(message==WM_SYSCOMMAND&&wParam==SC_MINIMIZE)
  291. AddIcon();
  292. return res;
  293. }
  294. void CCaptureDlg::AddIcon()
  295. {
  296. NOTIFYICONDATA data;
  297. data.cbSize=sizeof(NOTIFYICONDATA);
  298. CString tip;
  299. tip.LoadString(IDS_ICONTIP);
  300. data.hIcon=GetIcon(0);
  301. data.hWnd=GetSafeHwnd();
  302. strcpy(data.szTip,tip);
  303. data.uCallbackMessage=IDM_SHELL;
  304. data.uFlags=NIF_ICON|NIF_MESSAGE |NIF_TIP ;
  305. data.uID=98;
  306. Shell_NotifyIcon(NIM_ADD,&data);
  307. ShowWindow(SW_HIDE);
  308. bTray=TRUE;
  309. }
  310. void CCaptureDlg::DeleteIcon()
  311. {
  312. NOTIFYICONDATA data;
  313. data.cbSize=sizeof(NOTIFYICONDATA);
  314. data.hWnd=GetSafeHwnd();
  315. data.uID=98;
  316. Shell_NotifyIcon(NIM_DELETE,&data);
  317. ShowWindow(SW_SHOW);
  318. SetForegroundWindow();
  319. ShowWindow(SW_SHOWNORMAL);
  320. bTray=FALSE;
  321. }
  322. void CCaptureDlg::OnChange() 
  323. {
  324. RegisterHotkey();
  325. }
  326. BOOL CCaptureDlg::RegisterHotkey()
  327. {
  328. UpdateData();
  329. UCHAR mask=0;
  330. UCHAR key=0;
  331. if(m_bControl)
  332. mask|=4;
  333. if(m_bAlt)
  334. mask|=2;
  335. if(m_bShift)
  336. mask|=1;
  337. key=Key_Table[m_Key.GetCurSel()];
  338. if(bRegistered){
  339. DeleteHotkey(GetSafeHwnd(),cKey,cMask);
  340. bRegistered=FALSE;
  341. }
  342. cMask=mask;
  343. cKey=key;
  344. bRegistered=AddHotkey(GetSafeHwnd(),cKey,cMask);
  345. return bRegistered;
  346. }