A simple window to create a compilation tutorial

Source: Internet
Author: User

In this lesson we'll write a Windows program that displays a standard window on the desktop.

Theory:

In Windows programs, you need to invoke a large number of standard Windows Gui functions while writing a graphical user interface. In fact, this is good for users and programmers, for users, facing the same set of standard Windows, the operation of these windows are the same, so the use of different applications without learning operations. For programmers, the Gui source code has been rigorously tested by Microsoft and can be used at any time. Of course, it is still difficult for programmers to write programs specifically. In order to create a windows-based application, the specification must be strictly adhered to. This is not difficult, as long as you use modular or object-oriented programming methods.

I'll list a few steps to display a window on the desktop:

Get the handle of your application (required);

Get command-line arguments (optional if you want to get arguments from the command line);

Registers a window class (required unless you use a Windows predefined window class, such as MessageBox or dialog box;

Generate window (required);

Display the window on the desktop (required unless you do not want to show it immediately);

Refresh window client area;

Enter an infinite loop to get the window message;

If a message arrives, it is handled by the window callback function that is responsible for the window;

If the user closes the window, exit processing.

Compared to Single-user DOS programming, the program framework structure under Windows is quite complex. But Windows and DOS are very different on the system architecture. Windows is a multitasking operating system, so multiple applications work in tandem with each other. This requires that Windows programmers strictly adhere to the programming specifications and develop a good programming style.

Content:

Here is the source code for our simple window program. Before entering into the complex details, I will sketchy to point out several points:

You should put the declarations of all constants and structs that you want to use in your program into a header file, and include the header file at the beginning of the source program. Doing so will save you a lot of time and avoid knocking the keyboard again and again. At present, the most perfect header file is written by Hutch, you can download to hutch or my website. You can also define your own constants and structs, but it's best to put them in a separate header file

With the INCLUDELIB directive, include the library file that your program refers to, such as: If your program calls "MessageBox", you should include the following line in your source file: Includelib user32.lib this statement tells MASM Your program will use some of the introduction libraries. If you're not just referencing a library, simply add the Includelib statement and don't worry about how the linker handles so many libraries, as long as the link switches/libpath indicate the path to the library.

In other places, when you define function prototypes, constants, and structs in a header file, you strictly maintain consistency with the definitions in the header file, including capitalization. This saves you a lot of time when querying function definitions;

When compiling, link use makefile file, eliminates repeatedly keystrokes.

.386
. Model Flat,stdcall
Option Casemap:none
Include \masm32\include\windows.inc
Include \masm32\include\user32.inc
Includelib \masm32\lib\user32.lib; Calls to functions in User32.lib and Kernel32.lib
Include \masm32\include\kernel32.inc
Includelib \masm32\lib\kernel32.lib
WinMain Proto:D Word,:D Word,:D Word,:D Word
. DATA; Initialized data
ClassName db "Simplewinclass", 0; The name of our window class
AppName db "Our", 0; The name of our windows
. DATA?; Uninitialized data
HInstance hinstance?; Instance Handle of our
CommandLine LPSTR?
. CODE; Here begins our code
Start
Invoke GetModuleHandle, NULL; Get the instance handle.
; Under Win32, hmodule==hinstance mov hinstance,eax
MOV hinstance,eax
Invoke GetCommandLine; Get the command line. You don ' t have to call this function IF
; Your program doesn ' t process the command line.
MOV commandline,eax
Invoke WinMain, Hinstance,null,commandline, Sw_showdefault; Call the main function
Invoke ExitProcess, eax; Quit our program. The exit code is returned in EAX from WinMain.
WinMain proc Hinst:hinstance,hprevinst:hinstance,cmdline:lpstr,cmdshow:dword
Local wc:wndclassex; Create local variables on stack
Local msg:msg
Local Hwnd:hwnd
mov wc.cbsize,sizeof wndclassex; Fill values in the members of WC
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; Register our window class
Invoke createwindowex,null,\
ADDR classname,\
ADDR appname,\
Ws_overlappedwindow,\
Cw_usedefault,\
Cw_usedefault,\
Cw_usedefault,\
Cw_usedefault,\
Null,\
Null,\
Hinst,\
Null
MOV hwnd,eax
Invoke ShowWindow, Hwnd,cmdshow; Display our windows on desktop
Invoke UpdateWindow, HWND; Refresh the client area
. While TRUE; Enter message loop
Invoke GetMessage, ADDR msg,null,0,0
. Break. IF (!EAX)
Invoke TranslateMessage, ADDR msg
Invoke DispatchMessage, ADDR msg
. Endw
MOV eax,msg.wparam; Return exit code in EAX
Ret
WinMain ENDP
WNDPROC proc Hwnd:hwnd, Umsg:uint, Wparam:wparam, Lparam:lparam
. IF Umsg==wm_destroy; If the user closes our Windows
Invoke Postquitmessage,null; Quit our Application
. ELSE
Invoke Defwindowproc,hwnd,umsg,wparam,lparam; Default Message Processing
Ret
. ENDIF
XOR Eax,eax
Ret
WndProc ENDP
End Start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.