Windows Programming basics _ 2 a complete program

Source: Internet
Author: User
Windows development knowledge 1) the most basic concept in Windows is the window. Each foreground program has at least one window, and one window is what you can see. For example, QQ has the following Logon Windows. Basically, you can see a Windows window in windows, which is also a basic element (GUI program) used for direct user interaction in windows ). 2) handle window, file, socket, semaphore, pipeline, mail slot )...... Are basic objects in windows.
For example, we need something that can reference these objects. The thing that references these objects is handle ). A handle is like a remote control for a TV. A remote control can be used to better operate the TV without worrying about the details of the internal implementation. The handle is also like this. You can use a handle to better operate windows objects, it does not need to be related to its internal implementation details. In fact, you can only operate windows objects through a handle. For example, if you want to operate a thread, you can see the suspendthread prototype as follows: (the first parameter is a thread handle)
SyntaxDWORD WINAPI SuspendThread(  __in  HANDLE hThread);

For example, if you want to read a file, the prototype of the readfile function is as follows: (the first parameter is the file handle .)

BOOL WINAPI ReadFile(  __in         HANDLE hFile,  __out        LPVOID lpBuffer,  __in         DWORD nNumberOfBytesToRead,  __out_opt    LPDWORD lpNumberOfBytesRead,  __inout_opt  LPOVERLAPPED lpOverlapped);

............ There are many other similar examples. Basically, to operate windows objects, You need to point to their handles. The handles are syntactically the same as pointers in C, but they are completely different in syntax.
3) process is defined in some operating system textbooks as: A process is a running process of a program. This definition is actually correct, but it is also wrong. Why? There are many other operating systems before Windows, such as OS 360, Unix ...... When the concept of process was proposed, windows was not born yet. However, in windows, the process concept is completely different. In Windows, the process is the basic unit of program isolation. Every 32-bit application runs in its own process space, processes only provide 4G virtual address space for this program, and different processes do not interfere with each other (of course, we will talk about communication between processes ). 4) In Windows, the thread actually runs the program. The thread runs in the 4G virtual address space provided by the process and runs the. Text Segment of the PE file. That is to say, the thread is the real execution body. Basically normal Windows program code to create a project, or follow the previous method, in the previous section, and then enter the following code:

#include <windows.h>LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){    static TCHAR szAppName[] = TEXT ("BossJue");    HWND         hwnd;    MSG          msg;    WNDCLASSEX   wndclassex = {0};    wndclassex.cbSize        = sizeof(WNDCLASSEX);    wndclassex.style         = CS_HREDRAW | CS_VREDRAW;    wndclassex.lpfnWndProc   = WndProc;    wndclassex.cbClsExtra    = 0;    wndclassex.cbWndExtra    = 0;    wndclassex.hInstance     = hInstance;    wndclassex.hIcon         = LoadIcon (NULL, IDI_APPLICATION);    wndclassex.hCursor       = LoadCursor (NULL, IDC_ARROW);    wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);    wndclassex.lpszMenuName  = NULL;    wndclassex.lpszClassName = szAppName;    wndclassex.hIconSm       = wndclassex.hIcon;    if (!RegisterClassEx (&wndclassex))    {        MessageBox (NULL, TEXT ("RegisterClassEx failed!"), szAppName, MB_ICONERROR);        return 0;    }    hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW,                   szAppName,                   TEXT ("WindowTitle"),                  WS_OVERLAPPEDWINDOW,                  CW_USEDEFAULT,                   CW_USEDEFAULT,                   CW_USEDEFAULT,                   CW_USEDEFAULT,                   NULL,                   NULL,                   hInstance,                  NULL);       ShowWindow (hwnd, iCmdShow);    UpdateWindow (hwnd);    while (GetMessage (&msg, NULL, 0, 0))    {        TranslateMessage (&msg);        DispatchMessage (&msg);    }    return msg.wParam;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){static const LPTSTR text = TEXT("Hello,World");    HDC hdc;    PAINTSTRUCT ps;    switch (message)    {    case WM_CREATE:        return (0);    case WM_PAINT:        hdc = BeginPaint (hwnd, &ps);        TextOut (hdc, 0, 0, text, lstrlen(text));        EndPaint (hwnd, &ps);        return (0);    case WM_DESTROY:        PostQuitMessage (0);        return (0);    }    return DefWindowProc (hwnd, message, wParam, lParam);}
Run

Press Ctrl + F5 and you may encounter the following link error:
Don't worry. The incorrect setting is because Windows regards this program as a console program, and the default portal function of the console program is main, which is not defined here, instead, it defines an entry function called winmain, so the linker does not recognize it, but what we want is that the compiler considers our program as a GUI program, the GUI program's entry function is winmain (if you use # pragma
Comment (linker ............) You can also modify it. I wrote this method in the first chapter. Of course, we can also directly set it in Visual Studio ide by opening the project properties, and then setting it as follows: press Ctrl + F5, is there a window shown below? Yes, that's it. What you see is the compilation of a normal Windows program. The Code explains how to compile a Windows program. Three steps are required. The first step is to register a window class, and the second step is to create a window, the third step is to compile the message response function. The first and second steps are basically fixed. Apart from the individual parameters that need to be adjusted, the most important step is step 3, in a Windows program, the most code is in step 3. In Windows applications, if we want to receive user input and display some information in the window ...... We all need to process related messages. in windows, we need to notify the program of all events related to this program. As for how to process these messages, it is our own business. The above registerclassex
(& Wndclassex) is actually the registration window class createmediawex (ws_ex_overlappedwindow,
Szappname,
Text ("windowtitle "),
Ws_overlappedwindow,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
Cw_usedefault,
Null,
Null,
Hinstance,
Null); it is actually the creation window. lresult callback wndproc (hwnd, uint message, wparam, lparam) is actually the message processing function,, this message processing function does not need to be called by ourselves. When there is a message, Windows will call this function for processing. We only need to write the processing code. However, we do not need to manually call the code written (this may be the reason for the callback function ). The rest can read msdn if you still don't understand it. For example, if you are not familiar with createwindow, you can search for createwindow directly in msdn. msdn will not only tell you how to use this function, it will also tell you what the message loop is, and I will not elaborate on it here.

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.