风言枫语  
CBtn::CBtn()
{
	RegisterWndClass();
}
bool CBtn::RegisterWndClass(void)
{
	WNDCLASS n;
	HINSTANCE hInst=AfxGetInstanceHandle();
	if(GetClassInfo(hInst,"MyBtn",&n)==0)return true;//已经存在
	n.style=CS_DBLCLKS;
	n.cbClsExtra=0;
	n.cbWndExtra=0;

	n.hIcon=NULL;
	n.hCursor=AfxGetApp()->LoadStandardCursor(IDC_ARROW);
	n.hInstance=hInst;

	n.lpszClassName="MyBtn";
	n.lpszMenuName=NULL;
	n.lpfnWndProc=::DefWindowProcA;
	if(AfxRegisterClass(&n))
	{
		AfxThrowResourceException();
		return false;
	}
	return true;
}


void CBtn::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: 在此处添加消息处理程序代码
	// 不为绘图消息调用 CWnd::OnPaint()
	DrawBtn(IDR_JPG1,"JPG");
}


void CBtn::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	DrawBtn(IDB_PNG1,"PNG");
	CWnd::OnLButtonDown(nFlags, point);
}


void CBtn::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	DrawBtn(IDR_JPG1,"JPG");
	int id=22222;////给父窗口发送项被单击消息
	GetParent()->SendMessage(WM_COMMAND,MAKEWPARAM(id,0),LPARAM(this->GetSafeHwnd()));
	CWnd::OnLButtonUp(nFlags, point);
}


BOOL CBtn::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	SetCursor(AfxGetApp()->LoadCursorA(IDC_CURSOR1));
	return true;//CWnd::OnSetCursor(pWnd, nHitTest, message);
}

#include<atlimage.h>
bool CBtn::DrawBtn(int id, CString file)
{
	CImage img;
	HRSRC src=::FindResourceA(::AfxGetResourceHandle(),MAKEINTRESOURCE(id),file);
	if(src==NULL)return false;
	HGLOBAL data=::LoadResource(::AfxGetResourceHandle(),src);// 加载资源
	if(data==NULL){::FreeResource(data);return false;}
	LPVOID lp=::LockResource(data);// 锁定内存中的指定资源
	LPSTREAM is;
	DWORD size=::SizeofResource(::AfxGetResourceHandle(),src);
	HGLOBAL hg=::GlobalAlloc(GHND,size);
	LPBYTE by=(LPBYTE)::GlobalLock(hg);
	::memcpy(by,lp,size);
	::GlobalUnlock(hg);// 解除内存中的指定资源
	HRESULT res=::CreateStreamOnHGlobal(hg,true,&is);// 从指定内存创建流对象
	if(res!=S_OK)::GlobalFree(hg);
	else {img.Load(is);::GlobalFree(hg);}
	::FreeResource(data);// 释放资源
	COLORREF ref=RGB(0,0,0);
if(img.GetBPP()==32)//确认该图像包含Alpha通道
{
	for(int i=0;i<img.GetWidth();i++)
		for(int j=0;j<img.GetHeight();j++)
		{
			LPBYTE by=(LPBYTE)img.GetPixelAddress(i,j);
			by[0]=by[0]*by[3]/255;
			by[1]=by[1]*by[3]/255;
			by[2]=by[2]*by[3]/255;
		}
		ref=img.GetPixel(0,0);
}
   CClientDC dc(this);
   dc.SetStretchBltMode(3);//4
   ::SetBrushOrgEx(dc,0,0,NULL);
   CRect rc;GetClientRect(&rc);
   img.SetTransparentColor(ref);
   img.Draw(dc.m_hDC,0,0,rc.Width(),rc.Height());

   CString s="按钮";
   CFont *font=dc.SelectObject(CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT)));
   dc.SetTextColor(RGB(255,0,0));
   dc.SetBkMode(TRANSPARENT);
   dc.DrawText(s,s.GetLength(),rc,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
   return true;
}
void CMyBtnDlg::OnBnClickedButton1()
{
	b.Create(NULL,NULL,WS_VISIBLE|WS_CHILD,CRect(0,0,100,100),this,NULL,NULL);
}

#include<atlimage.h>
void CMyBtnDlg::OnMyBtn(void)
{
	//AfxMessageBox("我的按钮事件");
	CWnd *pwnd=CWnd::GetDesktopWindow();//屏幕
	CWindowDC dc(pwnd);
	CRect rc;
	pwnd->GetWindowRect(&rc);
	int pix=dc.GetDeviceCaps(BITSPIXEL)*dc.GetDeviceCaps(PLANES);
	CImage img;
	img.Create(rc.Width(),rc.Height(),pix);
	CImageDC mdc(img);
	::BitBlt(mdc,0,0,rc.Width(),rc.Height(),dc,0,0,SRCCOPY);
	img.Save("C:\\j.jpg");
}


posted on 2013-08-29 19:00  风言枫语  阅读(262)  评论(0编辑  收藏  举报