Getting into the world of Windows programming-----Introductory article

Source: Internet
Author: User
Tags configuration settings

1 basic types of Windows Programming foundation 1.1WIN32 Applications

1) Console Program

Windows Forms that do not need to be intact can be displayed using a DOS form

2) Win32 Form program

A program that includes a form that can interact with a program through a form

3) Win32 Library Program

Provide existing code for use by other programs

Dynamic libraries (DLLs): Can be loaded at run time.

Static library (LIB): is the program that is used to compile the link. Become part of the current program.

1.2 Header files and libraries 1.2.1 Header files

The main header file, windows.h, includes definitions commonly used by Windows, among others, and some other header files. Example:

1. Windef.h: Define the data type

2. Winbase.h: Defines the correlation function of kernel

3, WinGDI.h: Defines the drawing and the document

4, Winuser.h: Form and space

5. WinNT.h: Provides Unicode support

1.2.2 Library

1, Kernel32.lib: Provide process thread memory and so on API function definition

2. User32.lib: API functions for forms and interfaces

3, Gdi32.lib: Provide drawing, text and other API function support

1.1.1 Instance HelloWorld development environment VS2010. Errors may be reported during compilation, which may be a coding problem: Some configuration settings are required: Project-Properties---Configuration Properties-General: CharSet set to not set
/*file:helloworld.cpp *auth:sjin *date:20140519 *mail: [Email protected] */#include <iostream> #include <wi Ndows.h>int WINAPI WinMain (                     hinstance hinstance,       //handle to current instance                     hinstance hPrevInstance,      //Handle to previous instance                     LPSTR lpCmdLine,           //command line                     int nCmdShow                    //show state                     ) {MessageBox (NULL, "Hello World:", "first Win32 Test ", MB_OK); return 0;}

Execution Result:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvc2ppbl8xmze0/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

1.1 Writing the first form program

Process related processes such as the following:

1. Form entry function WinMain

2. Form processing function

3. Registration Form Type

4. Create a form

5. Display Form

6. Message Processing

7. Program exit

Look at the following code:

/*file:helloworld.cpp *auth:sjin *date:20140519 *mail: [Email protected] */#include <iostream> #include <wi Ndows.h>using namespace Std; hinstance G_hinst = NULL;//2 form processing function/* Function function: This function is an application-defined function.

It processes the message sent to the form * when the form processes a message message, the system calls the function * Parameters: *hwnd: A handle to the form.

*UMSG: Specifies the message type.

*wparam: Specifies the remaining, message-specific information. The content of this parameter is related to the Umsg reference value. *iparam: Specifies the remaining, message-specific information. The content of this parameter is related to the Umsg reference value.

*/lresult CALLBACK WndProc (HWND hwnd,/**/uint nmsg,wparam wparam,lparam iparam) {switch (nmsg) {case wm_destroy:// The form destroys the message//sends the message Exit function PostQuitMessage (0); return 0;default:break;} Call return DefWindowProc (Hwnd,nmsg,wparam,iparam);} 3 Register Form Type/* * * */bool myregister (LPSTR pszclassname) {wndclass WC = {'};wc.style '} = Cs_vredraw | Cs_hredraw;wc.lpfnwndproc = Wndproc;wc.cbclsextra = 0;wc.cbwndextra = 0;wc.hinstance = G_hInst;wc.hIcon = LoadIcon (G_hinst,makeintresource (100));; Wc.hcursor = Null;wc.hbrbackground = (hbrush) (color_btnface); wc.lpszmenuname = Null;wc.lpszclassname = PszClass Name; ATOM Natom = registerclass (&AMP;WC), if (0 = = Natom) {MessageBox (NULL, "Register Failed", "error!", MB_OK | mb_iconwarning);} Else{//messagebox (NULL, "Register successed", "successed!", MB_OK);} return TRUE;} 4 Form Creation hwnd Mycreatewindow (LPSTR pszclassname) {//Create form HWND hwnd =createwindow (Pszclassname, "Hellownd", Ws_ Overlappedwindow,100,100,300,500,null,null,g_hinst,null); if (0 = = hwnd) {MessageBox (NULL, "CreateWindow Failed", "error!", MB_OK | mb_iconwarning);} Else{//messagebox (NULL, "CreateWindow successed", "successed!", MB_OK);} return hwnd;} 5 show form void Displaywnd (HWND hwnd) {//Display ShowWindow (hwnd,sw_show);//Refresh UpdateWindow (hwnd);} 6 Message processing void message () {MSG msg = {'};//') messages loop processing. Gets the message while (GetMessage (&msg,null,0,0)) {//Dispatches the message dispatchmessage (&AMP;MSG);/* Runs the form handler function */}}//1, entry function int WINAPI WinMain (hinstance hinstance,//handle to current instance hinstance Hprevi Nstance,//handle to previous instance LPSTR lpCmdLine,//command line int nCmdShow//show state) {g_hinst = hinstance;//brochure form type Myregister ("My first Win32 ");//The form of the common register type HWND hwnd = Mycreatewindow (" My first Win32 ");//Display Form Displaywnd (HWND);//message processing messages (); return 0;}


Resource file

/*file:helloworld.rc *auth:sjin *date:20140519 *mail: [email protected] */100 ICON "2222.ico"

Pictures such as the following:


Getting into the world of Windows programming-----Introductory article

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.