API code

时间:2023-01-13 07:29:18
 /*---------------------------------------------------
BLOKOUT2.C -- Mouse Button & Capture Demo Program
(c) Charles Petzold, 1998
---------------------------------------------------*/ #include <windows.h>
#include <windowsx.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("BlokOut2") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
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 ;
} hwnd = CreateWindow (szAppName, TEXT ("Mouse Button & Capture Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, , ))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
} void DrawBoxOutline (HWND hwnd, POINT ptBeg, POINT ptEnd)
{
HDC hdc ; hdc = GetDC (hwnd) ; SetROP2 (hdc, R2_NOT) ;
SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ; ReleaseDC (hwnd, hdc) ;
} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL fBlocking, fValidBox ;
static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd ;
HDC hdc ;
PAINTSTRUCT ps ; switch (message)
{
case WM_LBUTTONDOWN :
ptBeg.x = ptEnd.x = GET_X_LPARAM (lParam) ;
ptBeg.y = ptEnd.y = GET_Y_LPARAM (lParam) ; DrawBoxOutline (hwnd, ptBeg, ptEnd) ; SetCapture (hwnd) ;
SetCursor (LoadCursor (NULL, IDC_CROSS)) ; fBlocking = TRUE ;
return ; case WM_MOUSEMOVE :
if (fBlocking)
{
SetCursor (LoadCursor (NULL, IDC_CROSS)) ; DrawBoxOutline (hwnd, ptBeg, ptEnd) ; ptEnd.x = GET_X_LPARAM (lParam) ;
ptEnd.y = GET_Y_LPARAM (lParam) ; DrawBoxOutline (hwnd, ptBeg, ptEnd) ;
}
return ; case WM_LBUTTONUP :
if (fBlocking)
{
DrawBoxOutline (hwnd, ptBeg, ptEnd) ; ptBoxBeg = ptBeg ;
ptBoxEnd.x = GET_X_LPARAM (lParam) ;
ptBoxEnd.y = GET_Y_LPARAM (lParam) ; ReleaseCapture () ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ; fBlocking = FALSE ;
fValidBox = TRUE ; InvalidateRect (hwnd, NULL, TRUE) ;
}
return ; case WM_CHAR :
if (fBlocking & (wParam == '\x1B')) // i.e., Escape
{
DrawBoxOutline (hwnd, ptBeg, ptEnd) ; ReleaseCapture () ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ; fBlocking = FALSE ;
}
return ; case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ; if (fValidBox)
{
SelectObject (hdc, GetStockObject (BLACK_BRUSH)) ;
Rectangle (hdc, ptBoxBeg.x, ptBoxBeg.y,
ptBoxEnd.x, ptBoxEnd.y) ;
} if (fBlocking)
{
SetROP2 (hdc, R2_NOT) ;
SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
Rectangle (hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y) ;
} EndPaint (hwnd, &ps) ;
return ; case WM_DESTROY :
PostQuitMessage () ;
return ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 /*-----------------------------------------
BEEPER1.C -- Timer Demo Program No. 1
(c) Charles Petzold, 1998
-----------------------------------------*/ #include <windows.h> #define ID_TIMER 1 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Beeper1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
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 ;
} hwnd = CreateWindow (szAppName, TEXT ("Beeper1 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, , ))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL fFlipFlop = FALSE ;
HBRUSH hBrush ;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rc ; switch (message)
{
case WM_CREATE:
SetTimer (hwnd, ID_TIMER, , NULL) ;
return ; case WM_TIMER :
MessageBeep (-) ;
fFlipFlop = !fFlipFlop ;
InvalidateRect (hwnd, NULL, FALSE) ;
return ; case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rc) ;
hBrush = CreateSolidBrush (fFlipFlop ? RGB(,,) : RGB(,,)) ;
FillRect (hdc, &rc, hBrush) ; EndPaint (hwnd, &ps) ;
DeleteObject (hBrush) ;
return ; case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage () ;
return ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 #include <windows.h>

 #define ID_TIMER    1

 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Beeper1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
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 ;
} hwnd = CreateWindow (szAppName, TEXT ("Beeper1 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, , ))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL fFlipFlop = FALSE ;
HBRUSH hBrush ;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rc ; switch (message)
{
case WM_CREATE:
SetTimer (hwnd, ID_TIMER, , NULL) ;
return ; case WM_TIMER :
MessageBeep (-) ;
fFlipFlop = !fFlipFlop ;
InvalidateRect (hwnd, NULL, FALSE) ;
return ; case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rc) ;
hBrush = CreateSolidBrush (fFlipFlop ? RGB(,,) : RGB(,,)) ;//red green blue
FillRect (hdc, &rc, hBrush) ; EndPaint (hwnd, &ps) ;
DeleteObject (hBrush) ;
return ; case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage () ;
return ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 /*-----------------------------------------
DIGCLOCK.c -- Digital Clock
(c) Charles Petzold, 1998
-----------------------------------------*/ #include <windows.h> #define ID_TIMER 1 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("DigClock") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = ;
wndclass.cbWndExtra = ;
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 ;
} hwnd = CreateWindow (szAppName, TEXT ("Digital Clock"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, , ))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
} void DisplayDigit (HDC hdc, int iNumber)
{
static BOOL fSevenSegment [][] = {
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , , //
, , , , , , } ; //
static POINT ptSegment [][] = {
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , } ;
int iSeg ; for (iSeg = ; iSeg < ; iSeg++)
if (fSevenSegment [iNumber][iSeg])
Polygon (hdc, ptSegment [iSeg], ) ;
} void DisplayTwoDigits (HDC hdc, int iNumber, BOOL fSuppress)
{
if (!fSuppress || (iNumber / != ))
DisplayDigit (hdc, iNumber / ) ; OffsetWindowOrgEx (hdc, -, , NULL) ;
DisplayDigit (hdc, iNumber % ) ;
OffsetWindowOrgEx (hdc, -, , NULL) ;
} void DisplayColon (HDC hdc)
{
POINT ptColon [][] = { , , , , , , , ,
, , , , , , , } ; Polygon (hdc, ptColon [], ) ;
Polygon (hdc, ptColon [], ) ; OffsetWindowOrgEx (hdc, -, , NULL) ;
} void DisplayTime (HDC hdc, BOOL f24Hour, BOOL fSuppress)
{
SYSTEMTIME st ; GetLocalTime (&st) ; if (f24Hour)
DisplayTwoDigits (hdc, st.wHour, fSuppress) ;
else
DisplayTwoDigits (hdc, (st.wHour %= ) ? st.wHour : , fSuppress) ; DisplayColon (hdc) ;
DisplayTwoDigits (hdc, st.wMinute, FALSE) ;
DisplayColon (hdc) ;
DisplayTwoDigits (hdc, st.wSecond, FALSE) ;
} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL f24Hour, fSuppress ;
static HBRUSH hBrushRed ;
static int cxClient, cyClient ;
HDC hdc ;
PAINTSTRUCT ps ;
TCHAR szBuffer [] ; switch (message)
{
case WM_CREATE:
hBrushRed = CreateSolidBrush (RGB (, , )) ;
SetTimer (hwnd, ID_TIMER, , NULL) ; // fall through case WM_SETTINGCHANGE:
GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_ITIME, szBuffer, ) ;
f24Hour = (szBuffer[] == '') ; GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_ITLZERO, szBuffer, ) ;
fSuppress = (szBuffer[] == '') ; InvalidateRect (hwnd, NULL, TRUE) ;
return ; case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return ; case WM_TIMER:
InvalidateRect (hwnd, NULL, TRUE) ;
return ; case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ; SetMapMode (hdc, MM_ISOTROPIC) ;
SetWindowExtEx (hdc, , , NULL) ;
SetViewportExtEx (hdc, cxClient, cyClient, NULL) ; SetWindowOrgEx (hdc, , , NULL) ;
SetViewportOrgEx (hdc, cxClient / , cyClient / , NULL) ; SelectObject (hdc, GetStockObject (NULL_PEN)) ;
SelectObject (hdc, hBrushRed) ; DisplayTime (hdc, f24Hour, fSuppress) ; EndPaint (hwnd, &ps) ;
return ; case WM_DESTROY:
KillTimer (hwnd, ID_TIMER) ;
DeleteObject (hBrushRed) ;
PostQuitMessage () ;
return ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}