masm32之一个使用rc资源的窗口文件

简短记录一下masm下第一个使用rc资源编译的窗口文件

注:代码完全来自于某个博主的博客,当初也只是想着复制,编译链接,运行一下看看效果。

hello.rc

// 资源文件注释格式为双斜杠
// 包含资源头文件,以能使用头键字
#include    <resource.h>

// 指定对话框ID,asm文件中要定义同值变量才可引用
#define    DLG_HELLOWORLD    1

// 定义对话框结构
DLG_HELLOWORLD    DIALOG 350,200,213,164
STYLE                        DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION                    "Helloworld Program"
FONT                        11,"宋体"
{
    CTEXT    "Win32 Assembly Helloworld Program",-1,50,54,170,21
    CONTROL "",-1,"Static",SS_ETCHEDHORZ | WS_CHILD | WS_VISIBLE,6,79,203,1
    DEFPUSHBUTTON "quit",IDOK,158,86,50,21
}

没有学过资源文件编写相关,当初看感觉还有点懵,现在再看,感觉结构清晰,一看就知道哪个部分有什么效果。

hello.asm

; asm文件注释格式为分号
; 定义程序模式
.386
.model            flat,stdcall
option            casemap    :none

; 包含必要头文件,基本每个win32 汇编程序都需要包含这几个
include windows.inc
include  user32.inc
includelib user32.lib
include  kernel32.inc
includelib kernel32.lib

; 指定对话框ID,该ID要与rc文件中的ID值相同
; 理论上,asm文件与rc文件中的控件是通过ID值关联的,控件名并不需要与rc文件相同,不过为了易看一般取一样的
; 比如这里重点是equ 1,叫不叫DLG_HELLOWORLD无所谓,不过为了易看所以选择与rc文件保持一致
DLG_HELLOWORLD    equ    1

; 数据段
.data?
hInstance    dd    ?

; 代码段
.code
; 对话框处理过程
_ProcDlgHelloworld proc uses ebx edi esi hWnd,wMsg,wParam,lParam
    mov    eax,wMsg
    .if eax == WM_CLOSE
            invoke    EndDialog,hWnd,NULL
    .elseif eax == WM_INITDIALOG
            ;invoke    LoadIcon,hInstance,ICO_MAIN
            ;incoke    SendMessage,hWnd,WM_SETICON,ICON_BIG,eax
    .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
_ProcDlgHelloworld    endp

start:
    invoke    GetModuleHandle,NULL
    mov       hInstance,eax
    ; 弹出对话框,对话框与及处理过程在这里绑定
    invoke    DialogBoxParam,hInstance,DLG_HELLOWORLD,NULL,offset _ProcDlgHelloworld,NULL
    invoke    ExitProcess,NULL
    ; 指定程序入口点为start标识处
    end       start

编译链接过程:

C:\Users\17724\Desktop\318>rc hello.rc

C:\Users\17724\Desktop\318>ml -c -coff hello.asm
Microsoft (R) Macro Assembler Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: hello.asm

***********
ASCII build
***********

C:\Users\17724\Desktop\318>link -subsystem:windows hello.obj hello.RES
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.


C:\Users\17724\Desktop\318>hello.exe

C:\Users\17724\Desktop\318>

pe.rc

#include <resource.h>

#define ICO_MAIN 1000
#define DLG_MAIN 1000
#define IDC_INFO 1001
#define IDM_MAIN 2000
#define IDM_OPEN 2001
#define IDM_EXIT 2002

#define IDM_1 4000
#define IDM_2 4001
#define IDM_3 4002
#define IDM_4 4003

ICO_MAIN ICON "saz.ico"

ICO_MAIN DIALOG 50,50,544,399
STYLE DS_MODALFRAME | WS_POPUP |WS_VISIBLE |WS_CAPTION| WS_SYSMENU
CAPTION "JIBEN XINXI"
MENU IDM_MAIN
FONT 9,"宋体"
BEGIN
	CONTROL "",IDC_INFO,"RichEdit20A",196|ES_WANTRETURN|WS_CHILD
		|WS_VISIBLE|WS_BORDER |WS_VSCROLL|WS_TABSTOP,0,0,540,396
END

IDM_MAIN menu discardable
BEGIN
	POPUP "file(&F)"
		BEGIN
			menuitem "openFile(&O)",IDM_OPEN
			menuitem separator
			menuitem "quit(&X)",IDM_EXIT
		END
	POPUP "LOOK"
		BEGIN
			menuitem "oriFile",IDM_1
			menuitem "touming",IDM_2
			menuitem separator
			menuitem "size",IDM_3
			menuitem "width",IDM_4
		END
END

pe.asm

	.386
	.model flat,stdcall
	option casemap:none

include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
include comdlg32.inc
includelib comdlg32.lib

ICO_MAIN EQU 1000
DLG_MAIN EQU 1000
IDC_INFO EQU 1001
IDM_MAIN EQU 2000
IDM_OPEN EQU 2001
IDM_EXIT EQU 2002
IDM_1 EQU 4000
IDM_2 EQU 4001
IDM_3 EQU 4002

	.data
hInstance dd ?
hRichEdit dd ?
hWinMain dd ?
hWinEdit dd ?
szFileName db MAX_PATH dup(?)

	.const
szDllEdit db 'RichEd20.dll',0
szClassEdit db 'RichEdit20A',0
szFont db '宋体',0

	.code
_init proc
	local @stCf:CHARFORMAT
	invoke GetDlgItem,hWinMain,IDC_INFO
	mov hWinEdit,eax

	invoke LoadIcon,hInstance,ICO_MAIN
	invoke SendMessage,hWinMain,WM_SETICON,ICON_BIG,eax

	invoke SendMessage,hWinEdit,EM_SETTEXTMODE,TM_PLAINTEXT,0
	invoke RtlZeroMemory,addr @stCf,sizeof @stCf
	mov @stCf.cbSize,sizeof @stCf
	mov @stCf.yHeight,9*20
	mov @stCf.dwMask,CFM_FACE or CFM_SIZE or CFM_BOLD
	invoke lstrcpy,addr @stCf.szFaceName,addr szFont
	invoke SendMessage,hWinEdit,EM_SETCHARFORMAT,0,addr @stCf
	invoke SendMessage,hWinEdit,EM_EXLIMITTEXT,0,-1
	ret
_init endp

_ProcDlgMain proc uses ebx edi esi hWnd,wMsg,wParam,lParam
	mov eax,wMsg
	.if eax==WM_CLOSE
		invoke EndDialog,hWnd,NULL
	.elseif eax==WM_INITDIALOG
		push hWnd
		pop hWinMain
		call _init
	.elseif eax == WM_COMMAND
		mov eax,wParam
		.if eax==IDM_EXIT
			invoke EndDialog,hWnd,NULL
		.elseif eax==IDM_OPEN
		.elseif eax==IDM_1
		.elseif eax==IDM_2
		.elseif eax==IDM_3
		.endif
	.else
		mov eax,FALSE
		ret
	.endif
	mov eax,TRUE
	ret
_ProcDlgMain endp

start:
	invoke LoadLibrary,offset szDllEdit
	mov hRichEdit,eax
	invoke GetModuleHandle,NULL
	mov hInstance,eax
	invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
	invoke FreeLibrary,hRichEdit
	invoke ExitProcess,NULL
	end start
posted @ 2022-03-18 22:26  念秋  阅读(208)  评论(0编辑  收藏  举报