好文档 - 专业文书写作范文服务资料分享网站

Visual studio 2012 C++ 指导教程

天下 分享 时间: 加入收藏 我要投稿 点赞

10. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESO

URCE(IDI_APPLICATION));

11. wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 12. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 13. wcex.lpszMenuName = NULL;

14. wcex.lpszClassName = szWindowClass;

15. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEIN

TRESOURCE(IDI_APPLICATION));

有关此结构的各字段的信息,请参见 WNDCLASSEX。

16. 现在您已经创建一个窗口类,接下来必须将其注册。 使用 RegisterClassEx 函数,并将

窗口类结构作为参数进行传递。

17. 18. 19. 20. 21. 22. 23. 24. 25. if (!RegisterClassEx(&wcex)) {

MessageBox(NULL,

_T(\ _T(\ NULL);

return 1; }

26. 现在可以创建一个窗口。 使用 CreateWindow 函数。

27. static TCHAR szWindowClass[] = _T(\

28. static TCHAR szTitle[] = _T(\

on\29.

30. // The parameters to CreateWindow explained: 31. // szWindowClass: the name of the application

32. // szTitle: the text that appears in the title bar 33. // WS_OVERLAPPEDWINDOW: the type of window to create

34. // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y) 35. // 500, 100: initial size (width, length) 36. // NULL: the parent of this window

37. // NULL: this application does not have a menu bar 38. // hInstance: the first parameter from WinMain 39. // NULL: not used in this application 40. HWND hWnd = CreateWindow( 41. szWindowClass, 42. szTitle,

43. WS_OVERLAPPEDWINDOW,

44. CW_USEDEFAULT, CW_USEDEFAULT, 45. 500, 100, 46. NULL,

47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. NULL,

hInstance, NULL );

if (!hWnd) {

MessageBox(NULL,

_T(\ _T(\ NULL);

return 1; }

此函数返回 HWND,它是某个窗口的句柄。 有关更多信息,请参见 Windows 数据类型。

60. 现在,使用下列代码来显示窗口。

61. 62. 63. 64. 65. 66. // The parameters to ShowWindow explained: // hWnd: the value returned from CreateWindow // nCmdShow: the fourth parameter from WinMain ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

此时,所显示的窗口不会有太多内容,因为您尚未实现 WndProc 函数。

67. 现在添加一个消息循环以侦听操作系统发送的消息。 如果应用程序收到一条消息,则此

循环会将该消息调度至WndProc 函数以接受处理。 消息循环类似于下列代码。

68. 69. 70. 71. 72. 73. 74. 75. MSG msg;

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

TranslateMessage(&msg); DispatchMessage(&msg); }

return (int) msg.wParam;

有关消息循环中各结构和函数的更多信息,请参见 MSG、GetMessage、TranslateMessage 和DispatchMessage。 此时,WinMain 函数应与下列代码类似。

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow) {

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));

if (!RegisterClassEx(&wcex)) {

MessageBox(NULL,

_T(\ _T(\ NULL);

return 1; }

hInst = hInstance; // Store instance handle in our global variable

// The parameters to CreateWindow explained: // szWindowClass: the name of the application

// szTitle: the text that appears in the title bar // WS_OVERLAPPEDWINDOW: the type of window to create // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)

// 500, 100: initial size (width, length) // NULL: the parent of this window

// NULL: this application dows not have a menu bar // hInstance: the first parameter from WinMain // NULL: not used in this application

HWND hWnd = CreateWindow( szWindowClass, szTitle,

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL,

hInstance, NULL );

if (!hWnd) {

MessageBox(NULL,

_T(\ _T(\ NULL);

return 1; }

// The parameters to ShowWindow explained: // hWnd: the value returned from CreateWindow // nCmdShow: the fourth parameter from WinMain ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

// Main message loop: MSG msg;

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

TranslateMessage(&msg); DispatchMessage(&msg); }

return (int) msg.wParam; }

向 WndProc 函数添加功能

1. 若要启用 WndProc 函数来处理应用程序所收到的消息,请实现 switch 语句。

要处理的第一条消息是 WM_PAINT 消息。 如果必须更新所显示的应用程序窗口的一部分,该应用程序就会收到此消息。 (首次显示该窗口时,必须将其全部更新。) 若要处理 WM_PAINT 消息,请首先调用 BeginPaint,然后处理用于布局该窗口中的文本、按钮和其他控件的所有逻辑,再调用 EndPaint。 对于此应用程序,开始调用和结束调用之间的逻辑会在窗口中显示字符串“Hello, World!”。 在下列代码中,请注意 TextOut 函数用于显示该字符串。

PAINTSTRUCT ps; HDC hdc;

TCHAR greeting[] = _T(\

switch (message) {

case WM_PAINT:

hdc = BeginPaint(hWnd, &ps);

// Here your application is laid out.

// For this introduction, we just print out \rld!\

// in the top left corner. TextOut(hdc, 5, 5,

greeting, _tcslen(greeting));

// End application-specific layout section.

EndPaint(hWnd, &ps); break; }

2. 应用程序通常会处理许多其他消息,例如 WM_CREATE 和 WM_DESTROY。 下列代码展

示了一个基本但完整的 WndProc 函数。

3. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM

wParam, LPARAM lParam) 4. {

5. PAINTSTRUCT ps; 6. HDC hdc;

7. TCHAR greeting[] = _T(\8.

9. switch (message) 10. {

11. case WM_PAINT:

12. hdc = BeginPaint(hWnd, &ps); 13.

14. // Here your application is laid out.

Visual studio 2012 C++ 指导教程

10.wcex.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_APPLICATION));11.wcex.hCursor=LoadCursor(NULL,IDC_ARROW);12.wcex.hbrBackground=(
推荐度:
点击下载文档文档为doc格式
64vik3yze68mpoj7oh4q
领取福利

微信扫码领取福利

微信扫码分享