Win32 assembly tutorial three simple dialogs --- use the use of resource files

zhaozj2021-02-08  249

-------------------------------------------------- ----------------------------- Windows resource files no matter what programmed under DOS is still programmed under Windows, we always use To many other data, such as sound data, graphics data, text, etc., in DOS, can we define the format of these files, but this has caused many resource sharing problems, everyone Many games under DOS may still be remembered, their graphics are stored in their own format, you can't use standard view software. It is not possible to save it as other formats. Although we can still do this in Win32, but Win32 is programmed to us a solution - it is a resource file that is unified, including strings, graphics, dialogs, and the above buttons, text, etc. In the file, it can be convenient to use it in different files. Most importantly, if we use your own file format, you should involve reading and writing operations in these files, but complicated, but use the resource file, Windows offers a range of APIs to load resources. Very convenient. Now let's take a simple source file for a very simple resource file, its extension is .rc, when it is compiled with the resource compiler, you can enter the .exe file when you use the .RES file, in Link: # include #define DLG_MAIN 1DLG_MAIN DIALOGEX 0, 0, 236, 185STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENUCAPTION "dialog template" FONT 9, "Times New Roman" BEGINDEFPUSHBUTTON "exit", IDOK, 177,163,50,14CONTROL "", -1, "static", ss_etchedhorz, 7,155,222,1nd now I briefly explain the grammar of the.rc file: #include - Resource.h file includes some constant definitions of the resource file, as follows The style of WS_POPUP, WS_VISIBLE and other window #define DLG_MAIN 1 - Similar to the EQU statement of the .asm file, the same as the assembly source, these definitions are readable for the program. DLG_MAIN DIALOGEX 0,0,236,185windows .rc files can define Bitmap, Cursor (Cursor), Icon (Icon), Accelerators, Dialog, Menu, StringTable (string table 8 resources such as RCDATA (Custom Resources), detailed descriptions can refer to books related to MFC, in the syntax of the resource compiler in Win32ASM, the general format is the definition method of these resources: bitmap definition: NameID Bitmap [load-mem] filename cursor definitions: nameID cURSOR [load-mem] filename icon Definition: nameID iCON [load-mem] filename accelerator key definitions: acctablename ACCELERATORS [optional-statements] BEGIN event, idvalue, [type] [options] ... Hend, etc. The specific definitions and parameters can refer to the RC.hlp help file in Masm32V5.

(You can download it in the programming tool), we can use the resource editor to edit resources, or you can define your resources yourself in your text editor yourself. Using resources in the program In the program, you must first load such as memory before using the resource. Windows defines a range of APIs to load resources, such as loadMenu, LoadString, LoadBitmap, etc., such as LoadBitmap: HbitMap LoadBitmap , // handle of application instance LPCTSTR lpBitmapName // address of bitmap resource name); return values ​​of these functions is a Load handle the call parameters typically at least two: hInstance and ResouceName, this ResouceName (eg BitmapName, MenuName) is In the value of #define in the resource file, if you use #define my_ICON 10 / my_ICON ICON "main.ico" to define an icon, you can use the main.ico icon in the program to use Loadicon (Hinstance, 10) To load icon files that have been defined as 10. Another parameter hinstance is the handle of the execution file, which corresponds to the file name where the resource is located, you can get Hinstance with Invoke getModuleHandle, NULL when executing the program. Other resources are not explicitly mounted, such as dialog resources, which is loaded by Windows in the functions of the dialog, in the following example Invoke DialogBoxParam, Hinstance, DLG_Main, Null, Offset _PrOcdlgmain, 0 is a dialog that has been defined in a resource file on the screen, and there is no API such as loadDialogbox to first load the dialog.

Win32ASM - Show a dialog and introduces so many related things, let us see how to display a dialog, the source program is as follows: .386.Model flat, stdcalloption casemap: none; case sensitiveinclude windows.include user32.incinclude kernel32 ? .incinclude comctl32.incinclude comdlg32.incincludelib user32.libincludelib kernel32.libincludelib comctl32.libincludelib comdlg32.libDLG_MAIN equ 1.data?hInstance dd szBuffer db 256 dup _ ProcDlgMain PROTO (?): DWORD,: DWORD,: DWORD,: DWORD.data .code; ******************************************************** ************* _ ProCDLGMain Proc Uses EBX EDI ESI, / HWND: DWORD, WMSG: DWORD, WPARAM: DWORD, LPARAM: DWORDMOV EAX, WMSG.IF EAX == WM_CLOSEinvoke EndDialog, hWnd, NULL.elseif eax == WM_INITDIALOG.elseif eax == WM_COMMANDmov eax, wParam.if eax == IDOKinvoke EndDialog, hWnd, NULL.elseif eax == IDCANCELinvoke EndDialog, hWnd, NULL.endif.elsemov eax , Falseret.Endif Mov EAX, TRUERET_PROCDLGMAIN ENDP; ****************************************** ************************************ Start: Invoke InitcommonControlsInvoke GetModuleHandle, Nullmov Hinstance, EaxInvoke DialogBoxParam, Hinstance, DLG_MAIN, NULL, OFFSET _PROCDLGMAIN, 0INVOKE EXITPROCDLGMAIN, 0INVOKE EXITPROCDLGMAIN, 0INVOKE EXITPROCDLGMAIN, 0INVOKE EXITPROCDLGMAIN, 0INVOKE EXITPROCDLGMAIN, 0INVOKE EXITPROCDLGMAIN, NULLEND Start After reading the most statement here, I will explain a few new statements: _PrOcdlgmain Proto: DWORD, DWORD,: DWORD,: DWORDPROTO statement is similar to the function definition in the C language, in the Win32 assembly, if the subroutine is defined after reference, you must define, of course, this definition is for the Invoke statement and other band parameters. Call, if your subroutine does not have a parameter, you can use the CALL instruction to call it instead of using the macro invoke, you don't have to declare this function. _PROCDLGMAIN Proc Uses EBX EDI ESI, / HWND: DWORD, WMSG: DWORD: DWORD, DWORD, LPARAM: DWORD This definition proc's statement should be unfamiliar, to repeat the Uses and the following parameters, registers under Uses Indicates that the compiler is automatically inserted to save and restore these registers, and the symbols of the row in the Masm32, indicating that the next line is the continuation of the Bank to avoid the content of the line.

转载请注明原文地址:https://www.9cbs.com/read-1430.html

New Post(0)