How To Develop Windows Application
This browser is no longer supported.
Upgrade to Microsoft Border to take advantage of the latest features, security updates, and technical support.
Walkthrough: Create a traditional Windows Desktop application (C++)
This walkthrough shows how to create a traditional Windows desktop application in Visual Studio. The example application you'll create uses the Windows API to display "Hello, Windows desktop!" in a window. You tin can utilise the code that you develop in this walkthrough every bit a pattern to create other Windows desktop applications.
The Windows API (also known as the Win32 API, Windows Desktop API, and Windows Classic API) is a C-linguistic communication-based framework for creating Windows applications. Information technology has been in beingness since the 1980s and has been used to create Windows applications for decades. More advanced and easier-to-program frameworks have been built on peak of the Windows API. For example, MFC, ATL, the .Internet frameworks. Even the virtually modern Windows Runtime code for UWP and Store apps written in C++/WinRT uses the Windows API underneath. For more information nigh the Windows API, see Windows API Alphabetize. At that place are many ways to create Windows applications, but the process above was the first.
Important
For the sake of brevity, some code statements are omitted in the text. The Build the lawmaking department at the end of this document shows the complete code.
Prerequisites
-
A computer that runs Microsoft Windows seven or later versions. We recommend Windows x or later for the best evolution experience.
-
A copy of Visual Studio. For information on how to download and install Visual Studio, see Install Visual Studio. When you lot run the installer, make sure that the Desktop development with C++ workload is checked. Don't worry if you didn't install this workload when you installed Visual Studio. Y'all tin run the installer again and install it now.
-
An understanding of the basics of using the Visual Studio IDE. If y'all've used Windows desktop apps before, yous tin probably keep upward. For an introduction, see Visual Studio IDE characteristic tour.
-
An understanding of enough of the fundamentals of the C++ language to follow forth. Don't worry, we don't do anything besides complicated.
Create a Windows desktop project
Follow these steps to create your first Windows desktop projection. As you become, yous'll enter the code for a working Windows desktop application. To run into the documentation for your preferred version of Visual Studio, use the Version selector control. It'south establish at the top of the table of contents on this page.
To create a Windows desktop projection in Visual Studio
-
From the main carte, choose File > New > Project to open the Create a New Project dialog box.
-
At the peak of the dialog, set up Language to C++, set up Platform to Windows, and set Project type to Desktop.
-
From the filtered listing of project types, cull Windows Desktop Wizard then choose Next. In the next folio, enter a name for the project, for example, DesktopApp.
-
Cull the Create button to create the project.
-
The Windows Desktop Projection dialog at present appears. Under Awarding type, select Desktop application (.exe). Under Additional options, select Empty project. Choose OK to create the project.
-
In Solution Explorer, right-click the DesktopApp project, cull Add together, then choose New Particular.
-
In the Add together New Particular dialog box, select C++ File (.cpp). In the Proper noun box, type a name for the file, for instance, HelloWindowsDesktop.cpp. Choose Add.
Your project is now created and your source file is opened in the editor. To continue, skip ahead to Create the code.
To create a Windows desktop projection in Visual Studio 2017
-
On the File card, choose New and then choose Projection.
-
In the New Project dialog box, in the left pane, expand Installed > Visual C++, so select Windows Desktop. In the middle pane, select Windows Desktop Wizard.
In the Name box, blazon a name for the projection, for instance, DesktopApp. Cull OK.
-
In the Windows Desktop Project dialog, nether Application blazon, select Windows application (.exe). Under Additional options, select Empty project. Make sure Precompiled Header isn't selected. Cull OK to create the project.
-
In Solution Explorer, correct-click the DesktopApp project, choose Add together, and so choose New Item.
-
In the Add New Item dialog box, select C++ File (.cpp). In the Name box, blazon a name for the file, for example, HelloWindowsDesktop.cpp. Choose Add.
Your projection is at present created and your source file is opened in the editor. To continue, skip ahead to Create the code.
To create a Windows desktop project in Visual Studio 2015
-
On the File menu, choose New and then cull Projection.
-
In the New Project dialog box, in the left pane, aggrandize Installed > Templates > Visual C++, and then select Win32. In the middle pane, select Win32 Projection.
In the Proper name box, type a name for the projection, for case, DesktopApp. Choose OK.
-
On the Overview page of the Win32 Application Magician, choose Next.
-
On the Application Settings page, under Application type, select Windows application. Under Additional options, uncheck Precompiled header, so select Empty project. Choose Finish to create the project.
-
In Solution Explorer, correct-click the DesktopApp project, choose Add together, then choose New Detail.
-
In the Add New Particular dialog box, select C++ File (.cpp). In the Name box, type a name for the file, for example, HelloWindowsDesktop.cpp. Cull Add.
Your project is at present created and your source file is opened in the editor.
Create the code
Next, y'all'll learn how to create the code for a Windows desktop application in Visual Studio.
To start a Windows desktop awarding
-
But as every C application and C++ awarding must have a
principal
function as its starting indicate, every Windows desktop application must accept aWinMain
function.WinMain
has the following syntax.int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow );
For information about the parameters and return value of this function, see WinMain entry point.
Notation
What are all those extra words, such as
WINAPI
, orCALLBACK
, orHINSTANCE
, or_In_
? The traditional Windows API uses typedefs and preprocessor macros extensively to abstruse abroad some of the details of types and platform-specific code, such as calling conventions,__declspec
declarations, and compiler pragmas. In Visual Studio, you tin use the IntelliSense Quick Info feature to see what these typedefs and macros ascertain. Hover your mouse over the discussion of interest, or select it and press Ctrl+K, Ctrl+I for a minor pop-up window that contains the definition. For more information, run into Using IntelliSense. Parameters and return types often apply SAL Annotations to aid you catch programming errors. For more information, see Using SAL Annotations to Reduce C/C++ Code Defects. -
Windows desktop programs crave
<windows.h>
.<tchar.h>
defines theTCHAR
macro, which resolves ultimately towchar_t
if the UNICODE symbol is divers in your project, otherwise information technology resolves tochar
. If you e'er build with UNICODE enabled, you don't need TCHAR and can just employwchar_t
directly.#include <windows.h> #include <tchar.h>
-
Along with the
WinMain
function, every Windows desktop awarding must also have a window-procedure function. This part is typically namedWndProc
, but you lot can name it whatsoever you similar.WndProc
has the post-obit syntax.LRESULT CALLBACK WndProc( _In_ HWND hWnd, _In_ UINT message, _In_ WPARAM wParam, _In_ LPARAM lParam );
In this function, you write code to handle letters that the awarding receives from Windows when events occur. For example, if a user chooses an OK push button in your awarding, Windows will transport a message to you and you tin write lawmaking inside your
WndProc
function that does whatever work is advisable. It'south called handling an event. You simply handle the events that are relevant for your application.For more data, see Window Procedures.
To add functionality to the WinMain function
-
In the
WinMain
function, yous populate a structure of type WNDCLASSEX. The structure contains information almost the window: the awarding icon, the groundwork color of the window, the name to brandish in the title bar, among other things. Importantly, information technology contains a function pointer to your window procedure. The following instance shows a typicalWNDCLASSEX
structure.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(wcex.hInstance, IDI_APPLICATION); wcex.hCursor = LoadCursor(Cipher, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+ane); wcex.lpszMenuName = Null; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
For information about the fields of the structure above, see WNDCLASSEX.
-
Register the
WNDCLASSEX
with Windows so that it knows nigh your window and how to send messages to it. Apply the RegisterClassEx function and pass the window grade construction as an argument. The_T
macro is used because we use theTCHAR
type.if (!RegisterClassEx(&wcex)) { MessageBox(Aught, _T("Call to RegisterClassEx failed!"), _T("Windows Desktop Guided Tour"), NULL); return ane; }
-
Now you can create a window. Use the CreateWindowEx function.
static TCHAR szWindowClass[] = _T("DesktopApp"); static TCHAR szTitle[] = _T("Windows Desktop Guided Bout Awarding"); // The parameters to CreateWindowEx explained: // WS_EX_OVERLAPPEDWINDOW : An optional extended window style. // szWindowClass: the name of the application // szTitle: the text that appears in the title bar // WS_OVERLAPPEDWINDOW: the blazon 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 does not take a menu bar // hInstance: the beginning parameter from WinMain // NULL: not used in this application HWND hWnd = CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, Goose egg, hInstance, Cypher ); if (!hWnd) { MessageBox(NULL, _T("Call to CreateWindowEx failed!"), _T("Windows Desktop Guided Bout"), NULL); return 1; }
This office returns an
HWND
, which is a handle to a window. A handle is somewhat similar a arrow that Windows uses to go along rail of open windows. For more data, see Windows Data Types. -
At this betoken, the window has been created, but we all the same demand to tell Windows to get in visible. That's what this code does:
// The parameters to ShowWindow explained: // hWnd: the value returned from CreateWindow // nCmdShow: the fourth parameter from WinMain ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);
The displayed window doesn't accept much content because you haven't notwithstanding implemented the
WndProc
office. In other words, the awarding isn't yet handling the messages that Windows is now sending to it. -
To handle the messages, nosotros first add a bulletin loop to listen for the messages that Windows sends. When the application receives a message, this loop dispatches information technology to your
WndProc
function to be handled. The message loop resembles the following code.MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam;
For more information about the structures and functions in the bulletin loop, meet MSG, GetMessage, TranslateMessage, and DispatchMessage.
At this point, the
WinMain
office should resemble the following code.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(wcex.hInstance, IDI_APPLICATION); wcex.hCursor = LoadCursor(Goose egg, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+i); wcex.lpszMenuName = NULL; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION); if (!RegisterClassEx(&wcex)) { MessageBox(Cypher, _T("Call to RegisterClassEx failed!"), _T("Windows Desktop Guided Tour"), NULL); return one; } // Store case handle in our global variable hInst = hInstance; // The parameters to CreateWindowEx explained: // WS_EX_OVERLAPPEDWINDOW : An optional extended window way. // szWindowClass: the name of the awarding // szTitle: the text that appears in the championship bar // WS_OVERLAPPEDWINDOW: the blazon of window to create // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y) // 500, 100: initial size (width, length) // NULL: the parent of this window // Naught: this application dows not have a card bar // hInstance: the showtime parameter from WinMain // Nada: not used in this application HWND hWnd = CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hInstance, NULL ); if (!hWnd) { MessageBox(Cipher, _T("Call to CreateWindow failed!"), _T("Windows Desktop Guided Bout"), Zero); render 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, Cypher, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; }
To add together functionality to the WndProc function
-
To enable the
WndProc
function to handle the letters that the awarding receives, implement a switch argument.One important message to handle is the WM_PAINT message. The application receives the
WM_PAINT
message when function of its displayed window must be updated. The event can occur when a user moves a window in front of your window, and then moves it abroad again. Your application doesn't know when these events occur. Only Windows knows, so it notifies your app with aWM_PAINT
bulletin. When the window is start displayed, all of it must be updated.To handle a
WM_PAINT
message, showtime call BeginPaint, then handle all the logic to lay out the text, buttons, and other controls in the window, and then call EndPaint. For the application, the logic between the kickoff call and the catastrophe call displays the cord "Howdy, Windows desktop!" in the window. In the following code, the TextOut function is used to brandish the string.PAINTSTRUCT ps; HDC hdc; TCHAR greeting[] = _T("Hello, Windows desktop!"); switch (message) { instance WM_PAINT: hdc = BeginPaint(hWnd, &ps); // Hither your application is laid out. // For this introduction, we simply print out "Hello, Windows desktop!" // in the top left corner. TextOut(hdc, v, 5, greeting, _tcslen(greeting)); // End application-specific layout department. EndPaint(hWnd, &ps); break; }
HDC
in the code is a handle to a device context, which is used to describe in the window'southward client expanse. Apply theBeginPaint
andEndPaint
functions to set up for and consummate the drawing in the client expanse.BeginPaint
returns a handle to the display device context used for drawing in the client expanse;EndPaint
ends the pigment request and releases the device context. -
An application typically handles many other messages. For example, WM_CREATE when a window is first created, and WM_DESTROY when the window is closed. The following lawmaking shows a basic but complete
WndProc
function.LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; TCHAR greeting[] = _T("Hullo, Windows desktop!"); switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // Here your application is laid out. // For this introduction, we merely print out "Hello, Windows desktop!" // in the top left corner. TextOut(hdc, 5, 5, greeting, _tcslen(greeting)); // End application specific layout section. EndPaint(hWnd, &ps); break; example WM_DESTROY: PostQuitMessage(0); pause; default: return DefWindowProc(hWnd, message, wParam, lParam); break; } return 0; }
Build the code
Every bit promised, here's the complete code for the working application.
To build this example
-
Delete whatever code you've entered in HelloWindowsDesktop.cpp in the editor. Copy this example code and and then paste it into HelloWindowsDesktop.cpp:
// HelloWindowsDesktop.cpp // compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c #include <windows.h> #include <stdlib.h> #include <string.h> #include <tchar.h> // Global variables // The main window grade name. static TCHAR szWindowClass[] = _T("DesktopApp"); // The cord that appears in the awarding's title bar. static TCHAR szTitle[] = _T("Windows Desktop Guided Tour Awarding"); // Stored instance handle for utilise in Win32 API calls such as FindResource HINSTANCE hInst; // Frontward declarations of functions included in this code module: LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow ) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.mode = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(wcex.hInstance, IDI_APPLICATION); wcex.hCursor = LoadCursor(Cypher, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION); if (!RegisterClassEx(&wcex)) { MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Windows Desktop Guided Tour"), Zilch); return 1; } // Store instance handle in our global variable hInst = hInstance; // The parameters to CreateWindowEx explained: // WS_EX_OVERLAPPEDWINDOW : An optional extended window style. // szWindowClass: the proper name of the application // szTitle: the text that appears in the championship bar // WS_OVERLAPPEDWINDOW: the type of window to create // CW_USEDEFAULT, CW_USEDEFAULT: initial position (10, y) // 500, 100: initial size (width, length) // NULL: the parent of this window // NULL: this awarding does non have a menu bar // hInstance: the offset parameter from WinMain // Nada: non used in this awarding HWND hWnd = CreateWindowEx( WS_EX_OVERLAPPEDWINDOW, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, Cypher, NULL, hInstance, Cypher ); if (!hWnd) { MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Windows Desktop Guided Bout"), Nada); return ane; } // 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, Cypher, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } render (int) msg.wParam; } // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the principal window. // // WM_PAINT - Paint the principal window // WM_DESTROY - post a quit bulletin and return LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; TCHAR greeting[] = _T("How-do-you-do, Windows desktop!"); switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // Here your application is laid out. // For this introduction, we just print out "Hi, Windows desktop!" // in the peak left corner. TextOut(hdc, 5, 5, greeting, _tcslen(greeting)); // End application-specific layout section. EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, bulletin, wParam, lParam); break; } render 0; }
-
On the Build card, cull Build Solution. The results of the compilation should appear in the Output window in Visual Studio.
-
To run the application, press F5. A window that contains the text "Hello, Windows desktop!" should appear in the upper-left corner of the brandish.
Congratulations! You've completed this walkthrough and congenital a traditional Windows desktop application.
Run into also
Windows Desktop Applications
Feedback
Submit and view feedback for
Source: https://docs.microsoft.com/en-us/cpp/windows/walkthrough-creating-windows-desktop-applications-cpp
Posted by: bakerdreme1954.blogspot.com
0 Response to "How To Develop Windows Application"
Post a Comment