;;启动后的效果
;点击性别后的效果
;把代码抄进radasm里面,可以直接编译运行。重要的地方加了备注。
;这个有点复杂,重要的地方加了备注
;以下是ASM文件
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat, stdcall
option casemap :none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
include gdi32.inc ;加入这个
includelib gdi32.lib ;加入这个
include comctl32.inc ;加入这个
includelib comctl32.lib ;加入这个
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN equ 1000h ;图标
DLG_MAIN equ 1
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
hInstance dd ?
h_List dd ?
hSortColumn dd ? ;要排序的第几列
.const
szClassName db 'MyClass',0
szlistViewClassName db "SysListView32",0 ;加这个
Heading1 db "序号",0 ;第1列名称
Heading2 db "姓名",0 ;第1列名称
Heading3 db "性别",0
Heading2_1 db "刘备",0 ;第2列第1行字符串
Heading2_2 db "绍婵",0 ;第2列第2行字符串
Heading2_3 db "关羽",0
Heading2_4 db "西施",0
Heading3_1 db "男",0
Heading3_2 db "女",0
Heading3_3 db "男",0
Heading3_4 db "女",0
.data
sztemplate db "%d",0
hProjectOrder01 dd 0 ;每列的排序状态,为0为未排序,为1为升序,为2为降序
hProjectOrder03 dd 0
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
RGB macro red,green,blue ;颜色宏,让listview列表控件好看点
xor eax,eax
mov ah,blue
shl eax,8
mov ah,green
mov al,red
endm
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_String2Dword proc uses ecx edi edx esi String:DWORD ;转换字符串为数字的函数,返回值为转换成功的数字
LOCAL Result:DWORD
mov Result,0
mov edi,String
invoke lstrlen,String
.while eax!=0
xor edx,edx
mov dl,byte ptr [edi]
sub dl,"0"
mov esi,eax
dec esi
push eax
mov eax,edx
push ebx
mov ebx,10
.while esi > 0
mul ebx
dec esi
.endw
pop ebx
add Result,eax
pop eax
inc edi
dec eax
.endw
mov eax,Result
ret
_String2Dword endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_CompareFunc proc uses edi lParam1:DWORD, lParam2:DWORD, SortType:DWORD ;列表视图使用的比较函数,lParam1 是第一项的当前索引, lParam2 是第二项的当前索引
LOCAL buffer[256]:BYTE
LOCAL buffer1[256]:BYTE
LOCAL lvi:LV_ITEM
LOCAL @szBuffer[256]:BYTE
invoke RtlZeroMemory,addr buffer,sizeof buffer
invoke RtlZeroMemory,addr buffer1,sizeof buffer1
mov lvi.imask,LVIF_TEXT
lea eax,buffer
mov lvi.pszText,eax
mov lvi.cchTextMax,256
.if SortType==1 ;比如 1 代表根据序号按升序排列所有的项目
mov eax,hSortColumn
mov lvi.iSubItem,eax
invoke SendMessage,h_List,LVM_GETITEMTEXT,lParam1,addr lvi
invoke _String2Dword,addr buffer
mov edi,eax
invoke SendMessage,h_List,LVM_GETITEMTEXT,lParam2,addr lvi
invoke _String2Dword,addr buffer
sub edi,eax
mov eax,edi
.elseif SortType==2
mov eax,hSortColumn
mov lvi.iSubItem,eax
invoke SendMessage,h_List,LVM_GETITEMTEXT,lParam1,addr lvi
invoke _String2Dword,addr buffer
mov edi,eax
invoke SendMessage,h_List,LVM_GETITEMTEXT,lParam2,addr lvi
invoke _String2Dword,addr buffer
sub eax,edi
.endif
ret
_CompareFunc endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_UpdatelParam proc uses edi ;更新重置列表视图内容
LOCAL lvi:LV_ITEM
invoke SendMessage,h_List, LVM_GETITEMCOUNT,0,0
mov edi,eax
mov lvi.imask,LVIF_PARAM
mov lvi.iSubItem,0
mov lvi.iItem,0
.while edi>0
push lvi.iItem
pop lvi.lParam
invoke SendMessage,h_List, LVM_SETITEM,0,addr lvi
inc lvi.iItem
dec edi
.endw
ret
_UpdatelParam endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ShowListViewInfo proc ;显示列表视图里的每项信息
LOCAL lvItem:LV_ITEM
LOCAL @buffer[10]:byte
;以下填充第1行的内容
mov lvItem.imask,LVIF_TEXT
mov lvItem.iItem, 0 ;0为第1列,从0开始计算
mov lvItem.iSubItem, 0 ;0为第1行
invoke wsprintf,addr @buffer, addr sztemplate,1 ;格式化第1列第1行要显示的字符串
lea eax, @buffer
mov lvItem.pszText, eax
mov lvItem.cchTextMax, sizeof Heading1
invoke SendMessage, h_List, LVM_INSERTITEM, 0, addr lvItem
mov lvItem.imask,LVIF_TEXT
mov lvItem.iSubItem,1
lea eax,offset Heading2_1
mov lvItem.pszText,eax
invoke SendMessage,h_List, LVM_SETITEM,0, addr lvItem
mov lvItem.imask,LVIF_TEXT
mov lvItem.iSubItem,2
lea eax,offset Heading3_1
mov lvItem.pszText,eax
invoke SendMessage,h_List, LVM_SETITEM,0, addr lvItem
;以下填充第2行的内容
mov lvItem.iItem, 1
mov lvItem.iSubItem, 0
invoke wsprintf,addr @buffer, addr sztemplate,2 ;第1列第2行字符串
lea eax, @buffer
mov lvItem.pszText, eax
invoke SendMessage, h_List, LVM_INSERTITEM, 0, addr lvItem
mov lvItem.imask,LVIF_TEXT
mov lvItem.iSubItem,1
lea eax,offset Heading2_2
mov lvItem.pszText,eax
invoke SendMessage,h_List, LVM_SETITEM,0, addr lvItem
mov lvItem.imask,LVIF_TEXT
mov lvItem.iSubItem,2
lea eax,offset Heading3_2
mov lvItem.pszText,eax
invoke SendMessage,h_List, LVM_SETITEM,0, addr lvItem
;以下填充第3行的内容
mov lvItem.iItem, 2
mov lvItem.iSubItem, 0
invoke wsprintf,addr @buffer, addr sztemplate,3 ;第1列第3行字符串
lea eax, @buffer
mov lvItem.pszText, eax
invoke SendMessage, h_List, LVM_INSERTITEM, 0, addr lvItem
mov lvItem.imask,LVIF_TEXT
mov lvItem.iSubItem,1
lea eax,offset Heading2_3
mov lvItem.pszText,eax
invoke SendMessage,h_List, LVM_SETITEM,0, addr lvItem
mov lvItem.imask,LVIF_TEXT
mov lvItem.iSubItem,2
lea eax,offset Heading3_3
mov lvItem.pszText,eax
invoke SendMessage,h_List, LVM_SETITEM,0, addr lvItem
;以下填充第4行的内容
mov lvItem.iItem, 3
mov lvItem.iSubItem, 0
invoke wsprintf,addr @buffer, addr sztemplate,4 ;第1列第4行字符串
lea eax, @buffer
mov lvItem.pszText, eax
invoke SendMessage, h_List, LVM_INSERTITEM, 0, addr lvItem
mov lvItem.imask,LVIF_TEXT
mov lvItem.iSubItem,1
lea eax,offset Heading2_4
mov lvItem.pszText,eax
invoke SendMessage,h_List, LVM_SETITEM,0, addr lvItem
mov lvItem.imask,LVIF_TEXT
mov lvItem.iSubItem,2
lea eax,offset Heading3_4
mov lvItem.pszText,eax
invoke SendMessage,h_List, LVM_SETITEM,0, addr lvItem
ret
_ShowListViewInfo endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_List_InsertColumn proc ;插入列数及名称的函数
LOCAL lvc:LV_COLUMN
mov lvc.imask,LVCF_TEXT+LVCF_WIDTH
mov lvc.pszText,offset Heading1 ;第1列的标题栏名称
mov lvc.lx,100 ;宽度
invoke SendMessage,h_List, LVM_INSERTCOLUMN, 0, addr lvc
or lvc.imask,LVCF_FMT
mov lvc.fmt,LVCFMT_LEFT
mov lvc.pszText,offset Heading2 ;第2列的标题栏名称
mov lvc.lx,150
invoke SendMessage,h_List, LVM_INSERTCOLUMN, 1 ,addr lvc
mov lvc.pszText,offset Heading3
mov lvc.lx,125
invoke SendMessage,h_List, LVM_INSERTCOLUMN, 2 ,addr lvc
ret
_List_InsertColumn endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcDlgMain proc uses ebx edi esi hWnd,wMsg,wParam,lParam
LOCAL @DEFAULT_GUI_FONT,@OLD_DEFAULT_GUI_FONT ;加入这个
mov eax,wMsg
.if eax == WM_CLOSE
invoke EndDialog,hWnd,NULL
.elseif eax == WM_INITDIALOG
invoke LoadIcon,hInstance,ICO_MAIN
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax
invoke GetStockObject,DEFAULT_GUI_FONT
mov @DEFAULT_GUI_FONT,eax
invoke CreateWindowEx, NULL, addr szlistViewClassName, NULL, LVS_REPORT or WS_CHILD or WS_VISIBLE, \ ;名单列表视图
0,0,0,0,hWnd, NULL, hInstance, NULL
mov h_List, eax
mov eax,LVS_EX_FULLROWSELECT or LVS_EX_GRIDLINES or LVS_EX_SUBITEMIMAGES ;设置ListView全行选择,网格等样式.
invoke SendMessage,h_List,LVM_SETEXTENDEDLISTVIEWSTYLE,0,eax
invoke ImageList_Create,0,25,ILC_COLORDDB or ILC_MASK,0,0 ;调整ImageList高,就可调整listView行高,就是把它撑大点
invoke SendMessage,h_List,LVM_SETIMAGELIST,LVSIL_SMALL,eax
invoke SendMessage,h_List,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
RGB 0,0,0
invoke SendMessage,h_List,LVM_SETTEXTCOLOR,0,eax ;设置文本颜色
RGB 131,175,155
invoke SendMessage,h_List,LVM_SETBKCOLOR,0,eax ;设置背景颜色
RGB 131,175,155
invoke SendMessage,h_List,LVM_SETTEXTBKCOLOR,0,eax ;设置文本背景颜色
invoke MoveWindow,h_List,0,70,600,520,TRUE ;调整列表视图的位置才能显示出来
invoke _List_InsertColumn ;调用初始化ListView控件的函数
invoke _ShowListViewInfo ;调用初始化显示列表视图内容的函数
.elseif eax== WM_NOTIFY
push edi
mov edi,lParam ;接收到了 WM_NOTIFY 消息后,lParam 包含了指向 NMHDR 型结构体变量的指针。我们通过把列表视图控件的值和 NMHDR 型
assume edi:ptr NMHDR ;结构体变量中的 hwndFrom 成员变量的值比较来判断,如果相等的话就可以确定消息是列表视图控件发送的
mov eax,[edi].hwndFrom
.if eax==h_List
.if [edi].code==LVN_COLUMNCLICK ;点击了列标题条
assume edi:ptr NM_LISTVIEW
.if [edi].iSubItem == 0 ;如果单击了第1列标题条
.if hProjectOrder01==0 || hProjectOrder01==2 ;用状态变量来保持当前的排序顺序。0 代表没有排序,1代表升序,2 代表降序
mov hSortColumn,0
invoke SendMessage,h_List,LVM_SORTITEMS,1,addr _CompareFunc ;发送消息设成升序
invoke _UpdatelParam
mov hProjectOrder01,1
.else
mov hSortColumn,0
invoke SendMessage,h_List,LVM_SORTITEMS,2,addr _CompareFunc
invoke _UpdatelParam
mov hProjectOrder01,2
.endif
.elseif [edi].iSubItem == 2 ;如果单击了第3列标题条
.if hProjectOrder03==0 || hProjectOrder03==2
mov hSortColumn,1
invoke SendMessage,h_List,LVM_SORTITEMS,1,addr _CompareFunc
invoke _UpdatelParam
mov hProjectOrder03,1
.else
mov hSortColumn,1
invoke SendMessage,h_List,LVM_SORTITEMS,2,addr _CompareFunc
invoke _UpdatelParam
mov hProjectOrder03,2
.endif
.endif
assume edi:ptr NMHDR
.endif
.endif
pop edi
.elseif eax == WM_COMMAND
mov eax,wParam
.if ax == IDOK
invoke EndDialog,hWnd,NULL
.endif
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
_ProcDlgMain endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
invoke ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start;以下是rc资源文件
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <resource.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#define ICO_MAIN 0x1000 //图标
#define DLG_MAIN 1
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN ICON "Main.ico"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DLG_MAIN DIALOG 50, 50, 250, 385
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "win32汇编环境窗口程序中生成listview列表控件点击标题栏自动排序的示例"
FONT 9, "宋体"
{
ICON ICO_MAIN, -1, 10, 11, 18, 21
CTEXT "简单的对话框例子\n用Win32ASM编写", -1, 36, 14, 70, 19
DEFPUSHBUTTON "退出(&X)", IDOK, 150, 14, 50, 14
// CONTROL "", -1, "Static", SS_ETCHEDHORZ | WS_CHILD | WS_VISIBLE, 6, 39, 103, 1
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>