【MFC】一个简单的截图工具源码

 一个简单的截图工具源码,供初学者参考学习
OnInitDialog()
{
	// 在此添加额外的初始化代码
	IsDownBtn=0;  
	complete=0;  
	int w=GetSystemMetrics(SM_CXSCREEN);  
	int h=GetSystemMetrics(SM_CYSCREEN);  
	SetWindowPos(&wndTopMost,0,0,w,h,SWP_SHOWWINDOW);  
	SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);  
	HINSTANCE hInst = LoadLibrary(L"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);  
	}

}

//截图,添加一个按钮
void CphotoDlg::OnBnClickedButton()
{
	// TODO: 在此添加控件通知处理程序代码
	try
	{
	if(complete)
	{ 
		CImage *m_Image;
		CRect r=rr;  
		ClientToScreen(&r);  
		CFileDialog d(false,L"jpg(*.jpg)|*.jpg",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,L"jpg(*.jpg)|*.jpg||",this);  
		if(d.DoModal()==IDOK)
		{  
			Sleep(100);  
			//RedrawWindow(0,0,RDW_INTERNALPAINT| RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);  
			ShowWindow(0);  
			CDC dc;  
			dc.CreateDC(L"DISPLAY",NULL,NULL,NULL);  
			ClientToScreen(&r);  
			CDC dc1;  
			dc1.CreateCompatibleDC(&dc);  
			CBitmap b;  
			b.CreateCompatibleBitmap(&dc,r.Width(),r.Height());  
			CBitmap *old=(CBitmap*)dc1.SelectObject(&b);  
			dc1.BitBlt(0,0,r.Width(),r.Height(),&dc,r.left,r.top,SRCCOPY|0x40000000);  
			ShowWindow(1);  
			dc1.SelectObject(old); 
			dc.DeleteDC(); 	

			m_Image = (CImage*)CBitmap::FromHandle((HBITMAP)b.m_hObject);
			HRESULT hResult;
			// 图像保存
			hResult = m_Image->Save(d.GetPathName());
		 
			if (FAILED(hResult)) 
				MessageBox(L"保存图像文件失败!");
			else
			    AfxMessageBox(L"保存完成");  
		}  

	}
	}
	catch(_com_error e)
	{
		AfxMessageBox(L"保存失败");
	}


	
}

void CphotoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	
    IsDownBtn=TRUE; 
	Sleep(1500);
	complete=0;   
	ppp=point;  
	CDialog::OnLButtonDown(nFlags, point);
}
//鼠标移动处理
void CphotoDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if(IsDownBtn)
	{  
		CRect r(ppp,point);  
		//RedrawWindow(0,0,RDW_INTERNALPAINT|   RDW_INVALIDATE   |   RDW_UPDATENOW   |   RDW_ERASE);  

		CDC *dc=GetDC();  
		dc->MoveTo(r.left,r.top);  
		dc->LineTo(r.right,r.top);  
		dc->LineTo(r.right,r.bottom);  
		dc->LineTo(r.left,r.bottom);  
		dc->LineTo(r.left,r.top);  
		dc->DeleteDC();  
		rr=r;  
	}  

	CDialog::OnMouseMove(nFlags, point);
}

void CphotoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if(IsDownBtn)
	{  
		complete=1;  
		IsDownBtn=FALSE;  
		
	}

	CDialog::OnLButtonUp(nFlags, point);
}


你可能感兴趣的:(【MFC】一个简单的截图工具源码)