Windows下如何自定义窗体控件
2011-08-31 23:46 Clingingboy 阅读(2789) 评论(0) 编辑 收藏 举报
Win32窗体的所有控件都认为是窗体,所以创建一个自定义的控件跟创建一个win32的窗体是相似的
参考此篇文章
http://www.codeproject.com/KB/miscctrl/custbutton001.aspx
先通过RegisterClassEx注册一个window class,然后就在消息循环里获取消息绘制控件.
在MFC中自定义控件
依照此思想,在MFC中也是如此做法,让一个控件继承自CWnd,然后通过AfxRegisterClass方法注册,可以参考此篇文章
http://www.codeproject.com/KB/miscctrl/customcontrol.aspx
主要的代码
CBitmapViewer::CBitmapViewer()
{
RegisterWindowClass();
}
CBitmapViewer::~CBitmapViewer()
{
}
// Register the window class if it has not already been registered.
BOOL CBitmapViewer::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();
if (!(::GetClassInfo(hInst, BITMAPVIEWER_CLASSNAME, &wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = BITMAPVIEWER_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CBitmapViewer methods
BOOL CBitmapViewer::Create(CWnd* pParentWnd, const RECT& rect, UINT nID, DWORD dwStyle /*=WS_VISIBLE*/)
{
return CWnd::Create(BITMAPVIEWER_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID);
}
默认windows系统皮肤
win32的控件在不同windows版本下皮肤不一样,默认创建的win32程序和mfc程序的皮肤还是老版本的样式.为了使用新的皮肤必须初始化一个类库
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
把这句话加到头文件中,然后会生产到manifest文件中
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
参考此文
http://www.codeproject.com/KB/cpp/xpstylemfc.aspx
这个配置搞的很郁闷。。。配置。。。