Win32控件的分类
这里先不讨论ImageList等控件组件、对话框等,只讨论有类型名称的Win32控件。
可以从下边的列表看出来,大部分控件的宏定义名称都是以WC_开头的,另外有8个是以_CLASS结尾的,工具栏、状态栏、ReBar则是以CLASSNAME结尾。
这些控件所需要的加载代码是不同的:
- 预定义控件是user32.dll定义的,不需要加载其它DLL
- 富文本框需要加载riched32.dll,riched20.dll,msftedit.dll,其中msftedit.dll需要WinXPSP1+
- 对于WinXP以前的系统,comctl32.dll最高版本为v5.81,加载comctl32.dll之后,系统会自动使用0x00FF作参数运行InitCommonControlsEx函数注册所有Win95控件
- 对于WinXP及以后的系统,comctl32.dll默认使用v5.82,系统会自动使用0x3FFF作参数运行InitCommonControlsEx函数注册所有可用控件
- IE3.0通用控件需要加载comctl32.dll,并使用0x0100,0x0200,0x0400作参数运行InitCommonControlsEx函数注册控件
- Win98/IE4.0通用控件需要加载comctl32.dll,并使用0x0800,0x1000,0x2000作参数运行InitCommonControlsEx函数注册控件
- WinXP/WinVista通用控件需要加载comctl32.dll v6.0,系统会自动使用0xFFFF作参数运行InitCommonControlsEx函数注册控件
- 同时,多次运行InitCommonControlsEx函数注册控件的效果是累积的
- WinVista网络地址控件需要加载shell32.dll,并运行InitNetworkAddressControl()函数注册控件
除此之外,预定义控件大多使用WM_COMMAND等比较老的通知消息;而通用控件大多使用WM_NOTIFY等比较新的通知消息,富文本框则两者都有。但是也并不总是这样的,比如加载comctl32.dll v6.0以后,部分预定义控件也会发出WM_NOTIFY消息。
预定义控件(user32.dll):
WC_BUTTON "BUTTON" // Button,CheckBox,RadioButton,GroupBox
WC_COMBOBOX "COMBOBOX"
WC_EDIT "EDIT"
WC_LISTBOX "LISTBOX"
WC_SCROLLBAR "SCROLLBAR" // Horizontal,Vertical
WC_STATIC "STATIC" // StaticText,PictureControl
富文本框(richedit.dll、riched20.dll、msftedit.dll):
RICHEDIT_CLASS10A "RICHEDIT" // richedit.dll
RICHEDIT_CLASS(A/W) "RichEdit20A/W" // riched20.dll
MSFTEDIT_CLASS "RICHEDIT50W" // msftedit.dll (WinXPSP1+)
Win95通用控件(comctl32.dll v4.0):
WC_LISTVIEW "SysListView32" // 0x0001
WC_HEADER "SysHeader32" // 0x0001
WC_TREEVIEW "SysTreeView32" // 0x0002
WC_TABCONTROL "SysTabControl32" // 0x0008
TOOLBARCLASSNAME "ToolbarWindow32" // 0x0004
STATUSCLASSNAME "msctls_statusbar32 // 0x0004
TRACKBAR_CLASS "msctls_trackbar32" // 0x0004
UPDOWN_CLASS "msctls_updown32" // 0x0010
PROGRESS_CLASS "msctls_progress32" // 0x0020
HOTKEY_CLASS "msctls_hotkey32" // 0x0040
ANIMATE_CLASS "SysAnimate32" // 0x0080
TOOLTIPS_CLASS "tooltips_class32" // 0x0002,0x0004,0x0008
IE3.0通用控件(comctl32.dll v4.70):
WC_COMBOBOXEX "ComboBoxEx32" // 0x0200
REBARCLASSNAME "ReBarWindow32" // 0x0400
MONTHCAL_CLASS "SysMonthCal32" // 0x0100
DATETIMEPICK_CLASS "SysDateTimePick32" // 0x0100
Win98/IE4.0通用控件(comctl32.dll v4.71,v4.72,v5.80,v5.81,v5.82):
WC_IPADDRESS "SysIPAddress32" // 0x0800
WC_PAGESCROLLER "SysPager" // 0x1000
WC_NATIVEFONTCTL "NativeFontCtl" // 0x2000 (这个控件好像没有官方介绍)
WinXP/Vista通用控件(comctl32.dll v6.0):
(重新注册预定义控件) // 0x4000 (comctl32.dll v6.0, WinXP+, 自动)
WC_LINK "SysLink" // 0x8000 (comctl32.dll v6.0, WinXP+, 自动)
WC_BUTTON "BUTTON" // SplitButton,CommandLink (comctl32.dll v6.0, WinVista+, 自动)
WinVista网络地址控件(shell32.dll):
WC_NETADDRESS "msctls_netaddress" // shell32.dll (WinVista+)
comctl32.dll用5.82还是6.0
现在的系统上通用控件库有两个版本,comctl32 v5.82和comctl32 v6.0。两者的区别主要是以下几点:
- 默认会加载5.82版,6.0版需要使用manifest手动指定
- 5.82使用标准GDI绘制,而6.0使用uxtheme.dll绘制,在新系统上比较好看
- 窗口回调替换:5.82支持ANSI/Unicode窗口回调,6.0只支持Unicode窗口回调
- 6.0多了一个SysLink控件
- 6.0给BUTTON等预定义控件增加了一些WM_NOTIFY通知
- Windows Vista以上版本,6.0多了一个Task Dialogs和BS_[DEF]SPLITBUTTON、BS_[DEF]COMMANDLINK两个Button Styles
5.82已经包含了绝大多数程序逻辑所必需的功能。6.0风格经常跟着系统变,还需要manifest,用起来确实麻烦一些。
如果比较在意程序风格的统一性和兼容性,建议使用5.82;如果比较在意程序的美观性,也可以使用6.0。
完整集成所有标准控件的代码:
#include <windows.h>
#include <tchar.h>
#include <windowsx.h>
#include <commctrl.h>
#include <commdlg.h>
#pragma comment(lib, "comctl32.lib")
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, _TCHAR *szCmdLine, int nShowCmd)
{
// comctl32.dll v6.0 if exists
#ifdef USE_COMCTL32_V6
#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_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' 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
// comctl32.dll v4.70-5.81 compatible
HMODULE hcomctl32 = LoadLibrary(TEXT("comctl32.dll"));
INITCOMMONCONTROLSEX icc = { sizeof icc };
typedef BOOL(WINAPI*TInitCommonControlsEx)(LPINITCOMMONCONTROLSEX);
TInitCommonControlsEx pInitCommonControlsEx = NULL;
if (hcomctl32)
pInitCommonControlsEx = (TInitCommonControlsEx)GetProcAddress(hcomctl32, "InitCommonControlsEx");
if (pInitCommonControlsEx)
for (int i = 0; i < 16; i++)
icc.dwICC = 1 << i, pInitCommonControlsEx(&icc);
// riched32.dll,riched20.dll,msftedit.dll
LoadLibrary(TEXT("riched32.dll"));
LoadLibrary(TEXT("riched20.dll"));
LoadLibrary(TEXT("msftedit.dll"));
// shell32.dll
HMODULE hshell32 = LoadLibrary(TEXT("shell32.dll"));
FARPROC pInitNetworkAddressControl = NULL;
if (hshell32)
pInitNetworkAddressControl = GetProcAddress(hshell32, "InitNetworkAddressControl");
if (pInitNetworkAddressControl)
pInitNetworkAddressControl();
DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc, NULL);
return 0;
}
</commdlg.h></commctrl.h></windowsx.h></tchar.h></windows.h>