本课中我们将学习什么是通用控件和如何使用它们。
理论:win95相对于win3x有几个加强的用户界面控件。其实在win95正式发行前这些控件就在使用,譬如:状态条、工具条等。以前程序员要自己去编程使用它们,现在微软已经把它们包含到了win9x和winnt中了。
- toolbar ---工具条
- tooltip ---提示文本
- status bar ---状态条
- property sheet ---属性页单
- property page ---属性页
- tree view ---树型视图
- list view ---列表视图
- animation ---动画
- drag list ---能够处理drag-drop的列表框
- header ---
- hot-key ---热键
- image list ---图象链表
- progress bar ---进程状态条
- right edit ---
- tab ---跳格表
- trackbar ---跟踪条
- up-down ---滚动条
因为通用控件的数量非常多,把它们全部装入内存并注册它们是非常浪费内存的。除了“rtf文本编辑”控件外其他控件的可执行代码都放在comctl32.dll中,这样其他的应用程序就可以使用它们了。“rtf文本编辑”控件在richedxx.dll中,由于该控件非常的复杂,所以也比其它控件大。 要加载comctl32.dll可以在您的应用程序中调用函数initcommoncontrols。initcommoncontrols函数是动态链接库comctl32.dll中的一个函数,只要在您的程序中的任意地方引用了该函数就、会使得windows的程序加载器pe loader加载该库。函数initcommoncontrols其实只有一条指令“ret”,它的唯一目的是为了使得在调用了个该函数的应用程序的可执行文件的pe头中的“引入”段中包含有comctl32.dll,这样无论什么时候该应用程序都会为您加载该库。所以真正初始化的工作是在该库的入口点处做的,在这里会注册所有的通用控件类,然后所有的通用控件就可以在这些类上进行创建,这就象创建其它的子窗口控件一样。 rtf文本编辑控件则不同。如果您要使用它,就必须调用loadlibrary函数来动态加载,并调用freelibrary来动态地卸载。 现在我们学习如何创建这些通用控件。您可以用资源编辑器把它们放到一个对话框中,或者您也可以自己调用相关的函数来手动创建它们。几乎所有的通用控件都是调用函数createwindowex或createwindow来创建的,您只要在其中传递通用控件的类名即可。有一些通用控件有一些特别的创建函数,但是其实这些函数在内部都调用了createwindowex,只是包装后的函数更方便使用而已。经过包装的函数有:
- createtoolbarex
- createstatuswindow
- createpropertysheetpage
- propertysheet
- imagelist_create
为了创建通用控件您必须要知道它们的类名,我们把类名列于如下:
|
类名 |
通用控件 |
| toolbarwindow32 |
toolbar |
| tooltips_class32 |
tooltip |
| msctls_statusbar32 |
status bar |
| systreeview32 |
tree view |
| syslistview32 |
list view |
| sysanimate32 |
animation |
| sysheader32 |
header |
| msctls_hotkey32 |
hot-key |
| msctls_progress32 |
progress bar |
| richedit |
rich edit |
| msctls_updown32 |
up-down |
| systabcontrol32 |
tab |
property sheets、property pages和image list控件有它们自己的创建函数。drag list其实是可以伸缩的listbox控件,所以它没有自己的类名。上面的类名是vc++的资源编辑器提供的,它们和borland公司的win32 api指南中提出的不一样,和petzold的书《programming windows 95》也不一样。可以肯定的是我们上面列出的类名绝对准确。 这些通用控件可以有通用的窗口类的一些风格,譬如ws_child等。它们当然还有其他的特殊风格,譬如树型视图控件就有tvs_xxxxx风格,列表控件就有lvs_xxxx风格。具体的最好查找有关的win32 api函数指南。 既然我们已经知道了如何创建一个通用控件,我们就可以讨论这些通用控件之间以及和它们的父窗口之间是如何通讯的了。不象子窗口控件,通用控件在某些状态发生变化时不是通过发送wm_command而是发送wm_notify消息和父窗口通讯的。父窗口可以通过发送消息来控制子窗口的行为。对于那些新的通用控件,还有一些新的消息类型。您可以参考您的win32 api手册。
在下面的例子中我们将要实验一下进度条和状态条。
例子代码:.386 .model flat,stdcall option casemap:none include \masm32\include\windows.inc include \masm32\include\user32.inc include \masm32\include\kernel32.inc include \masm32\include\comctl32.inc includelib \masm32\lib\comctl32.lib includelib \masm32\lib\user32.lib includelib \masm32\lib\kernel32.lib
winmain proto :dword,:dword,:dword,:dword
.const idc_progress equ 1 ; control ids idc_status equ 2 idc_timer equ 3
.data classname db "commoncontrolwinclass",0 appname db "common control demo",0 progressclass db "msctls_progress32",0 ; the class name of the progress bar message db "finished!",0 timerid dd 0
.data? hinstance hinstance ? hwndprogress dd ? hwndstatus dd ? currentstep dd ? .code start: invoke getmodulehandle, null mov hinstance,eax invoke winmain, hinstance,null,null, sw_showdefault invoke exitprocess,eax invoke initcommoncontrols
winmain proc hinst:hinstance,hprevinst:hinstance,cmdline:lpstr,cmdshow:dword local wc:wndclassex local msg:msg local hwnd:hwnd
mov wc.cbsize,sizeof wndclassex mov wc.style, cs_hredraw or cs_vredraw mov wc.lpfnwndproc, offset wndproc mov wc.cbclsextra,null mov wc.cbwndextra,null push hinst pop wc.hinstance mov wc.hbrbackground,color_appworkspace mov wc.lpszmenuname,null mov wc.lpszclassname,offset classname invoke loadicon,null,idi_application mov wc.hicon,eax mov wc.hiconsm,eax invoke loadcursor,null,idc_arrow mov wc.hcursor,eax invoke registerclassex, addr wc invoke createwindowex,ws_ex_clientedge,addr classname,addr appname,\ ws_overlapped+ws_caption+ws_sysmenu+ws_minimizebox+ws_maximizebox+ws_visible,cw_usedefault,\ cw_usedefault,cw_usedefault,cw_usedefault,null,null,\ hinst,null mov hwnd,eax .while true invoke getmessage, addr msg,null,0,0 .break .if (!eax) invoke translatemessage, addr msg invoke dispatchmessage, addr msg .endw mov eax,msg.wparam ret winmain endp
wndproc proc hwnd:hwnd, umsg:uint, wparam:wparam, lparam:lparam .if umsg==wm_create invoke createwindowex,null,addr progressclass,null,\ ws_child+ws_visible,100,\ 200,300,20,hwnd,idc_progress,\ hinstance,null mov hwndprogress,eax mov eax,1000 ; the lparam of pbm_setrange message contains the range mov currentstep,eax shl eax,16 ; the high range is in the high word invoke sendmessage,hwndprogress,pbm_setrange,0,eax invoke sendmessage,hwndprogress,pbm_setstep,10,0 invoke createstatuswindow,ws_child+ws_visible,null,hwnd,idc_status mov hwndstatus,eax invoke settimer,hwnd,idc_timer,100,null ; create a timer mov timerid,eax .elseif umsg==wm_destroy invoke postquitmessage,null .if timerid!=0 invoke killtimer,hwnd,timerid .endif .elseif umsg==wm_timer ; when a timer event occurs invoke sendmessage,hwndprogress,pbm_stepit,0,0 ; step up the progress in the progress bar sub currentstep,10 .if currentstep==0 invoke killtimer,hwnd,timerid mov timerid,0 invoke sendmessage,hwndstatus,sb_settext,0,addr message invoke messagebox,hwnd,addr message,addr appname,mb_ok+mb_iconinformation invoke sendmessage,hwndstatus,sb_settext,0,0 invoke sendmessage,hwndprogress,pbm_setpos,0,0 .endif .else invoke defwindowproc,hwnd,umsg,wparam,lparam ret .endif xor eax,eax ret wndproc endp end start
分析:
invoke winmain, hinstance,null,null, sw_showdefault invoke exitprocess,eax invoke initcommoncontrols 我故意把函数initcommoncontrols放到exitprocess后,这样就可以验证调用该函数仅仅是为了在我们程序的可执行文件的pe头中的引入段中放入引用了comctl32.dll的信息。您可以看到,即使该函数什么都没有做,我们的通用控件对话框依旧可以正常工作。
.if umsg==wm_create invoke createwindowex,null,addr progressclass,null,\ ws_child+ws_visible,100,\ 200,300,20,hwnd,idc_progress,\ hinstance,null mov hwndprogress,eax 在这里我们创建了通用控件。注意createwindowex函数中的参数hwnd是父窗口的句柄。另外它也指定了通用控件的id号。因为我们直接使用控件的窗口句柄,所以就没有使用该id号。所有的窗口都必须具有ws_child风格。
mov eax,1000 mov currentstep,eax shl eax,16 invoke sendmessage,hwndprogress,pbm_setrange,0,eax invoke sendmessage,hwndprogress,pbm_setstep,10,0 在创建了进度条后我们先设定它的范围。缺省的范围是0-100。如果您不满意,可以重新设置,这通过传递pbm_setrange消息来实现。参数lparam中包含了范围值,其中底字和高字分别是范围的起始和终了的值。您可以指定进度条每移动一格的步长。本例子中把步长设置成10,意味着每发送一次pbm_stepit消息给进度条,它的显示指针就会移动10。当然您可以调用pbm_setpos 来直接设定进度条上的指针的位置。用该消息您可以更方便地设定进度条了。
invoke createstatuswindow,ws_child+ws_visible,null,hwnd,idc_status mov hwndstatus,eax invoke settimer,hwnd,idc_timer,100,null ; create a timer mov timerid,eax 下面我们调用createstatuswindow来创建状态条。这个调用很好理解,无需我多解释。在状态条创建后我们创建一个计时器。在本例中我们每隔100毫秒就更新一次进度条。下面时创建记时器的函数原型:
settimer proto hwnd:dword, timerid:dword, timeinterval:dword, lptimerproc:dword hwnd : 父窗口的句柄。 timerid : 计时器的id号。您可以指定一个唯一的非零值。 timerinterval : 以毫秒计的时间间隔。 lptimerproc : 计时器回调函数的地址。每当时间间隔到了的时候,该函数就会被系统调用。如果该值为null,计时器就会把wm_timer消息发送到父窗口。
如果settimer调用成功的话就会返回计时器的id号值,否则返回0。这也是为什么计时器的id号必须为非零值的原因。
.elseif umsg==wm_timer invoke sendmessage,hwndprogress,pbm_stepit,0,0 sub currentstep,10 .if currentstep==0 invoke killtimer,hwnd,timerid mov timerid,0 invoke sendmessage,hwndstatus,sb_settext,0,addr message invoke messagebox,hwnd,addr message,addr appname,mb_ok+mb_iconinformation invoke sendmessage,hwndstatus,sb_settext,0,0 invoke sendmessage,hwndprogress,pbm_setpos,0,0 .endif 当指定的时间到了的时候,计时器将发送wm_timer消息。您可以在处理该消息时作适当的处理。本例中我们将更新进度条,并检查进度条是否超过最大的值。如果超过了的话,我们通过发送sb_settext消息来在状态条中设置文本。这时,弹出一个对话框,当用户关闭掉对话框后,我们去除掉进度条和状态条中的文本。 |