C++ Hello World win32(1998) 用 VS CODE
有没有时光倒流的感觉?
https://resources.oreilly.com/examples/9781572319950/tree/master/cd_contents
2023 小更新:
跑这些例子, 用 GCC + C:/msys64/mingw64/include
的 TASKS.JSON 节段。
{
"type": "cppbuild",
"label": "MSYS2 G++ C/C++ build Gtk file",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": [
"-g",
"-Wall",
"-Wextra",
"-mms-bitfields",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe",
"-D_FILE_OFFSET_BITS=64",
"-D_LARGEFILE64_SOURCE",
"-fdiagnostics-color=always",
"-IC:/portaudio/include",
"-IC:/msys64/mingw64/include/gtk-3.0",
"-IC:/msys64/mingw64/include/pango-1.0",
"-IC:/msys64/mingw64/include",
"-IC:/msys64/mingw64/include/glib-2.0",
"-IC:/msys64/mingw64/lib/glib-2.0/include",
"-IC:/msys64/mingw64/include/harfbuzz",
"-IC:/msys64/mingw64/include/freetype2",
"-IC:/msys64/mingw64/include/libpng16",
"-IC:/msys64/mingw64/include/fribidi",
"-IC:/msys64/mingw64/include/cairo",
"-IC:/msys64/mingw64/include/lzo",
"-IC:/msys64/mingw64/include/pixman-1",
"-IC:/msys64/mingw64/include/gdk-pixbuf-2.0",
"-IC:/msys64/mingw64/include/atk-1.0",
"-IC:/msys64/mingw64/include/gstreamer-1.0",
"-IC:/Aconda3/Library/include",
"-IC:/gnuplot-iostream",
// "-IC:/gstreamer/1.0/msvc_x86_64/include/gstreamer-1.0",
"-LC:/msys64/mingw64/lib/mpg123",
"-LC:/msys64/mingw64/lib",
"-lsndfile", "-lportaudio",
"-lgstapp-1.0",
"-lgstbase-1.0",
"-lgstreamer-1.0",
"-lgtk-3",
"-lgdk-3",
"-lmpg123",
"-lz",
"-lgdi32",
"-limm32",
"-lshell32",
"-lole32",
"-luuid",
"-lcomdlg32",
"-lwinmm",
"-ldwmapi",
"-lsetupapi",
"-lcfgmgr32",
"-lpangowin32-1.0",
"-lpangocairo-1.0",
"-lpango-1.0",
"-lharfbuzz",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lglib-2.0",
"-lintl"
"options": {
"cwd": "${workspaceFolder}"
"problemMatcher": [
"$gcc"
"detail": "compiler: C:/msys64/mingw64/bin/gcc.exe",
"group": {
"kind": "build",
"isDefault": true
执行结果:
* Executing task: MSYS2 G++ C/C++ build Gtk file
Starting build...
C:/msys64/mingw64/bin/g++.exe -g -Wall -Wextra -mms-bitfields C:\gtk\helloW32_test02.c -o C:\gtk/helloW32_test02.exe -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -fdiagnostics-color=always -IC:/portaudio/include -IC:/msys64/mingw64/include/gtk-3.0 -IC:/msys64/mingw64/include/pango-1.0 -IC:/msys64/mingw64/include -IC:/msys64/mingw64/include/glib-2.0 -IC:/msys64/mingw64/lib/glib-2.0/include -IC:/msys64/mingw64/include/harfbuzz -IC:/msys64/mingw64/include/freetype2 -IC:/msys64/mingw64/include/libpng16 -IC:/msys64/mingw64/include/fribidi -IC:/msys64/mingw64/include/cairo -IC:/msys64/mingw64/include/lzo -IC:/msys64/mingw64/include/pixman-1 -IC:/msys64/mingw64/include/gdk-pixbuf-2.0 -IC:/msys64/mingw64/include/atk-1.0 -IC:/msys64/mingw64/include/gstreamer-1.0 -IC:/Aconda3/Library/include -IC:/gnuplot-iostream -LC:/msys64/mingw64/lib/mpg123 -LC:/msys64/mingw64/lib -lsndfile -lportaudio -lgstapp-1.0 -lgstbase-1.0 -lgstreamer-1.0 -lgtk-3 -lgdk-3 -lmpg123 -lz -lgdi32 -limm32 -lshell32 -lole32 -luuid -lcomdlg32 -lwinmm -ldwmapi -lsetupapi -lcfgmgr32 -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
C:\gtk\helloW32_test02.c:5: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
5 | #pragma comment(lib,"Ole32.Lib")
C:\gtk\helloW32_test02.c:6: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
6 | #pragma comment(lib,"kernel32.lib")
C:\gtk\helloW32_test02.c:7: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
7 | #pragma comment(lib,"user32.lib")
In file included from C:/msys64/mingw64/include/minwindef.h:163,
from C:/msys64/mingw64/include/windef.h:9,
from C:/msys64/mingw64/include/windows.h:69,
from C:\gtk\helloW32_test02.c:1:
C:\gtk\helloW32_test02.c: In function 'int WinMain(HINSTANCE, HINSTANCE, PSTR, int)':
C:\gtk\helloW32_test02.c:30:33: warning: ISO C++ forbids converting a string constant to 'TCHAR*' {aka 'char*'} [-Wwrite-strings]
30 | MessageBoxPrintf (TEXT ("ScrnSize"),
| ^~~~~~~~~~
C:\gtk\helloW32_test02.c:31:15: warning: ISO C++ forbids converting a string constant to 'TCHAR*' {aka 'char*'} [-Wwrite-strings]
31 | TEXT ("The screen is %i pixels wide by %i pixels high."),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\gtk\helloW32_test02.c:25:31: warning: unused parameter 'hInstance' [-Wunused-parameter]
25 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
| ~~~~~~~~~~^~~~~~~~~
C:\gtk\helloW32_test02.c:25:52: warning: unused parameter 'hPrevInstance' [-Wunused-parameter]
25 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
| ~~~~~~~~~~^~~~~~~~~~~~~
C:\gtk\helloW32_test02.c:25:72: warning: unused parameter 'szCmdLine' [-Wunused-parameter]
25 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
| ~~~~~^~~~~~~~~
C:\gtk\helloW32_test02.c:25:87: warning: unused parameter 'iCmdShow' [-Wunused-parameter]
25 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
| ~~~~^~~~~~~~
Build finished with warning(s).
* Terminal will be reused by tasks, press any key to close it.
命令行详情:
C:/msys64/mingw64/bin/g++.exe -g -Wall -Wextra -mms-bitfields C:\gtk\helloW32_test02.c
-o C:\gtk/helloW32_test02.exe -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE
-fdiagnostics-color=always
-IC:/portaudio/include -IC:/msys64/mingw64/include/gtk-3.0 -IC:/msys64/mingw64/include/pango-1.0
-IC:/msys64/mingw64/include -IC:/msys64/mingw64/include/glib-2.0 -IC:/msys64/mingw64/lib/glib-2.0/include
-IC:/msys64/mingw64/include/harfbuzz -IC:/msys64/mingw64/include/freetype2 -IC:/msys64/mingw64/include/libpng16
-IC:/msys64/mingw64/include/fribidi -IC:/msys64/mingw64/include/cairo -IC:/msys64/mingw64/include/lzo
-IC:/msys64/mingw64/include/pixman-1 -IC:/msys64/mingw64/include/gdk-pixbuf-2.0 -IC:/msys64/mingw64/include/atk-1.0
-IC:/msys64/mingw64/include/gstreamer-1.0 -IC:/Aconda3/Library/include -IC:/gnuplot-iostream
-LC:/msys64/mingw64/lib/mpg123 -LC:/msys64/mingw64/lib -lsndfile -lportaudio -lgstapp-1.0 -lgstbase-1.0
-lgstreamer-1.0 -lgtk-3 -lgdk-3 -lmpg123 -lz -lgdi32 -limm32 -lshell32 -lole32 -luuid -lcomdlg32 -lwinmm
-ldwmapi -lsetupapi -lcfgmgr32 -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject
-lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
/*--------------------------------------------------------------
HelloMsg.c -- Displays "Hello, Windows 98!" in a message box
(c) Charles Petzold, 1998
--------------------------------------------------------------*/
#include <windows.h>
//using namespace std;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ;
return 0 ;
}
Button Click Demo
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK MyEventHandler_aka_WndProc (HWND, UINT, WPARAM, LPARAM);
#define IDBUTTON 102
/* Make the class name into a global variable */
char szClassName[ ] = "My_Virgin_App_0";
HINSTANCE g_hInst;
LRESULT CALLBACK MyEventHandler_aka_WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
HWND hwndButton;
switch (message) { /* Handles all Windows Messages */
case WM_COMMAND:{
if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)){
int iMID;
iMID = LOWORD(wParam);
switch(iMID){
case IDBUTTON:{
MessageBox(hwnd, (LPCTSTR)"Thank you for letting me out ! Thank you! Thank you! Thank you! \n Thank you! Thank you!\n Thank you! ", (LPCTSTR) "Voila, My Lord!", MB_OK|MB_ICONEXCLAMATION);
break;
default:
break;
break;
case WM_DESTROY:{
PostQuitMessage (0); /* send a WM_QUIT to Message Queue, to shut off program */
break;
case WM_CREATE:{
hwndButton = CreateWindowEx(0, /* more or ''extended'' styles */
TEXT("BUTTON"), /* GUI ''class'' to create */
TEXT(" Dear, Honey, Try Me, Gimme a push! Turn me on ............................. "), /* GUI caption */
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON, /* control styles separated by | */
10, /* LEFT POSITION (Position from left) */
10, /* TOP POSITION (Position from Top) */
600, /* WIDTH OF CONTROL */
50, /* HEIGHT OF CONTROL */
hwnd, /* Parent window handle */
(HMENU)IDBUTTON, /* control''s ID for WM_COMMAND */
g_hInst, /* application instance */
NULL);
break;
default: /* messages that we will not process */
return DefWindowProc (hwnd, message, wParam, lParam);
return 0;
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
g_hInst = hThisInstance;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = MyEventHandler_aka_WndProc; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows''s default color as the background of the window */
wincl.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let''s create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Very Fancy Name For Your App v0.0.0...9888880", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
800, /* The programs MAIN WINDOW width */
600, /* and MAIN WINDOW height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
/* Make the window visible on the screen */
ShowWindow (hwnd, SW_SHOW);
UpdateWindow(hwnd);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to MyEventHandler_aka_WndProc */
DispatchMessage(&messages);
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
练习 1
#include <windows.h> //winuser.h (include Windows.h)
//#pragma comment(lib,"libucrt.lib")
#pragma comment(lib,"Ole32.Lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
//#pragma comment(lib,"oleaut32.lib") //COM Automation
//#pragma comment(lib,"user32.lib")
// CL.EXE WILL USE THIS AND G++ WILL IGNORE THIS
// G++ YOU NEED TO ADD "-lole32", "-lkernel32",
/* WHILE EQUIVALANT TO ADDING THIS FOR G++ "-lole32", "-lkernel32",
FOR CL.EXE IS ADDING "ole32.lib","kernel32.lib", TO TASKS.JSON
OR ole32.lib kernel32.lib after cl.exe
Starting build...
cl.exe /WL /favor:AMD64 /EHsc ole32.lib kernel32.lib -D_FILE_OFFSET_BITS=64 WXrecx64.cpp -o WXrecx64.exe
//#pragma comment(lib,"libcpmt.lib")
//#pragma comment(lib,"strmiids.lib")
//using namespace std;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ;
return 0 ;
}
练习 2
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib,"Ole32.Lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
TCHAR szBuffer [1024] ;
va_list pArgList ;
// The va_start macro (defined in STDARG.H) is usually equivalent to:
// pArgList = (char *) &szFormat + sizeof (szFormat) ;
va_start (pArgList, szFormat) ;
// The last argument to wvsprintf points to the arguments
_vsntprintf (szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
szFormat, pArgList) ;
// The va_end macro just zeroes out pArgList for no good reason
va_end (pArgList) ;
return MessageBox (NULL, szBuffer, szCaption, 0) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
int cxScreen, cyScreen ;
cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
MessageBoxPrintf (TEXT ("ScrnSize"),
TEXT ("The screen is %i pixels wide by %i pixels high."),
cxScreen, cyScreen) ;
return 0 ;
}
练习 3
** 似乎没有 ++ 的东西, 所以存成 .C 文件, 用 gcc 编译也是可以的
> Executing task: MSYS2_64 C/C++: gcc.exe build active file <
Starting build...
C:\msys64\mingw64\bin\gcc.exe -g -Wall -Wextra C:\___GTK___\HELLOWIN98.c -o C:\___GTK___\HELLOWIN98.exe -lm -fdiagnostics-color=always -IC:/msys64/mingw64/include/gtk-3.0 -IC:/msys64/mingw64/include/pango-1.0 -IC:/msys64/mingw64/include -IC:/msys64/mingw64/include/glib-2.0 -IC:/msys64/mingw64/lib/glib-2.0/include -IC:/msys64/mingw64/include/harfbuzz -IC:/msys64/mingw64/include/freetype2 -IC:/msys64/mingw64/include/libpng16 -IC:/msys64/mingw64/include/fribidi -IC:/msys64/mingw64/include/cairo -IC:/msys64/mingw64/include/lzo -IC:/msys64/mingw64/include/pixman-1 -mms-bitfields -IC:/msys64/mingw64/include/gdk-pixbuf-2.0 -IC:/msys64/mingw64/include/atk-1.0 -lgtk-3 -lgdk-3 -lz -lgdi32 -limm32 -lshell32 -lole32 -luuid -lwinmm -ldwmapi -lsetupapi -lcfgmgr32 -lpangowin32-1.0 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
C:\___GTK___\HELLOWIN98.c:3: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
3 | #pragma comment(lib,"Ole32.Lib")
C:\___GTK___\HELLOWIN98.c:4: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
4 | #pragma comment(lib,"kernel32.lib")
C:\___GTK___\HELLOWIN98.c:5: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
5 | #pragma comment(lib,"user32.lib")
C:\___GTK___\HELLOWIN98.c:7: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
7 | #pragma comment(lib,"Winmm.lib")13576679-388071169
C:\___GTK___\HELLOWIN98.c:8: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
8 | #pragma comment(lib,"Gdi32.lib") // wingdi.h (include Windows.h)
474b.com/file/
C:\___GTK___\HELLOWIN98.c: In function 'WinMain':
C:\___GTK___\HELLOWIN98.c:12:52: warning: unused parameter 'hPrevInstance' [-Wunused-parameter]
12 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
| ~~~~~~~~~~^~~~~~~~~~~~~
C:\___GTK___\HELLOWIN98.c:12:72: warning: unused parameter 'szCmdLine' [-Wunused-parameter]
12 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
| ~~~~~^~~~~~~~~
Build finished with warning(s). 警告就警告吧
Terminal will be reused by tasks, press any key to close it.
// sine plot
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <math.h>
#pragma comment(lib,"Ole32.Lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
//Winmm.lib
#pragma comment(lib,"Winmm.lib")
#pragma comment(lib,"Gdi32.lib") // wingdi.h (include Windows.h) Gdi32.lib
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
hwnd = CreateWindow(szAppName, // window class name
TEXT("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
TranslateMessage(&msg);
DispatchMessage(&msg);
return msg.wParam;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
HDC hdc;
PAINTSTRUCT ps;
int i;
#define NUM 300
POINT apt[NUM];
switch (message)
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
MoveToEx(hdc, 0, 200, NULL);
LineTo(hdc, 500, 200);
for (i = 0; i < NUM; i++){
apt[i].x = i * 500 / NUM;
apt[i].y = (int)(200 - 100*sin(2 * 3.14159*i / NUM));
Polyline(hdc,apt,NUM);
EndPaint(hwnd, &ps);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
return 0;
}
/*-----------------------------------------
SINEWAVE.C -- Sine Wave Using Polyline
(c) Charles Petzold, 1998
-----------------------------------------*/
#include <windows.h>
#include <math.h>
#pragma comment(lib,"Ole32.Lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
//Winmm.lib
#pragma comment(lib,"Winmm.lib")
#pragma comment(lib,"Gdi32.lib") // wingdi.h (include Windows.h) Gdi32.lib
#define NUM 1000
#define TWOPI (2 * 3.14159)
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName[] = TEXT ("SineWave") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
hwnd = CreateWindow (szAppName, TEXT ("Sine Wave Using Polyline"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
return msg.wParam ;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
static int cxClient, cyClient ;
HDC hdc ;
int i ;
PAINTSTRUCT ps ;
POINT apt [NUM] ;
switch (message)
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
MoveToEx (hdc, 0, cyClient / 2, NULL) ;
LineTo (hdc, cxClient, cyClient / 2) ;
for (i = 0 ; i < NUM ; i++)
apt[i].x = i * cxClient / NUM ;
apt[i].y = (int) (cyClient / 2 * (1 - sin (TWOPI * i / NUM))) ;
Polyline (hdc, apt, NUM) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
return DefWindowProc (hwnd, message, wParam, lParam) ;
#include <windows.h> //Mmsystem.h (include Windows.h)
#pragma comment(lib,"Ole32.Lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
//Winmm.lib
#pragma comment(lib,"Winmm.lib")
#pragma comment(lib,"Gdi32.lib") // wingdi.h (include Windows.h)
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
return msg.wParam ;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
case WM_CREATE:
PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
return DefWindowProc (hwnd, message, wParam, lParam) ;
//https://docs.microsoft.com/en-us/windows/win32/winmsg/using-timers
#include <windows.h>
#pragma comment(lib,"Ole32.Lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
//Winmm.lib
#pragma comment(lib,"Winmm.lib")
#pragma comment(lib,"Gdi32.lib") // wingdi.h (include Windows.h)
#define ID_TIMER 1
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
VOID CALLBACK TimerProc (HWND, UINT, UINT, DWORD ) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName[] = TEXT ("Beeper2") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
hwnd = CreateWindow (szAppName, TEXT ("Beeper2 Timer Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
return msg.wParam ;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
switch (message)
case WM_CREATE:
SetTimer (hwnd, ID_TIMER, 1000, (TIMERPROC)TimerProc) ;
return 0 ;
case WM_DESTROY:
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage (0) ;
return 0 ;
return DefWindowProc (hwnd, message, wParam, lParam) ;
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
static BOOL fFlipFlop = FALSE ;
HBRUSH hBrush ;
HDC hdc ;
RECT rc ;
MessageBeep (-1) ;
fFlipFlop = !fFlipFlop ;
GetClientRect (hwnd, &rc) ;
hdc = GetDC (hwnd) ;
hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) : RGB(0,0,255)) ;
FillRect (hdc, &rc, hBrush) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
附录 tasks.json
{
"version": "2.0.0",
"tasks": [
"type": "cppbuild",
"label": "MSVC19 cl.exe build active file",
"command": "C:\\vs19btool\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX64\\x64\\cl.exe",
"args": [
"/WL",
"/favor:AMD64",
"/GL",
"/Gy",
"/homeparams",
"/EHsc",
"ole32.lib", "user32.lib","kernel32.lib",
"libucrt.lib",
"/D_LARGEFILE_SOURCE",
"/D_FILE_OFFSET_BITS=64",
"/WX",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
"problemMatcher": [
"$msCompile"
"detail": "MSVC 2019 compiler: CL.EXE",
"group": {
"kind": "build",
"isDefault": true
"type": "shell",
"label": "MSVC11 cl.exe build active file",
"command": "C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/bin/x86_amd64/cl.exe",
"args": [
"/WL",
"/favor:AMD64",
"/GL",
"/Gy",
"/homeparams",
"/EHsc",
"ole32.lib",
"kernel32.lib",
"/D_LARGEFILE_SOURCE",
"/D_FILE_OFFSET_BITS=64",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
"problemMatcher": [
"$msCompile"
"group": "build",
"detail": "MSVC 2012 compiler: CL.EXE"
"type": "cppbuild",
"label": "MSYS2_64 C/C++: gcc.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"-Wall",
"-Wextra",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-lm",
"-fdiagnostics-color=always",
"-IC:/msys64/mingw64/include/gtk-3.0",
"-IC:/msys64/mingw64/include/pango-1.0",
"-IC:/msys64/mingw64/include",
"-IC:/msys64/mingw64/include/glib-2.0",
"-IC:/msys64/mingw64/lib/glib-2.0/include",
"-IC:/msys64/mingw64/include/harfbuzz",
"-IC:/msys64/mingw64/include/freetype2",
"-IC:/msys64/mingw64/include/libpng16",
"-IC:/msys64/mingw64/include/fribidi",
"-IC:/msys64/mingw64/include/cairo",
"-IC:/msys64/mingw64/include/lzo",
"-IC:/msys64/mingw64/include/pixman-1",
"-mms-bitfields",
"-IC:/msys64/mingw64/include/gdk-pixbuf-2.0",
"-IC:/msys64/mingw64/include/atk-1.0",
"-lgtk-3",
"-lgdk-3",
"-lz",
"-lgdi32",
"-limm32",
"-lshell32",
"-lole32",
"-luuid",
"-lwinmm",
"-ldwmapi",
"-lsetupapi",
"-lcfgmgr32",
"-lpangowin32-1.0",
"-lpangocairo-1.0",
"-lpango-1.0",
"-lharfbuzz",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lglib-2.0",
"-lintl"
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
"problemMatcher": [
"$gcc"
"detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe",
"group": "build"
"type": "cppbuild",
"label": "MSYS2 GCC_10 C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"-Wall",
"-Wextra",
"-mms-bitfields",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-lm",
"-lole32",
"-lkernel32",
"-lucrt",
"-D_FILE_OFFSET_BITS=64",
"-D_LARGEFILE64_SOURCE",
"-fdiagnostics-color=always",
"-IC:/msys64/mingw64/include/gtk-3.0",
"-IC:/msys64/mingw64/include/pango-1.0",
"-IC:/msys64/mingw64/include",
"-IC:/msys64/mingw64/include/glib-2.0",
"-IC:/msys64/mingw64/lib/glib-2.0/include",
"-IC:/msys64/mingw64/include/harfbuzz",
"-IC:/msys64/mingw64/include/freetype2",
"-IC:/msys64/mingw64/include/libpng16",
"-IC:/msys64/mingw64/include/fribidi",
"-IC:/msys64/mingw64/include/cairo",
"-IC:/msys64/mingw64/include/lzo",
"-IC:/msys64/mingw64/include/pixman-1",
"-mms-bitfields",
"-IC:/msys64/mingw64/include/gdk-pixbuf-2.0",
"-IC:/msys64/mingw64/include/atk-1.0",
"-lgtk-3",
"-lgdk-3",
"-lz",
"-lgdi32",
"-limm32",
"-lshell32",
"-lole32",
"-luuid",
"-lwinmm",
"-ldwmapi",
"-lsetupapi",
"-lcfgmgr32",
"-lpangowin32-1.0",
"-lpangocairo-1.0",
"-lpango-1.0",
"-lharfbuzz",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lglib-2.0",
"-lintl"
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
"problemMatcher": [
"$gcc"
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe",
"group": {
"kind": "build",
"isDefault": true
}
////////////////////////////////////////////////////////////////////
// 注释掉 /WX
"type": "cppbuild",
"label": "MSVC19 cl.exe build active file",
"command": "C:\\vs19btool\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX64\\x64\\cl.exe",
"args": [
"/WL",
"/favor:AMD64",
"/GL",
"/Gy",
"/homeparams",
"/EHsc",
//"ole32.lib", "user32.lib","kernel32.lib",
//"libucrt.lib",
//"/D_LARGEFILE_SOURCE",
//"/D_FILE_OFFSET_BITS=64",
//"/WX",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
"problemMatcher": [
"$msCompile"
"detail": "MSVC 2019 compiler: CL.EXE",
"group": {
"kind": "build",
"isDefault": true
/*----------------------------------------
BTNLOOK.C -- Button Look Program
(c) Charles Petzold, 1998
----------------------------------------*/
#include <windows.h>
#pragma comment(lib,"Ole32.Lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
//Winmm.lib
#pragma comment(lib,"Winmm.lib")
#pragma comment(lib,"Gdi32.lib") // wingdi.h (include Windows.h)
struct
int iStyle ;
TCHAR * szText ;
button[] =
BS_PUSHBUTTON, TEXT ("PUSHBUTTON"),
BS_DEFPUSHBUTTON, TEXT ("DEFPUSHBUTTON"),
BS_CHECKBOX, TEXT ("CHECKBOX"),
BS_AUTOCHECKBOX, TEXT ("AUTOCHECKBOX"),
BS_RADIOBUTTON, TEXT ("RADIOBUTTON"),
BS_3STATE, TEXT ("3STATE"),
BS_AUTO3STATE, TEXT ("AUTO3STATE"),
BS_GROUPBOX, TEXT ("GROUPBOX"),
BS_AUTORADIOBUTTON, TEXT ("AUTORADIO"),
BS_OWNERDRAW, TEXT ("OWNERDRAW")
#define NUM (sizeof button / sizeof button[0])
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName[] = TEXT ("BtnLook") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
hwnd = CreateWindow (szAppName, TEXT ("Button Look"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
return msg.wParam ;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
static HWND hwndButton[NUM] ;
static RECT rect ;
static TCHAR szTop[] = TEXT ("message wParam lParam"),
szUnd[] = TEXT ("_______ ______ ______"),
szFormat[] = TEXT ("%-16s%04X-%04X %04X-%04X"),
szBuffer[50] ;
static int cxChar, cyChar ;
HDC hdc ;
PAINTSTRUCT ps ;
int i ;
switch (message)
case WM_CREATE :
cxChar = LOWORD (GetDialogBaseUnits ()) ;
cyChar = HIWORD (GetDialogBaseUnits ()) ;
HWND CreateWindow(
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu or child-window identifier
HANDLE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
for (i = 0 ; i < NUM ; i++)
hwndButton[i] = CreateWindow ( TEXT("button"), button[i].szText,
( WS_CHILD | WS_VISIBLE |button[i].iStyle ), cxChar, cyChar * (1 + 2 * i),
20 * cxChar, 7 * cyChar / 4, hwnd, (HMENU) i,
((LPCREATESTRUCT) lParam)->hInstance , NULL ) ;
return 0 ;
case WM_SIZE :
rect.left = 24 * cxChar ;
rect.top = 2 * cyChar ;
rect.right = LOWORD (lParam) ;
rect.bottom = HIWORD (lParam) ;
return 0 ;
case WM_PAINT :
InvalidateRect (hwnd, &rect, TRUE) ;
hdc = BeginPaint (hwnd, &ps) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
SetBkMode (hdc, TRANSPARENT) ;
TextOut (hdc, 24 * cxChar, cyChar, szTop, lstrlen (szTop)) ;
TextOut (hdc, 24 * cxChar, cyChar, szUnd, lstrlen (szUnd)) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DRAWITEM :
case WM_COMMAND :
ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;
hdc = GetDC (hwnd) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),
szBuffer,
wsprintf (szBuffer, szFormat,
message == WM_DRAWITEM ? TEXT ("WM_DRAWITEM") :
TEXT ("WM_COMMAND"),
HIWORD (wParam), LOWORD (wParam),
HIWORD (lParam), LOWORD (lParam))) ;
ReleaseDC (hwnd, hdc) ;
ValidateRect (hwnd, &rect) ;
break ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
64 BIT SYSTEM:
/*----------------------------------------
COLORS1.C -- Colors Using Scroll Bars
(c) Charles Petzold, 1998
----------------------------------------*/
#include <windows.h>
#pragma comment(lib,"Ole32.Lib")
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
//#pragma comment(lib,"oleaut32.lib") //COM Automation
//#pragma comment(lib,"user32.lib")
#pragma comment(lib,"Winmm.lib")
#pragma comment(lib,"Gdi32.lib") // wingdi.h (include Windows.h) Gdi32.lib
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ScrollProc (HWND, UINT, WPARAM, LPARAM) ;
int idFocus ;
WNDPROC OldScroll[3] ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName[] = TEXT ("Colors1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = CreateSolidBrush (0) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
hwnd = CreateWindow (szAppName, TEXT ("Color Scroll"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
return msg.wParam ;
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
static COLORREF crPrim[3] = { RGB (255, 0, 0), RGB (0, 255, 0),
RGB (0, 0, 255) } ;
static HBRUSH hBrush[3], hBrushStatic ;
static HWND hwndScroll[3], hwndLabel[3], hwndValue[3], hwndRect ;
static int color[3], cyChar ;
static RECT rcColor ;
static TCHAR * szColorLabel[] = { TEXT ("Red"), TEXT ("Green"),
TEXT ("Blue") } ;
HINSTANCE hInstance ;
int i, cxClient, cyClient ;
TCHAR szBuffer[10] ;
switch (message)
case WM_CREATE :
hInstance = (HINSTANCE) GetWindowLong (hwnd, GWLP_HINSTANCE) ;
// Create the white-rectangle window against which the
// scroll bars will be positioned. The child window ID is 9.
hwndRect = CreateWindow (TEXT ("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_WHITERECT,
0, 0, 0, 0,
hwnd, (HMENU) 9, hInstance, NULL) ;
for (i = 0 ; i < 3 ; i++)
// The three scroll bars have IDs 0, 1, and 2, with
// scroll bar ranges from 0 through 255.
hwndScroll[i] = CreateWindow (TEXT ("scrollbar"), NULL,
WS_CHILD | WS_VISIBLE |
WS_TABSTOP | SBS_VERT,
0, 0, 0, 0,
hwnd, (HMENU) i, hInstance, NULL) ;
SetScrollRange (hwndScroll[i], SB_CTL, 0, 255, FALSE) ;
SetScrollPos (hwndScroll[i], SB_CTL, 0, FALSE) ;
// The three color-name labels have IDs 3, 4, and 5,
// and text strings "Red", "Green", and "Blue".
hwndLabel [i] = CreateWindow (TEXT ("static"), szColorLabel[i],
WS_CHILD | WS_VISIBLE | SS_CENTER,
0, 0, 0, 0,
hwnd, (HMENU) (i + 3),
hInstance, NULL) ;
// The three color-value text fields have IDs 6, 7,
// and 8, and initial text strings of "0".
hwndValue [i] = CreateWindow (TEXT ("static"), TEXT ("0"),
WS_CHILD | WS_VISIBLE | SS_CENTER,
0, 0, 0, 0,
hwnd, (HMENU) (i + 6),
hInstance, NULL) ;
OldScroll[i] = (WNDPROC) SetWindowLong (hwndScroll[i],
GWLP_WNDPROC, (LONG) ScrollProc) ;
hBrush[i] = CreateSolidBrush (crPrim[i]) ;
hBrushStatic = CreateSolidBrush (
GetSysColor (COLOR_BTNHIGHLIGHT)) ;
cyChar = HIWORD (GetDialogBaseUnits ()) ;
return 0 ;
case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
SetRect (&rcColor, cxClient / 2, 0, cxClient, cyClient) ;
MoveWindow (hwndRect, 0, 0, cxClient / 2, cyClient, TRUE) ;
for (i = 0 ; i < 3 ; i++)
MoveWindow (hwndScroll[i],
(2 * i + 1) * cxClient / 14, 2 * cyChar,
cxClient / 14, cyClient - 4 * cyChar, TRUE) ;
MoveWindow (hwndLabel[i],
(4 * i + 1) * cxClient / 28, cyChar / 2,
cxClient / 7, cyChar, TRUE) ;
MoveWindow (hwndValue[i],
(4 * i + 1) * cxClient / 28,
cyClient - 3 * cyChar / 2,
cxClient / 7, cyChar, TRUE) ;
SetFocus (hwnd) ;
return 0 ;
case WM_SETFOCUS :
SetFocus (hwndScroll[idFocus]) ;
return 0 ;
case WM_VSCROLL :
i = GetWindowLong ((HWND) lParam, GWL_ID) ;
switch (LOWORD (wParam))
case SB_PAGEDOWN :
color[i] += 15 ;
// fall through
case SB_LINEDOWN :
color[i] = min (255, color[i] + 1) ;
break ;
case SB_PAGEUP :
color[i] -= 15 ;
// fall through
case SB_LINEUP :
color[i] = max (0, color[i] - 1) ;
break ;
case SB_TOP :
color[i] = 0 ;
break ;
case SB_BOTTOM :
color[i] = 255 ;
break ;
case SB_THUMBPOSITION :
case SB_THUMBTRACK :
color[i] = HIWORD (wParam) ;
break ;
default :
break ;
SetScrollPos (hwndScroll[i], SB_CTL, color[i], TRUE) ;
wsprintf (szBuffer, TEXT ("%i"), color[i]) ;
SetWindowText (hwndValue[i], szBuffer) ;
DeleteObject ((HBRUSH)
SetClassLong (hwnd, GCLP_HBRBACKGROUND, (LONG)
CreateSolidBrush (RGB (color[0], color[1], color[2])))) ;
InvalidateRect (hwnd, &rcColor, TRUE) ;
return 0 ;
case WM_CTLCOLORSCROLLBAR :
i = GetWindowLong ((HWND) lParam, GWL_ID) ;
return (LRESULT) hBrush[i] ;
case WM_CTLCOLORSTATIC :
i = GetWindowLong ((HWND) lParam, GWL_ID) ;
if (i >= 3 && i <= 8) // static text controls
SetTextColor ((HDC) wParam, crPrim[i % 3]) ;
SetBkColor ((HDC) wParam, GetSysColor (COLOR_BTNHIGHLIGHT));
return (LRESULT) hBrushStatic ;
break ;
case WM_SYSCOLORCHANGE :
DeleteObject (hBrushStatic) ;
hBrushStatic = CreateSolidBrush (GetSysColor (COLOR_BTNHIGHLIGHT)) ;
return 0 ;
case WM_DESTROY :
DeleteObject ((HBRUSH)
SetClassLong (hwnd, GCLP_HBRBACKGROUND, (LONG)
GetStockObject (WHITE_BRUSH))) ;
for (i = 0 ; i < 3 ; i++)
DeleteObject (hBrush[i]) ;
DeleteObject (hBrushStatic) ;
PostQuitMessage (0) ;
return 0 ;
return DefWindowProc (hwnd, message, wParam, lParam) ;
LRESULT CALLBACK ScrollProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
int id = GetWindowLong (hwnd, GWL_ID) ;
switch (message)
case WM_KEYDOWN :
if (wParam == VK_TAB)
SetFocus (GetDlgItem (GetParent (hwnd),
(id + (GetKeyState (VK_SHIFT) < 0 ? 2 : 1)) % 3)) ;
break ;
case WM_SETFOCUS :
idFocus = id ;
break ;