vc編寫仿qq截圖程式

來源:互聯網
上載者:User

     一直想研究一些程式是如何,今天閑下來看了下資料,搗鼓了一下

// photoDlg.cpp : implementation file<br />//</p><p>#include "stdafx.h"<br />#include "photo.h"<br />#include "photoDlg.h"<br />#include <ATLCONV.h></p><p>#ifdef _DEBUG<br />#define new DEBUG_NEW<br />#undef THIS_FILE<br />static char THIS_FILE[] = __FILE__;<br />#endif</p><p>/////////////////////////////////////////////////////////////////////////////<br />// CAboutDlg dialog used for App About</p><p>class CAboutDlg : public CDialog<br />{<br />public:<br />CAboutDlg();</p><p>// Dialog Data<br />//{{AFX_DATA(CAboutDlg)<br />enum { IDD = IDD_ABOUTBOX };<br />//}}AFX_DATA</p><p>// ClassWizard generated virtual function overrides<br />//{{AFX_VIRTUAL(CAboutDlg)<br />protected:<br />virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support<br />//}}AFX_VIRTUAL</p><p>// Implementation<br />protected:<br />//{{AFX_MSG(CAboutDlg)<br />//}}AFX_MSG<br />DECLARE_MESSAGE_MAP()<br />};</p><p>CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)<br />{<br />//{{AFX_DATA_INIT(CAboutDlg)<br />//}}AFX_DATA_INIT<br />}</p><p>void CAboutDlg::DoDataExchange(CDataExchange* pDX)<br />{<br />CDialog::DoDataExchange(pDX);<br />//{{AFX_DATA_MAP(CAboutDlg)<br />//}}AFX_DATA_MAP<br />}</p><p>BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)<br />//{{AFX_MSG_MAP(CAboutDlg)<br />// No message handlers<br />//}}AFX_MSG_MAP<br />END_MESSAGE_MAP()</p><p>/////////////////////////////////////////////////////////////////////////////<br />// CPhotoDlg dialog</p><p>CPhotoDlg::CPhotoDlg(CWnd* pParent /*=NULL*/)<br />: CDialog(CPhotoDlg::IDD, pParent)<br />{<br />//{{AFX_DATA_INIT(CPhotoDlg)<br />// NOTE: the ClassWizard will add member initialization here<br />//}}AFX_DATA_INIT<br />// Note that LoadIcon does not require a subsequent DestroyIcon in Win32<br />m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);<br />}</p><p>void CPhotoDlg::DoDataExchange(CDataExchange* pDX)<br />{<br />CDialog::DoDataExchange(pDX);<br />//{{AFX_DATA_MAP(CPhotoDlg)<br />// NOTE: the ClassWizard will add DDX and DDV calls here<br />//}}AFX_DATA_MAP<br />}</p><p>BEGIN_MESSAGE_MAP(CPhotoDlg, CDialog)<br />//{{AFX_MSG_MAP(CPhotoDlg)<br />ON_WM_SYSCOMMAND()<br />ON_WM_PAINT()<br />ON_WM_QUERYDRAGICON()<br />ON_WM_LBUTTONDOWN()<br />ON_WM_MOUSEMOVE()<br />ON_WM_LBUTTONUP()<br />ON_WM_LBUTTONDBLCLK()<br />//}}AFX_MSG_MAP<br />END_MESSAGE_MAP()</p><p>/////////////////////////////////////////////////////////////////////////////<br />// CPhotoDlg message handlers</p><p>BOOL CPhotoDlg::OnInitDialog()<br />{<br />CDialog::OnInitDialog();</p><p>// Add "About..." menu item to system menu.</p><p>// IDM_ABOUTBOX must be in the system command range.<br />ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);<br />ASSERT(IDM_ABOUTBOX < 0xF000);</p><p>CMenu* pSysMenu = GetSystemMenu(FALSE);<br />if (pSysMenu != NULL)<br />{<br />CString strAboutMenu;<br />strAboutMenu.LoadString(IDS_ABOUTBOX);<br />if (!strAboutMenu.IsEmpty())<br />{<br />pSysMenu->AppendMenu(MF_SEPARATOR);<br />pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);<br />}<br />}</p><p>// Set the icon for this dialog. The framework does this automatically<br />// when the application's main window is not a dialog<br />SetIcon(m_hIcon, TRUE);// Set big icon<br />SetIcon(m_hIcon, FALSE);// Set small icon<br />isdown=0;<br />complete=0;<br />int w=GetSystemMetrics(SM_CXSCREEN);<br />int h=GetSystemMetrics(SM_CYSCREEN);<br />SetWindowPos(&wndTopMost,0,0,w,h,SWP_SHOWWINDOW);<br />SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);<br />HINSTANCE hInst = LoadLibrary("User32.DLL");<br />if(hInst)<br />{<br />typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);<br />MYFUNC fun = NULL;<br />//取得SetLayeredWindowAttributes函數指標<br />fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");<br />if(fun)fun(this->GetSafeHwnd(),0,100,2);<br />FreeLibrary(hInst);<br />}<br />return TRUE; // return TRUE unless you set the focus to a control<br />}</p><p>void CPhotoDlg::OnSysCommand(UINT nID, LPARAM lParam)<br />{<br />if ((nID & 0xFFF0) == IDM_ABOUTBOX)<br />{<br />CAboutDlg dlgAbout;<br />dlgAbout.DoModal();<br />}<br />else<br />{<br />CDialog::OnSysCommand(nID, lParam);<br />}<br />}</p><p>// If you add a minimize button to your dialog, you will need the code below<br />// to draw the icon. For MFC applications using the document/view model,<br />// this is automatically done for you by the framework.</p><p>void CPhotoDlg::OnPaint()<br />{<br />if (IsIconic())<br />{<br />CPaintDC dc(this); // device context for painting</p><p>SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);</p><p>// Center icon in client rectangle<br />int cxIcon = GetSystemMetrics(SM_CXICON);<br />int cyIcon = GetSystemMetrics(SM_CYICON);<br />CRect rect;<br />GetClientRect(&rect);<br />int x = (rect.Width() - cxIcon + 1) / 2;<br />int y = (rect.Height() - cyIcon + 1) / 2;</p><p>// Draw the icon<br />dc.DrawIcon(x, y, m_hIcon);<br />}<br />else<br />{<br />CDialog::OnPaint();<br />}<br />}</p><p>// The system calls this to obtain the cursor to display while the user drags<br />// the minimized window.<br />HCURSOR CPhotoDlg::OnQueryDragIcon()<br />{<br />return (HCURSOR) m_hIcon;<br />}</p><p>void CPhotoDlg::OnLButtonDown(UINT nFlags, CPoint point)<br />{<br />complete=0;<br />isdown=1;<br />p=point;<br />CDialog::OnLButtonDown(nFlags, point);<br />}</p><p>void CPhotoDlg::OnMouseMove(UINT nFlags, CPoint point)<br />{<br />if(isdown){</p><p>CRect r(p,point);<br />RedrawWindow(0,0,RDW_INTERNALPAINT| RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);</p><p>CDC *dc=GetDC();<br />dc->MoveTo(r.left,r.top);<br />dc->LineTo(r.right,r.top);<br />dc->LineTo(r.right,r.bottom);<br />dc->LineTo(r.left,r.bottom);<br />dc->LineTo(r.left,r.top);<br />dc->DeleteDC();<br />rr=r;<br />}<br />CDialog::OnMouseMove(nFlags, point);<br />}</p><p>void CPhotoDlg::OnLButtonUp(UINT nFlags, CPoint point)<br />{<br />if(isdown){<br />complete=1;<br />isdown=0;</p><p>}</p><p>CDialog::OnLButtonUp(nFlags, point);<br />}<br />int CPhotoDlg::GetCodecClsid(const WCHAR* format, CLSID* pClsid)<br />{<br />UINT num = 0; // number of image encoders<br />UINT size = 0; // size of the image encoder array in bytes</p><p>ImageCodecInfo* pImageCodecInfo = NULL;</p><p>GetImageEncodersSize(&num, &size);<br />if(size == 0)<br />return -1; // Failure</p><p>pImageCodecInfo = (ImageCodecInfo*)(malloc(size));<br />if(pImageCodecInfo == NULL)<br />return -1; // Failure</p><p>GetImageEncoders(num, size, pImageCodecInfo);</p><p>for(UINT j = 0; j < num; ++j)<br />{<br />if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )<br />{<br />*pClsid = pImageCodecInfo[j].Clsid;<br />return j; // Success<br />}<br />} // for</p><p>return -1; // Failure<br />}</p><p>void CPhotoDlg::OnLButtonDblClk(UINT nFlags, CPoint point)<br />{</p><p>if(complete){<br />CRect r=rr;<br />ClientToScreen(&r);<br />CFileDialog d(false, "jpg(*.jpg)|*.jpg", NULL, </p><p>OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, </p><p>"jpg(*.jpg)|*.jpg||", this);<br />if(d.DoModal()==IDOK){<br />Sleep(100);<br />RedrawWindow(0,0,RDW_INTERNALPAINT| RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);<br />ShowWindow(0);<br />CDC dc;<br />dc.CreateDC("DISPLAY",NULL,NULL,NULL);<br />ClientToScreen(&rr);<br />CDC dc1;<br />dc1.CreateCompatibleDC(&dc);<br />CBitmap b;<br />b.CreateCompatibleBitmap(&dc,rr.Width(),rr.Height());<br />CBitmap *old=(CBitmap*)dc1.SelectObject(&b);<br />BitBlt(dc1.m_hDC,0,0,rr.Width(),rr.Height(),dc.m_hDC,rr.left,rr.top,SRCCOPY|0x40000000);<br />ShowWindow(1);<br />dc1.SelectObject(old);<br />dc.DeleteDC();<br />Image *i;<br />i=Bitmap::FromHBITMAP((HBITMAP)b.m_hObject,0);<br />CLSID gifcodec;<br />GetCodecClsid(L"image/jpeg", &gifcodec);<br />CString path =d.GetPathName();</p><p>wchar_t *wstr = path.AllocSysString();</p><p>i->Save(wstr,&gifcodec, 0);<br />delete i;<br />AfxMessageBox("complete");<br />}</p><p>}<br />CDialog::OnLButtonDblClk(nFlags, point);<br />}<br />

     程式是mfc的,可以截取帶有透明屬性的表單,按enter或esc退出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.