win32汇编环境,窗口程序中组合框的应用举例
;运行效果
;win32汇编环境,窗口程序中组合框的应用举例
;比如在窗口程序中生成组合框,增加子项,删除某项,取得指定项内容等
;直接抄进RadAsm可编译运行。重点部分加备注。
;以下是ASM文件
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
include gdi32.inc
include kernel32.inc
includelib kernel32.lib
includelib user32.lib
includelib gdi32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.DATA
ClassName db "SimpleWinClass",0
AppName db "窗口程序的框架",0
szButtonClassName db "button",0 ;按钮类名
szComboBoxClassName db "ComboBox",0 ;组合框类名
szTitle db "提示",0
szAddCb01 db "关羽",0
szAddCb02 db "许褚",0
szAddCb03 db "吕布",0
szAddCb04 db "张飞",0
szAddCb05 db "孙策",0
szCheshi_Format01 db "总项数是 %d",0
szButtonTitle01 db "点击从后面增加张飞",0
szButtonTitle02 db "点击删除第3项",0
szButtonTitle03 db "判断当前选中谁",0
szButtonTitle04 db "将孙策插入第2项",0
szButtonTitle05 db "得到组合框的总项数",0
.DATA?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hComboBox01 HWND ? ;组合框控件句柄变量
hButton01 HWND ? ;按钮控件句柄变量
hButton02 HWND ?
hButton03 HWND ?
hButton04 HWND ?
hButton05 HWND ?
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.const
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IDC_CBO1 equ 10 ;组合框控件标识符
ButtonID01 equ 11 ;按钮控件标识符
ButtonID02 equ 12
ButtonID03 equ 13
ButtonID04 equ 14
ButtonID05 equ 15
.CODE
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke GetCommandLine
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess, eax
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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 hInstance
pop wc.hInstance
mov wc.hbrBackground,COLOR_WINDOW+1
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,NULL, ADDR ClassName,ADDR AppName,WS_OVERLAPPEDWINDOW,100,100,255,330, NULL,NULL,hInst, NULL
mov hwnd,eax
invoke ShowWindow, hwnd,CmdShow
invoke UpdateWindow, hwnd
.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
LOCAL @DEFAULT_GUI_FONT,@OLD_DEFAULT_GUI_FONT
LOCAL @szBuffer[256]:byte
.if uMsg == WM_CREATE
invoke CreateWindowEx,NULL,ADDR szComboBoxClassName,NULL, WS_CHILDWINDOW or WS_VISIBLE or WS_TABSTOP or CBS_DROPDOWN ,20,20,200,100,hWnd,IDC_CBO1,NULL,NULL ;创建组合框
mov hComboBox01,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle01, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,70,200,25,hWnd,ButtonID01,NULL,NULL
mov hButton01,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle02, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,110,200,25,hWnd,ButtonID02,NULL,NULL
mov hButton02,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle03, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,150,200,25,hWnd,ButtonID03,NULL,NULL
mov hButton03,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle04, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,190,200,25,hWnd,ButtonID04,NULL,NULL
mov hButton04,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle05, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,230,200,25,hWnd,ButtonID05,NULL,NULL
mov hButton05,eax
invoke GetStockObject,DEFAULT_GUI_FONT
mov @DEFAULT_GUI_FONT,eax
invoke GetDlgItem,hWnd,IDC_CBO1
invoke SendMessage,eax,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hButton01,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE ;设置按钮控件的字体
invoke SendMessage,hButton02,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hButton03,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hButton04,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hButton05,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 == ButtonID01
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_ADDSTRING,0,addr szAddCb04 ;从后面增加新项
.elseif ax == ButtonID02
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_DELETESTRING,2,0 ;从零开始的索引,为2则删除第3项
.elseif ax == ButtonID03
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 == ButtonID04
invoke SendDlgItemMessage,hWnd,IDC_CBO1,CB_INSERTSTRING,1,addr szAddCb05 ;把孙策插入成为第2项
.elseif ax == ButtonID05
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_DESTROY
invoke PostQuitMessage,NULL
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start