1// HelloWindowsDesktop.cpp
2// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
3
4#include <windows.h>
5#include <stdlib.h>
6#include <string.h>
7#include <tchar.h>
8
9// Global variables
10
11// The main window class name.
12static TCHAR szWindowClass[] = _T("DesktopApp");
13
14// The string that appears in the application's title bar.
15static TCHAR szTitle[] = _T("Windows Desktop Guided Tour Application");
16
17HINSTANCE hInst;
18
19// Forward declarations of functions included in this code module:
20LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
21
22int CALLBACK WinMain(
23 _In_ HINSTANCE hInstance,
24 _In_opt_ HINSTANCE hPrevInstance,
25 _In_ LPSTR lpCmdLine,
26 _In_ int nCmdShow
27)
28{
29 WNDCLASSEX wcex;
30
31 wcex.cbSize = sizeof(WNDCLASSEX);
32 wcex.style = CS_HREDRAW | CS_VREDRAW;
33 wcex.lpfnWndProc = WndProc;
34 wcex.cbClsExtra = 0;
35 wcex.cbWndExtra = 0;
36 wcex.hInstance = hInstance;
37 wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
38 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
39 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
40 wcex.lpszMenuName = NULL;
41 wcex.lpszClassName = szWindowClass;
42 wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
43
44 if (!RegisterClassEx(&wcex))
45 {
46 MessageBox(NULL,
47 _T("Call to RegisterClassEx failed!"),
48 _T("Windows Desktop Guided Tour"),
49 NULL);
50
51 return 1;
52 }
53
54 // Store instance handle in our global variable
55 hInst = hInstance;
56
57 // The parameters to CreateWindow explained:
58 // szWindowClass: the name of the application
59 // szTitle: the text that appears in the title bar
60 // WS_OVERLAPPEDWINDOW: the type of window to create
61 // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
62 // 500, 100: initial size (width, length)
63 // NULL: the parent of this window
64 // NULL: this application does not have a menu bar
65 // hInstance: the first parameter from WinMain
66 // NULL: not used in this application
67 HWND hWnd = CreateWindow(
68 szWindowClass,
69 szTitle,
70 WS_OVERLAPPEDWINDOW,
71 CW_USEDEFAULT, CW_USEDEFAULT,
72 500, 100,
73 NULL,
74 NULL,
75 hInstance,
76 NULL
77 );
78
79 if (!hWnd)
80 {
81 MessageBox(NULL,
82 _T("Call to CreateWindow failed!"),
83 _T("Windows Desktop Guided Tour"),
84 NULL);
85
86 return 1;
87 }
88
89 // The parameters to ShowWindow explained:
90 // hWnd: the value returned from CreateWindow
91 // nCmdShow: the fourth parameter from WinMain
92 ShowWindow(hWnd,
93 nCmdShow);
94 UpdateWindow(hWnd);
95
96 // Main message loop:
97 MSG msg;
98 while (GetMessage(&msg, NULL, 0, 0))
99 {
100 TranslateMessage(&msg);
101 DispatchMessage(&msg);
102 }
103
104 return (int) msg.wParam;
105}
106
107// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
108//
109// PURPOSE: Processes messages for the main window.
110//
111// WM_PAINT - Paint the main window
112// WM_DESTROY - post a quit message and return
113LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
114{
115 PAINTSTRUCT ps;
116 HDC hdc;
117 TCHAR greeting[] = _T("Hello, Windows desktop!");
118
119 switch (message)
120 {
121 case WM_PAINT:
122 hdc = BeginPaint(hWnd, &ps);
123
124 // Here your application is laid out.
125 // For this introduction, we just print out "Hello, Windows desktop!"
126 // in the top left corner.
127 TextOut(hdc,
128 5, 5,
129 greeting, _tcslen(greeting));
130 // End application-specific layout section.
131
132 EndPaint(hWnd, &ps);
133 break;
134 case WM_DESTROY:
135 PostQuitMessage(0);
136 break;
137 default:
138 return DefWindowProc(hWnd, message, wParam, lParam);
139 break;
140 }
141
142 return 0;
143}
144