;运行效果
;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
szChecked01 db "当前选中的是: 刘备",0
szChecked02 db "当前选中的是: 关羽",0
szChecked03 db "当前选中的是: 张飞",0
.data?
hInstance HINSTANCE ?
.const
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN equ 1000 ;图标
IDR01 equ 11
IDR02 equ 12
IDR03 equ 13
IDB01 equ 21
IDB02 equ 22
IDB03 equ 23
IDB04 equ 24
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.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[1024]: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,IDR01
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE ;设置控件的字体为系统字体,好看一点
invoke GetDlgItem,hWnd,IDR02
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke GetDlgItem,hWnd,IDR03
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke GetDlgItem,hWnd,IDB01
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 CheckDlgButton,hWnd,IDR02,BST_CHECKED ;可以先初始化让哪个处于选定状态,初定为第2项
.elseif uMsg == WM_COMMAND
mov eax,wParam
.if lParam == 0 ;如果lParam参数不包含控件句柄,一般是指菜单信息
.else
mov edx,wParam ;wParam是32位的数值,其高16位为控件标识符,低16位为通知码
shr edx,16 ;将edx右移16位,即让其高16位置0,也即让edx的低16位dx的值为取得的通知码,然后下面进行判断
.if dx == BN_CLICKED
.if ax == IDB01
invoke IsDlgButtonChecked,hWnd,IDR01 ;检查第1项是否选中
.if eax == BST_CHECKED ;如果选中,则弹出提示
invoke MessageBox,NULL,addr szChecked01,addr szTitle,MB_OK
.endif
invoke IsDlgButtonChecked,hWnd,IDR02
.if eax == BST_CHECKED
invoke MessageBox,NULL,addr szChecked02,addr szTitle,MB_OK
.endif
invoke IsDlgButtonChecked,hWnd,IDR03
.if eax == BST_CHECKED
invoke MessageBox,NULL,addr szChecked03,addr szTitle,MB_OK
.endif
.elseif ax == IDB02
invoke CheckDlgButton,hWnd,IDR01,BST_UNCHECKED ;让3项都未选中
invoke CheckDlgButton,hWnd,IDR02,BST_UNCHECKED
invoke CheckDlgButton,hWnd,IDR03,BST_UNCHECKED
.elseif ax == IDB03
invoke CheckDlgButton,hWnd,IDR03,BST_CHECKED ;选中第3项
invoke CheckDlgButton,hWnd,IDR01,BST_UNCHECKED
invoke CheckDlgButton,hWnd,IDR02,BST_UNCHECKED
.elseif ax == IDB04
invoke GetDlgItemText,hWnd,IDR01,addr @szBuffer,sizeof @szBuffer ;得到第1项的名称
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 IDR01 11 //单选框标识符
#define IDR02 12
#define IDR03 13
#define IDB01 21 //按钮标识符
#define IDB02 22
#define IDB03 23
#define IDB04 24
ICO_MAIN ICON "Main.ico"
//定义对话框
MyDialog DIALOG 10, 10, 120, 150
STYLE DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK | WS_THICKFRAME
CAPTION "对话框程序模版"
BEGIN
AUTORADIOBUTTON "刘备", IDR01, 10, 10, 100, 12, WS_TABSTOP //定义单选框,使用AUTORADIOBUTTON关键字定义的单选框,会形成一组,只能单选其中一个
AUTORADIOBUTTON "关羽", IDR02, 10, 25, 100, 12, WS_TABSTOP
AUTORADIOBUTTON "张飞", IDR03, 10, 40, 100, 12, WS_TABSTOP
CONTROL "点击查看当前选定了哪一项",IDB01,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,60,100,14
CONTROL "让所有项都处于未选中状态",IDB02,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,80,100,14
CONTROL "选中第3项",IDB03,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,100,100,14
CONTROL "得到第1项的名称",IDB04,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,120,100,14
END