1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| #include <windows.h>
extern HPALETTE CreateRoutine (HWND);
extern void PaintRoutine (HDC, int, int);
extern void TimerRoutine (HDC, HPALETTE);
extern void DestroyRoutine(HWND, HPALETTE);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
extern TCHAR szAppName[];
extern TCHAR szTitle[];
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
if (!RegisterClass(&wc))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (!hwnd)
return 0;
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
BOOL CheckDisplay(HWND hwnd)
{
HDC hDC;
int iPalSize;
hDC = GetDC(hwnd);
iPalSize = GetDeviceCaps(hDC, SIZEPALETTE);
ReleaseDC(hwnd, hDC);
if (iPalSize != 256)
{
MessageBox(hwnd, TEXT("This program requires that the video ")
TEXT("display mode have a 256-color palette."),
szAppName, MB_ICONERROR);
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HPALETTE hPalette;
static int cxClient, cyClient;
HDC hDC;
PAINTSTRUCT ps;
switch (uMsg)
{
case WM_CREATE:
if (!CheckDisplay(hwnd))
return -1;
hPalette = CreateRoutine(hwnd);
return 0;
case WM_DISPLAYCHANGE:
if (!CheckDisplay(hwnd))
DestroyWindow(hwnd);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hDC = BeginPaint(hwnd, &ps);
SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
PaintRoutine(hDC, cxClient, cyClient);
EndPaint(hwnd, &ps);
return 0;
case WM_TIMER:
hDC = GetDC(hwnd);
SelectPalette(hDC, hPalette, FALSE);
TimerRoutine(hDC, hPalette);
ReleaseDC(hwnd, hDC);
return 0;
case WM_QUERYNEWPALETTE:
if (!hPalette)
return FALSE;
hDC = GetDC(hwnd);
SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
InvalidateRect(hwnd, NULL, TRUE);
ReleaseDC(hwnd, hDC);
return TRUE;
case WM_PALETTECHANGED:
if (!hPalette || (HWND)wParam == hwnd)
break;
hDC = GetDC(hwnd);
SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
UpdateColors(hDC);
ReleaseDC(hwnd, hDC);
break;
case WM_DESTROY:
DestroyRoutine(hwnd, hPalette);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
} |
Partager