DirectX SDK

Step 1: Create a Window

[Visual Basic]

The information in this section pertains only to applications written in C and C++. See Direct3D Immediate Mode Visual Basic Tutorials.

[C++]

The first thing any Windows® application must do when it is executed is create an application window to display a user interface. Keeping with this, when Triangle begins execution at its WinMain function, it uses the following code to perform window initialization:

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
    // Register the window class
    WNDCLASS wndClass = { CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInst,
                          LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN_ICON)),
                          LoadCursor(NULL, IDC_ARROW), 
                          (HBRUSH)GetStockObject(WHITE_BRUSH), NULL,
                          TEXT("Render Window") };
    RegisterClass( &wndClass );
 
    // Create our main window
    HWND hWnd = CreateWindow( TEXT("Render Window"),
                              TEXT("D3D Tutorial: Drawing One Triangle"),
                              WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
                              CW_USEDEFAULT, 300, 300, 0L, 0L, hInst, 0L );
    ShowWindow( hWnd, SW_SHOWNORMAL );
    UpdateWindow( hWnd );

The preceding code is standard Windows programming, covered here mostly for thoroughness. The sample starts by defining and registering a window class called "Render Window." The window class is defined to redraw on size events, to use an application-provided icon as a resource, and to have a white background. After the class is registered, the code creates a basic top-level window that uses the registered class, with a client area of 300 pixels wide by 300 pixels tall, and has no menu or child windows. The sample uses the WS_OVERLAPPEDWINDOW window style to create a window that includes minimize, maximize, and close boxes common to windowed applications. (If the sample were to run in full-screen mode, the preferred window style is WS_EX_TOPMOST.) Once the window is created, the code calls standard Win32® functions to show and update the window.

With the application window ready, you can begin setting up the essential DirectX® objects, which is the topic of Step 2: Initialize System Objects.