How to create a native window program
How to create a native window program is now useful now, because the current RAD tool is generally used, and you don't have to spend our minds to consider the details created by the windows. Indeed, generally not someone will first think of the original Method to build a general window, but you know that the original approach is great, I have a great experience, such as the window created with the RAD development tool is often very standard, if you want Create some special windows, then you have to change the window you have created. At this time, you still ask you to know how to create a native window, let alone After you know how to create a native window program, you will feel that the window is still It can be built like this, and you will also learn how to change the style of the window through the style of the control window. Ok, let me introduce how to create a native window program, the tool I use is Borland C Builder, maybe as you, and it is also a fast development tool.
1. Start C Builder (not your version) to create a new console program,
2. In the pop-up dialog box, please be sure that only C Radio Button is selected, as shown:
3, click the OK button you will see the following code
#include
#include
#pragma HDRSTOP
/ / -------------------------------------------------------------------------------------------- ---------------------------
#pragma argsused
WinApi WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW)
{
Return 0;
}
Where the Winmain function is the entry function of the window program, just like the main function in C or C , the only difference between C or C main function is optional, and the parameters of the WinMain function are fixed, this is you The program and the operating system exchange information. These parameters are represented as follows:
The first parameter hinstance indicates the handle of the application you write.
The second parameter hprevinstance indicates that your previous instance handle, the condition is to have the previous instance of your program is running, if not, you can ignore this parameter.
The third parameter LPCMDLINE is a string, and its main use is the command of the corresponding command line, as you may compile the program by the way
The fourth parameter ncmdshow controls the display of the window you created by your program
The above is only introduced the parameters of the WinMain function. This is the beginning of the creation of the native window program. There are also some almost fixed steps to complete the creation of the original window program, steps below:
1. Register you want to create a window of the window
2, create and display the display window according to the registered window class
3, handle message loop
Register You want to create a window class
Win32 provides WNDCLASS and WNDCLASSEX classes (actually structures) for us to use, you can use one of them, they are as defined below:
typedef struct _WNDCLASS {UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName;} WNDCLASS, * PWtypedef struct _WNDCLASSEX {UINT cbSize; UINT style; WNDPROC lpfnWndProc ; int cbClsExtra; int cbWndExtra; hINSTANCE hInstance; HICON hIcon; hCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName; HICON hIconSm;} WNDCLASSEX, * PWNDCLASSEX; before registering the window class should be initialized instance of the window class, then we The task is how to complete the initialization of the WNDCLASSEX instance. If this step is completed, the following things become simple. Ok, let's build a window class according to the structure of WNDCLASSEX.
code show as below:
WNDCLASSEX WNDCLSEX;
Wndclsex.cbsize = sizeof (wndclassex);
Wndclsex.style = CS_HREDRAW | CS_VREDRAW;
Wndclsex.lpfnwndproc = wndprocedure;
WNDCLSEX.CBCLSEXTRA = 0;
Wndclsex.cbWndextra = 0;
WNDCLSEX.HICON = Loadicon (NULL, IDI_Application);
WNDCLSEX.HCURSOR = loadingcursor (null, idc_arrow);
Wndclsex.hbrbackground = getStockObject (White_brush);
WNDCLSEX.LPSZMENUNUNUNAME = NULL;
Wndclsex.lpszclassName = clsname; // Form of window class name
WNDCLSEX.HINSTANCE = HINSTANCE;
WNDCLSEX.HICONSM = LoadCon (null, idi_application);
Most of the parameters in this code can be set to MSDN, I want to explain the following aspects.
WndProcedure is the callback function of the window, mainly to handle the window message:
LResult Callback Wndprocedure (HWND HWND, UINT MSG, WPARAM WPARAM, LPARAM LPARAM)
{
Switch (msg)
{
Case WM_DESTROY:
PostquitMessage (WM_QUIT);
Break;
DEFAULT:
// Treatment of messages that do not processed
Return DefWindowProc (HWND, MSG, WPARAM, LPARAM);
Return 0;
}
In addition to the WndProcedure callback function, the API function of loading loading (), loadcursor (), getStockObject () is called to complete the window icon, small icon, mouse, background color setting. About functions You can refer to MSDN to call, but also Define some constants such as CLSNAME, etc.
The current program code after completing the above steps is as follows:
#include
#include
#pragma HDRSTOP
/ / Define two yields used to represent window class names and window names
const char * clsname = "Window Class Name";
Const char * wndname = "window name";
// The callback function of the window is used to handle the window message.
LResult Callback Wndprocedure (HWND HWND, UINT MSG, WPARAM WPARAM, LPARAM LPARAM)
{
Switch (msg)
{
Case WM_DESTROY:
PostquitMessage (WM_QUIT);
Break;
DEFAULT:
Return DefWindowProc (HWND, MSG, WPARAM, LPARAM);
}
Return 0;
}
/ / -------------------------------------------------------------------------------------------- ---------------------------
#pragma argsused
WinApi WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW)
{
WNDCLASSEX WNDCLSEX;
Wndclsex.cbsize = sizeof (wndclassex);
Wndclsex.style = CS_HREDRAW | CS_VREDRAW;
Wndclsex.lpfnwndproc = wndprocedure;
WNDCLSEX.CBCLSEXTRA = 0;
Wndclsex.cbWndextra = 0;
WNDCLSEX.HICON = Loadicon (NULL, IDI_Application);
WNDCLSEX.HCURSOR = loadingcursor (null, idc_arrow);
Wndclsex.hbrbackground = getStockObject (White_brush);
WNDCLSEX.LPSZMENUNUNUNAME = NULL;
Wndclsex.lpszclassname = "Window Window Name";
WNDCLSEX.HINSTANCE = HINSTANCE;
WNDCLSEX.HICONSM = LoadCon (null, idi_application);
Return 0;
}
Registration program window
This test program window is simple, just a simple call of the RegisterClassex (& Wndclsex) function, and the parameters of the RegisterClassex function are the window classes defined above, the code is as follows:
WinApi WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW)
{
WNDCLASSEX WNDCLSEX ;. . . . . .
RegisterClassex (& WndClsex);
Return 0;
}
Create and display window
Creating a window is also very simple, just call the CREATEWINDOW function, just if you want to use the window you created in your program, you should set a window handle used to save the window, the code is as follows:
WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance,
LPSTR LPCMDLINE, INT NCMDSHOW)
{
Hwnd hwnd;
WNDCLASSEX WNDCLSEX;
.
RegisterClassex (& WndClsex);
HWND = CREATEWINDOW
CLSName,
Wndname,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
Hinstance,
NULL);
If (! hw) return 0; // If the window creates failed, exit the program
// Display the window created
Showwindow (hwnd, sw_shownormal);
UpdateWindow (HWND);
}
This completes the creation and display of the window, but when we go to run the program, we found that the window we created is flashing, the program is over, this is not what we need, how to solve this problem, actually I have already said, there is still a step without completion, then the message loop, only after the establishment of the message loop, the program window will not disappear, the reason is very simple, because the program has been in the loop, it does not have no End, so we created the window will not display.
Processing message loop:
The processing of the message loop is mainly used for the three API functions of GetMessage, TranslateMessage, and DispatchMessage. The role of GetMessage is to obtain a window message in the message queue of the program. The role of the TranslateMessage function is to process the format of the message obtained by the getMessage function. Application identification, the role of the DispatchMessage function is assigned to the application after processing, and their function prototype can refer to MSDN, which is described in detail.
// Treat message loop
While (GetMessage (& MSG, NULL, 0, 0))
{
TranslateMessage (& MSG);
DispatchMessage (& MSG);
}
Only in this way, our window will not disappear, the window program will not end, but the problem is coming again, you said that the program will not end, when I will be over, this is not telling you. Contradictory, in fact, there is no contradiction, when you click the closing button of the window, the program will be subject to the WM_DESTROY message, and we have handled it in the window's callback function WndProcedure, that is, call the postquitMessage (wm_quit) function to end the program. This process will happen that every time the program's window is subject to a window message, it is called, just here, the process is to end the program when it is affected by the WM_DESTROY message. You can comment about the code that processes the WM_DESTROY message, you will see if you look at the program! When I test results, it just closed the window, the program did not end, you can also see it in the task management! The above speech is just simply expressing how to create a native window program. In fact, there are many things that are going to learn. If you say the message queue when you process the message loop, how Window is handled by these messages, this Require readers to slowly experience in future learning! If you are interested in this program, you can download the program to the http://www.zccfamily.com/zqget/ website to test your smile.! If you have any questions, you can contact me, my email: zqget123 @ sina. COM