Cプログラミンの一環として始めた Windos API ですが、やってみたら面白かったので、もっと詳しく、かつ簡単に、ステップバイステップで学習するコーナーも作りました。
Windows API Primer
Primer は、入門書という意味です。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void DrawColors(HWND);
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
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.lpszMenuName = NULL;
wc.lpszClassName = TEXT("Color");
RegisterClass (&wc);
hwnd = CreateWindow(TEXT("Color"),
TEXT("Color"),
WS_OVERLAPPEDWINDOW,
(GetSystemMetrics(SM_CXSCREEN) - 400) / 2,
(GetSystemMetrics(SM_CYSCREEN) - 250) / 2,
400,
250,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
DrawColors(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void DrawColors(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HPEN hPen = CreatePen(PS_NULL, 1, RGB( 0, 0, 0));
HPEN holdPen = SelectObject(hdc, hPen);
HBRUSH hBrush1 = CreateSolidBrush(RGB( 0, 255, 255));
HBRUSH hBrush2 = CreateSolidBrush(RGB(255, 0, 255));
HBRUSH hBrush3 = CreateSolidBrush(RGB(255, 255, 0));
HBRUSH hBrush4 = CreateSolidBrush(RGB(255, 0, 0));
HBRUSH hBrush5 = CreateSolidBrush(RGB( 0, 255, 0));
HBRUSH hBrush6 = CreateSolidBrush(RGB( 0, 0, 255));
HBRUSH holdBrush = SelectObject(hdc, hBrush1);
Rectangle(hdc, 20, 20, 120, 100);
SelectObject(hdc, hBrush2);
Rectangle(hdc, 140, 20, 240, 100);
SelectObject(hdc, hBrush3);
Rectangle(hdc, 260, 20, 360, 100);
SelectObject(hdc, hBrush4);
Rectangle(hdc, 20, 120, 120, 200);
SelectObject(hdc, hBrush5);
Rectangle(hdc, 140, 120, 240, 200);
SelectObject(hdc, hBrush6);
Rectangle(hdc, 260, 120, 360, 200);
EndPaint(hwnd, &ps);
}