Windows-classic-samples
This repo contains samples that demonstrate the API used in Windows classic desktop applications.
Top Related Projects
API samples for the Universal Windows Platform.
This repo contains driver samples prepared for use with Microsoft Visual Studio and the Windows Driver Kit (WDK). It contains both Universal Windows Driver and desktop-only driver samples.
Quick Overview
The microsoft/Windows-classic-samples repository is a collection of code samples that demonstrate various Windows API functionalities and programming techniques for classic Windows desktop applications. These samples cover a wide range of topics, including user interface, system services, networking, and more, providing developers with practical examples for Windows development.
Pros
- Comprehensive collection of Windows API usage examples
- Official samples from Microsoft, ensuring reliability and best practices
- Covers both basic and advanced Windows programming concepts
- Regularly updated to reflect changes in Windows APIs
Cons
- Primarily focused on classic Windows desktop development, not modern UWP or .NET Core
- Some samples may be outdated or less relevant for newer Windows versions
- Limited coverage of cross-platform development scenarios
- May require specific Windows SDK versions for certain samples
Code Examples
Here are a few short code examples from the repository:
- Creating a basic window (from the HelloWorld sample):
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 5, 5, TEXT("Hello, Windows!"), 15);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
- Enumerating processes (from the ProcessEnumeration sample):
BOOL EnumerateProcesses()
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return FALSE;
}
cProcesses = cbNeeded / sizeof(DWORD);
for (DWORD i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
PrintProcessNameAndID(aProcesses[i]);
}
}
return TRUE;
}
- Using Direct2D to draw a rectangle (from the D2DDemo sample):
HRESULT OnRender()
{
HRESULT hr = S_OK;
hr = CreateDeviceResources();
if (SUCCEEDED(hr))
{
m_pRenderTarget->BeginDraw();
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
D2D1_SIZE_F rtSize = m_pRenderTarget->GetSize();
D2D1_RECT_F rectangle = D2D1::RectF(
rtSize.width / 2 - 50.0f,
rtSize.height / 2 - 50.0f,
rtSize.width / 2 + 50.0f,
rtSize.height / 2 + 50.0f
);
m_pRenderTarget->FillRectangle(&rectangle, m_pLightSlateGrayBrush);
m_pRenderTarget->DrawRectangle(&rectangle, m_pCornflowerBlueBrush);
hr = m_pRenderTarget->EndDraw();
}
return hr;
}
Getting Started
To get started with the Windows-classic-samples:
-
Clone the repository:
git clone https://github.com/microsoft/Windows-classic-samples.git
-
Open the desired sample project in Visual Studio.
-
Ensure you have the required Windows SDK version installed.
-
Build and run the sample to see it in action.
-
Explore the source code to
Competitor Comparisons
API samples for the Universal Windows Platform.
Pros of Windows-universal-samples
- Focuses on Universal Windows Platform (UWP) development, supporting modern Windows apps
- Includes samples for newer Windows features and APIs
- Provides examples for cross-device development (PC, mobile, Xbox, etc.)
Cons of Windows-universal-samples
- Limited to UWP, not applicable for traditional desktop application development
- May not cover older Windows APIs or features still used in some scenarios
- Requires newer versions of Windows for development and testing
Code Comparison
Windows-universal-samples (UWP):
Windows.UI.Xaml.Application.Start((p) =>
{
var rootFrame = new Frame();
Window.Current.Content = rootFrame;
rootFrame.Navigate(typeof(MainPage));
Window.Current.Activate();
});
Windows-classic-samples (Win32):
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.lpszClassName = L"SampleWindowClass";
RegisterClassEx(&wcex);
Summary
Windows-universal-samples is tailored for modern UWP development, offering cross-device compatibility and access to newer Windows features. However, it's limited to UWP and may not be suitable for traditional desktop applications. Windows-classic-samples, on the other hand, provides examples for Win32 and other classic Windows APIs, making it more appropriate for legacy or desktop-focused development. The choice between the two depends on the target platform, required features, and development goals.
This repo contains driver samples prepared for use with Microsoft Visual Studio and the Windows Driver Kit (WDK). It contains both Universal Windows Driver and desktop-only driver samples.
Pros of Windows-driver-samples
- Focuses on driver development, providing specialized examples for hardware interaction
- Includes samples for various driver types (WDF, KMDF, UMDF) and device categories
- Offers more recent and actively maintained samples for modern Windows versions
Cons of Windows-driver-samples
- More complex and requires deeper system-level knowledge
- Limited scope, primarily useful for driver developers rather than general Windows programmers
- Steeper learning curve for beginners compared to classic samples
Code Comparison
Windows-driver-samples (KMDF driver):
NTSTATUS
EvtDeviceAdd(
_In_ WDFDRIVER Driver,
_Inout_ PWDFDEVICE_INIT DeviceInit
)
{
NTSTATUS status;
WDF_OBJECT_ATTRIBUTES deviceAttributes;
WDFDEVICE device;
PDEVICE_CONTEXT deviceContext;
Windows-classic-samples (Win32 API):
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Windows classic samples
This repo contains samples that demonstrate the API used in Windows classic desktop applications.
Note. You can use Microsoft Visual Studio to search the entire set of source code here to see whether the usage of a particular Windows API is being demonstrated. Clone this repo (or download the ZIP) to your local file system. Then Find in Files in Visual Studio, set Look in to the folder you cloned or downloaded into, and search for an API name. You can install Visual Studio without expense. A Community edition is available—it's free for students, open-source contributors, and individuals.
These samples demonstrate the functionality and programming model for Windows and Windows Server. This repo contains Visual Studio solution (SLN) files for each sample, along with the source files, assets, resources, and metadata needed to compile and run the sample. For more info about the programming models, platforms, languages, and APIs demonstrated in these samples, check out the documentation on the Windows Dev Center.
Most of these samples were created for Windows 7, Windows 8.1 and/or Windows Server 2012 R2 using Visual Studio 2013, but some were created with earlier versions, or with specific SKU and other requirements. In many cases, the samples will also work on later versions than the ones indicated. If you cannot get a sample to work as expected, it might have platform, version, or SKU-specific requirements. If you have any issues with the samples, please provide feedback using the Issues list.
To get a copy of Visual Studio, go to Visual Studio Downloads.
Other samples:
- Windows Universal Samples (UWP)
- Desktop Bridge to Universal Windows Platform (UWP)
- Code samples browser
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Top Related Projects
API samples for the Universal Windows Platform.
This repo contains driver samples prepared for use with Microsoft Visual Studio and the Windows Driver Kit (WDK). It contains both Universal Windows Driver and desktop-only driver samples.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot