How to: Creating Win32 Applications (C++)

来源:互联网 发布:普朗克常数算法 编辑:程序博客网 时间:2024/05/22 06:42

Transcript

I'm Kathleen McGrath. In this presentation you're going to learn how to create a WIN 32 application using Visual C++. The Win32 API (also known as the Windows API) is a C-based framework for creating Windows applications, and has been around since Windows 1.0.

In this demonstration, I'll show you how to create a simple Win32 application that displays "Hello, World!" in a window.

Because the procedure steps are identical for all Win32 applications, you can use this code as a skeleton to create any other Win32 application.

Create the Project

I'll create a new project by clicking File, New and then New Project. In the New Project dialog box, I'll expand Visual C++, and click Win32. Then I'll select the Win32 Project. I'll name this win32app, saving it to the default location, and then click OK.

In the Win32 Application Wizard, I'll click Next. For Application type, I'll select Windows application. For Additional options I'll select Empty project, and then I'll click Finish to create the project.

Now I'll add a C++ file to the project by selecting Add New Item... from the Project menu. And then I'll select C++ File (.cpp). I'll name this HelloWorldWin32, and click Add. The file HelloWorldWin32.cpp is added to the project.

As you know, every C and C++ application must have a main function. This function is the starting point for the application. Similarly, in a Win32 application, every application must have a WinMain function.

So I'll add it here:

int WINAPI WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

}

In addition to WinMain, each Win32 application must also have a second function which is usually called WndProc, which stands for window procedure. I'll add the WndProc function here:

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM)

{

}

This function handles any messages that your application receives from the operating system.

For example, imagine that you've created a dialog box that has an OK button. When the user clicks that button, the operating system sends your application a message, which lets you know that a user pressed this button. The WndProc function is responsible for responding to that event; for example, it might close the dialog box.

Add Functionality to WinMain

Now I’ll add some functionality to the WinMain function.

Since I'm going to show "Hello, World" in a window, first I'm going to define the window UI. Then I need to register the window, create an instance, and display it. To catch events, I'll set up a message loop to listen to messages from the new window. Finally, I'll define my text message and use the paint API to display the message when the window UI is painted.

I'll add a window class structure of type WNDCLASSEX inside WinMain:

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;

wcex.lpfnWndProc = WndProc;

wcex.cbClsExtra = 0;

wcex.cbWndExtra = 0;

wcex.hInstance = hInstance;

wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

wcex.lpszMenuName = NULL;

wcex.lpszClassName = szWindowClass;

wcex.hIconSm = LoadIcon(wcex.hInstance,

MAKEINTRESOURCE(IDI_APPLICATION));

This window class structure contains information about your window, such as the application's icon, or the background color of the window.

Notice that for style, we use CS_HREDRAW | CS_VREDRAW. This means that the window will automatically be redrawn when the horizontal or vertical size changes.

Next, I'll add a RegisterClassEx function to register the window class with the operating system:

if (!RegisterClassEx(&wcex))

{

MessageBox(NULL,

_T("Call to RegisterClassEx failed!"),

_T("Win32 Guided Tour"),

NULL);

return 1;

}

Next, I'll create the window using the CreateWindow function.

hInst = hInstance;

HWND hWnd = CreateWindow(

szWindowClass,

szTitle,

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT,

500, 100,

NULL,

NULL,

hInstance,

NULL

);

if (!hWnd)

{

MessageBox(NULL,

_T("Call to CreateWindow failed!"),

_T("Win32 Guided Tour"),

NULL);

return 1;

}

This function returns an HWND, which is a handle to a window.

To display the window on the screen, we'll use this code:

// The parameters to ShowWindow explained:

// hWnd: the value returned from CreateWindow

// nCmdShow: the fourth parameter from WinMain

ShowWindow(hWnd,

nCmdShow);

UpdateWindow(hWnd);

It won't display anything yet, because we haven't added functionality to the wndProc function. Next, we need a message loop:

MSG msg;

while (GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return (int) msg.wParam;

This message loop listens for messages that the operating system sends. When the application receives a message, the message is dispatched to the WndProc function to handle it.

Adding Functionality to WndProc

Typically, you use a switch statement within the WndProc function. When the application receives a message that a portion of your application's window must be updated, called a WM_PAINT message, you can handle it with the BeginPaint and EndPaint functions.

PAINTSTRUCT ps;

HDC hdc;

TCHAR greeting[] = _T("Hello, World!");

switch (message)

{

case WM_PAINT:

hdc = BeginPaint(hWnd, &ps);

// Here your application is laid out.

// For this introduction, we just print out "Hello, World!"

// in the top left corner.

TextOut(hdc,

5, 5,

greeting, _tcslen(greeting));

// End application-specific layout section.

EndPaint(hWnd, &ps);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

break;

}

return 0;

}

Within these functions, you add the code to handle the event.

So in the case that a WM_PAINT message is received, BeginPaint occurs, and we display "Hello World" inside the window by using the TextOut function., and then EndPaint occurs.

Your application will typically handle many other messages, such as WM_CREATE and WM_DESTROY. I'll add a WM_DESTROY case to make the code more complete. WM_DESTROY is called when the window is closed and PostQuitMessage unregisters the window with the operating system and closes the window:

case WM_DESTROY:

PostQuitMessage(0);

break;

Next, I’ll add some code at the top of my file that includes the Header files and some Global variables needed to run the application.

To build the application, I'll click Build and select Build Solution. The application compiled without any errors, so now I'll run the application by pressing F5.

A simple window with the text "Hello, World!" is displayed on the screen near the upper-left corner.

For More Information

You can get more information about developing Visual C++ applications in the Visual C++ Help. You can find other resources such as technical articles, samples, blogs, and videos at the Visual C++ Developer Center. Just go to msdn.microsoft.com/visualc.