;运行效果
;win32汇编环境,对话框程序中组合框的应用举例
;比如在对话框中生成组合框,增加子项,删除某项,取得指定项内容等
;直接抄进RadAsm可编译运行。重点部分加备注。
;以下是ASM文件
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
include kernel32.inc
include gdi32.inc
includelib user32.lib
includelib kernel32.lib
includelib gdi32.lib
; 自定义函数声明
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD ;对话框窗口函数
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data
DlgName db "MyDialog",0
szTitle db "提示",0
szAddCb01 db "关羽",0
szAddCb02 db "许褚",0
szAddCb03 db "吕布",0
szAddCb04 db "张飞",0
szAddCb05 db "孙策",0
szCheshi_Format01 db "总项数是 %d",0
.data?
hInstance HINSTANCE ?
.const
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN equ 1000 ;图标
IDC_CBO1 equ 31
IDB01 equ 21
IDB02 equ 22
IDB03 equ 23
IDB04 equ 24
IDB05 equ 25
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke DialogBoxParam, hInstance, ADDR DlgName,NULL, addr DlgProc, NULL
invoke ExitProcess,eax
DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL @DEFAULT_GUI_FONT,@OLD_DEFAULT_GUI_FONT
LOCAL @szBuffer[256]:byte
LOCAL @szBuffer01[256]:byte
.if uMsg == WM_INITDIALOG
invoke LoadIcon,hInstance,ICO_MAIN
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax
invoke GetStockObject,DEFAULT_GUI_FONT ;调用GetStockObject函数,得到系统默认字体
mov @DEFAULT_GUI_FONT,eax
invoke GetDlgItem,hWnd,IDB01 ;发送WM_SETFONT消息,设置控件的字体
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke GetDlgItem,hWnd,IDB02
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke GetDlgItem,hWnd,IDB03
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke GetDlgItem,hWnd,IDB04
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke GetDlgItem,hWnd,IDB05
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke GetDlgItem,hWnd,IDC_CBO1
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke DeleteObject,@DEFAULT_GUI_FONT ;删除本程序新建的字体
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_ADDSTRING,0,addr szAddCb01 ;在初始化时可增加组合框的文字项
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_SETCURSEL,0,0 ;设置为焦点项
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_ADDSTRING,0,addr szAddCb02
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_ADDSTRING,0,addr szAddCb03
.elseif uMsg == WM_COMMAND
mov eax,wParam
.if lParam != 0
mov edx,wParam
shr edx,16
.if dx == BN_CLICKED
.if ax == IDB01
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_ADDSTRING,0,addr szAddCb04 ;从后面增加新项
.elseif ax == IDB02
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_DELETESTRING,2,0 ;从零开始的索引,为2则删除第3项
.elseif ax == IDB03
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_GETCURSEL,0,0 ;返回当前焦点项的索引
mov ebx,eax ;借用一下ebx,因为下句的addr语句前不能使用eax,addr在编译时也要使用寄存器eax
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_GETLBTEXT,ebx,addr @szBuffer ;ebx为当前具有焦点的项的索引值,从0开始
invoke MessageBox,NULL,addr @szBuffer,addr szTitle,MB_OK
.elseif ax == IDB04
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_INSERTSTRING,1,addr szAddCb05 ;把孙策插入成为第2项
.elseif ax == IDB05
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_GETCOUNT,0,0 ;得到组合框的总项数,可以根据总项数,循环一遍与已定的字符串比较,找到需要的某一项
invoke wsprintf,addr @szBuffer,addr szCheshi_Format01,eax
invoke MessageBox,hWnd,addr @szBuffer,addr szTitle,MB_OK or MB_ICONINFORMATION
.endif
.endif
.endif
.elseif uMsg == WM_CLOSE
invoke EndDialog, hWnd,NULL
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgProc endp
end start
;下面为rc文件内容
#include "resource.h" //提示缺少该文件,可以在资源里下载
#define ICO_MAIN 1000 //图标
#define IDC_CBO1 31 //组合框标识符
#define IDB01 21 //按钮标识符
#define IDB02 22
#define IDB03 23
#define IDB04 24
#define IDB05 25
ICO_MAIN ICON "Main.ico"
//定义对话框
MyDialog DIALOG 10, 10, 120, 140
STYLE DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "对话框程序模版"
BEGIN
CONTROL "",IDC_CBO1,"ComboBox",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP|CBS_DROPDOWN,10,10,100,100
CONTROL "点击从后面增加张飞",IDB01,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,30,100,14
CONTROL "点击删除第3项",IDB02,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,50,100,14
CONTROL "判断当前选中谁",IDB03,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,70,100,14
CONTROL "将孙策插入第2项",IDB04,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,90,100,14
CONTROL "得到组合框的总项数",IDB05,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,110,100,14
END