C ++ directx11 Development NOTE 1: Create a Win32 Application Form

Source: Internet
Author: User

In fact, this is quite simple and common. If you don't pay special attention to it, it is sometimes not that easy. I haven't written a blog for a long time, and I don't know how to describe it. In short, I recently decided to learn about DirectX development. I downloaded and installed the June 2010 SDK. It should be the latest version. That is to say, it involves the development of DirectX 11, so there is basically no place to look for the tutorial, not to mention I am a beginner, for those DirectX 9. X and 10. X is not a problem, so I want to record my learning process.

 

Let's take a look.ProgramWhen opening the EXE application, the latest method to execute is the main function. Let's take a look at the format of this function:

Int Winapi wwinmain (hinstance, hinstance hprevinstance, lpwstr lpcmdline, Int Ncmdshow)

Parameter description:

    1. Hinstance: the handle of the current application instance;
    2. Hprevinstance: the handle of the previous instance of the program. If the current program is the first instance, this parameter is null. You can use this parameter to determine whether the program is running, but it generally does not matter;
    3. Lpcmdline: indicates the command line string;
    4. Ncmdshow: the specified program is displayed as a window or icon.

From the above, we can understand the basic method of Windows programming. In VC ++, we can directly call windows APIs without the need for proxy (alias Declaration) Like. net ). Of course, C ++ will be a little more complicated than. NET programming, but it is not so disgusted to read more C ++ programs. It is quite practical and time-consuming. After knowing this function, of course, you need to do something in this function. For example, to display a window, there must be an icon in the window. Of course, this icon should be obtained from the resource, next we will look at how to add resources. In fact, it is simple and commonCodeIn the same way, right-click the project folder-> Add-> resource, find a. ICO icon, add it to the Resource Directory, and set the ID to idi_mainicon in "attribute.

 

To display the window, first initialize the program. We can declare a method using the following code:

Hresult initwindow (hinstance, Int Ncmdshow );

The function is defined as follows:

1 Hresult initwindow (hinstance, Int Ncmdshow)
2 {
3 // Register class
4 Wndclassex wcex;
5 Wcex. cbsize =   Sizeof (Wndclassex );
6 Wcex. Style = Cs_hredraw | Cs_vredraw;
7 Wcex. lpfnwndproc = Wndproc;
8 Wcex. cbclsextra =   0 ;
9 Wcex. cbwndextra =   0 ;
10 Wcex. hinstance = Hinstance;
11 Wcex. hicon = Loadicon (hinstance, (lpctstr) idi_mainicon );
12 Wcex. hcursor = Loadcursor (null, idc_arrow );
13 Wcex. hbrbackground = (Hbrush) (color_window +   1 );
14 Wcex. lpszmenuname = NULL;
15 Wcex. lpszclassname = L " Tutorialwindowclass " ;
16 Wcex. hiconsm = Loadicon (wcex. hinstance, (lpctstr) idi_mainicon );
17 If ( ! Registerclassex ( & Wcex ))
18 Return E_fail;
19
20 // Create window
21 G_hinst = Hinstance;
22 Rect RC = { 0 , 0 , 640 , 480 };
23 Adjustwindowrect ( & RC, ws_overlappedwindow, false );
24 G_hwnd = Createwindow (L " Tutorialwindowclass " , L " Direct3d 11 tutorial 1: direct3d 11 Basics " , Ws_overlappedwindow,
25 Cw_usedefault, cw_usedefault, RC. Right - RC. Left, RC. Bottom - RC. Top, null, null, hinstance,
26 Null );
27 If ( ! G_hwnd)
28 Return E_fail;
29
30 Showwindow (g_hwnd, ncmdshow );
31
32 Return S_ OK;
33 }

From the code above, we can see that the first thing to do is to register the window class, which involves a wndclassex struct. here we need to note that Windows programs are mainly driven by events, here we need a process function for message processing. Its definition is as follows:

Lresult callback wndproc (hwnd, uint, wparam, lparam );

Parameter description:

    1. Hwnd: handle of the current form;
    2. Uint: the message definition value. Each message has a unique value, which can be customized;
    3. Wparam: contains the Virtual Value of the button;
    4. Lparam: contains the context of the button;

For more details, you can query msdn by yourself. Here we will not continue. The definition code of the process function (also called the callback function) is as follows, mainly to process messages:

1 Lresult callback wndproc (hwnd, uint message, wparam, lparam)
2 {
3 Paintstruct pS;
4 HDC;
5
6 Switch (Message)
7 {
8 Case Wm_paint:
9 HDC = Beginpaint (hwnd, & PS );
10 Endpaint (hwnd, & PS );
11 Break ;
12
13 Case Wm_destroy:
14 Postquitmessage ( 0 );
15 Break ;
16
17 Default :
18 Return Defwindowproc (hwnd, message, wparam, lparam );
19 }
20
21 Return   0 ;
22 }

 

From the above we can see that there is a loadicon method, that is, to load the icon and get the handle of the icon, you can set the window icon. If the minimize icon is not set, the window icon is used by default. The usage of loadcursor is the same as that of the icon, which can be seen in the code. After registering a form class, you must create a form and display it. Of course, you also need to process the message. The Code is as follows:

1 MSG msg = { 0 };
2 While (Wm_quit ! = MSG. Message)
3 {
4 If (Peekmessage ( & MSG, null, 0 , 0 , Pm_remove ))
5 {
6 Translatemessage ( & MSG );
7 Dispatchmessage ( & MSG );
8 }
9 Else
10 {
11 Render ();
12 }
13 }

in this way, the entire process of creating a form is basically completed. In fact, this is the basis of VC ++ programming and DirectX programming is not much related, because I do not pay much attention to it at ordinary times, today, I checked the code. OK. This is the opening note of directx11 programming. It will be continued ......

Related 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.