;运行效果
;win32汇编环境,窗口程序中基础列表框的应用举例
;比如在窗口程序中生成列表框,增加子项,删除某项,取得指定项内容等
;直接抄进RadAsm可编译运行。重点部分加备注。
;以下是ASM文件
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
include kernel32.inc
include gdi32.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 ;按钮类名
szListBoxClassName db "LISTBOX",0 ;列表框类名
szTitle db "提示",0
szCheckTitle db "只能选一位",0
szAddLib01 db "刘备",0
szAddLib02 db "关羽",0
szAddLib03 db "张飞",0
szAddLib04 db "孙策",0
szAddLib05 db "吕布",0
szFormat db '总项数是 %d',0
szButtonTitle01 db "新增孙策",0
szButtonTitle02 db "删除选中项",0
szButtonTitle03 db "将吕布插入第2项",0
szButtonTitle04 db "得到总项数",0
szButtonTitle05 db "得到选中项的名称",0
.DATA?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hID_GROUP HWND ? ;组框控件句柄
hListBox01 HWND ? ;列表框控件句柄变量
hButton01 HWND ? ;按钮控件句柄变量
hButton02 HWND ?
hButton03 HWND ?
hButton04 HWND ?
hButton05 HWND ?
.const
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IDC_LBO1 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,260,370, 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 GetStockObject,DEFAULT_GUI_FONT
mov @DEFAULT_GUI_FONT,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szCheckTitle, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_GROUPBOX,10,5,220,125,hWnd,NULL,NULL,NULL ;创建组框控件 mov hID_GROUP,eax
mov hID_GROUP,eax
invoke CreateWindowEx,NULL,ADDR szListBoxClassName,NULL, WS_BORDER + WS_CHILDWINDOW + WS_VISIBLE + WS_TABSTOP ,20,20,200,100,hWnd,IDC_LBO1,NULL,NULL ;创建列表框
mov hListBox01,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle01, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,150,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,190,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,220,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,250,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,280,200,25,hWnd,ButtonID05,NULL,NULL
mov hButton05,eax
invoke SendMessage,hID_GROUP,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hListBox01,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_LBO1,LB_ADDSTRING,0,addr szAddLib01
invoke SendDlgItemMessage,hWnd,IDC_LBO1,LB_SETCURSEL,0,0 ;设置为焦点项
invoke SendDlgItemMessage,hWnd,IDC_LBO1,LB_ADDSTRING,0,addr szAddLib02
invoke SendDlgItemMessage,hWnd,IDC_LBO1,LB_ADDSTRING,0,addr szAddLib03
.elseif uMsg == WM_COMMAND
mov eax,wParam
mov edx,wParam
shr edx,16
.if ax == ButtonID01
.if dx == BN_CLICKED
invoke SendDlgItemMessage,hWnd,IDC_LBO1,LB_ADDSTRING,0,addr szAddLib04 ;从后面增加新项
.endif
.elseif ax == ButtonID02
.if dx == BN_CLICKED
invoke GetDlgItem,hWnd,IDC_LBO1 ;得到列表控件的句柄
invoke SendMessage,eax,LB_GETCURSEL,0,0 ;得到当前选中项的索引,从0开始
invoke SendDlgItemMessage,hWnd,IDC_LBO1,LB_DELETESTRING,eax,0 ;删除指定项,eax是焦点项
.endif
.elseif ax == ButtonID03
.if dx == BN_CLICKED
invoke SendDlgItemMessage,hWnd,IDC_LBO1,LB_INSERTSTRING,1,addr szAddLib05 ;插入某项,索引从0开始,1就是第2项
.endif
.elseif ax == ButtonID04
.if dx == BN_CLICKED
invoke SendDlgItemMessage,hWnd,IDC_LBO1,LB_GETCOUNT,0,0 ;获取总项数
invoke wsprintf,addr @szBuffer,addr szFormat,eax
invoke MessageBox,hWnd,addr @szBuffer,addr szTitle,MB_OK or MB_ICONINFORMATION
.endif
.elseif ax == ButtonID05
.if dx == BN_CLICKED
invoke GetDlgItem,hWnd,IDC_LBO1
invoke SendMessage,eax,LB_GETCURSEL,0,0
mov ebx,eax
invoke SendDlgItemMessage,hWnd,IDC_LBO1,LB_GETTEXT,ebx,addr @szBuffer ;addr前不能使用eax,所以用ebx转一下
invoke MessageBox,hWnd,addr @szBuffer,addr szTitle,MB_OK or MB_ICONINFORMATION
.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