一、先做透明的EDIT、STATIC,查看参考资料一,下载里面的文件,然后把“EditTrans.h" ”EditTrans.cpp"导入工程,把项目中的需要透明的EDIT改一下就可以了,
类似CEditTrans m_result; //原CEdit m_result 改为CEditTrans m_result就可以透明了。其他用法请查看资料1.
注:如果在vs2010创建项目的时候选择了公共控件清单,会造成edit背景变黑。需要注释掉stdafx.h里面的这段代码
//#ifdef _UNICODE //#if defined _M_IX86 //#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") //#elif defined _M_X64 //#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"") //#else //#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") //#endif //#endif
1.添加对应的4个文件
如果在VS2010下,因为CMemDC的关系,请对源码进行相应的修改。在CMmeDC.h增加命名空间,
namespace ns_gdiBtn { class CMemDC : public CDC { .............................. }; };2.修改代码用到CMemDC的地方改为ns_gdiBtn::CMemDC.
3.添加对应的PNG资源,然后按钮做对应修改,CButton m_btnLogin 改为CGdipButton m_btnLogin;增加下面代码
m_btnLogin.LoadStdImage(IDB_LOGINBTN, _T("PNG")); m_btnLogin.LoadAltImage(IDB_LOGINACTIVE, _T("PNG")); m_btnLogin.EnableToggle(TRUE);
效果图:
备注:因为背景图是png的,而LoadFromResource只能加载BMP图像,所以用函数转换一下。
bool CTransEditDlg::LoadImageFromResource( CImage* pImage, UINT nResID, LPCWSTR lpTyp) { ASSERT(pImage); pImage->Destroy(); HRSRC hrsrc=FindResource(AfxGetResourceHandle(),MAKEINTRESOURCE(nResID),lpTyp); if (hrsrc==NULL) { return false; } HGLOBAL hImageData=LoadResource(AfxGetResourceHandle(),hrsrc); if (hImageData==NULL) { FindResource(AfxGetResourceHandle(),MAKEINTRESOURCE(nResID),lpTyp); return false; } // 锁定内存中的指定资源 LPVOID lpVoid = LockResource(hImageData); LPSTREAM pStream = NULL; DWORD dwSize = SizeofResource(AfxGetResourceHandle(), hrsrc); HGLOBAL hNew = GlobalAlloc(GHND, dwSize); LPBYTE lpByte = (LPBYTE)::GlobalLock(hNew); memcpy(lpByte, lpVoid, dwSize); // 解除内存中的指定资源 GlobalUnlock(hNew); // 从指定内存创建流对象 HRESULT ht = CreateStreamOnHGlobal(hNew, TRUE, &pStream); if ( ht != S_OK ) { GlobalFree(hNew); } else { // 加载图片 pImage->Load(pStream); GlobalFree(hNew); } // 释放资源 FreeResource(hImageData); return true; }
CTransEditDlg::CTransEditDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CTransEditDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); //m_back.Load(_T("res/back.png"));外部加载 //m_back.LoadFromResource(AfxGetInstanceHandle(), IDB_BACK);仅能加载BMP图像 LoadImageFromResource(&m_back, IDB_BACK, _T("PNG"));//内部加载 }
参考资料:
1. (透明EDIT、STATIC控件)http://www.codeproject.com/Articles/1436/CEdit-CStatic-Transparency-Control?fid=2701&fr=1&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick&spc=Relaxed#xx0xx
2.(透明BUTTON)http://www.codeproject.com/Articles/26887/A-user-draw-button-that-supports-PNG-files-with-tr
3.http://blog.sina.com.cn/s/blog_8406420b0100rz3u.html