多角形を描画するには、Polygon関数を使います。
内部はブラシで塗りつぶされます。
BOOL Polygon(
[in] HDC hdc,
[in] const POINT *apt,
[in] int cpt
);
1. [in] const POINT *apt,
頂点の配列へのポインターです。
2. [in] int cpt
頂点の数です。
〇プログラム例
#include <windows.h>
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
LPCWSTR lpString = TEXT("日本、こんにちは!");
static HBRUSH hBrush[2];
int i;
COLORREF color;
LONG R, G, B;
POINT pt1[] = { {120,50 }, { 10,200 }, { 320,150 }, };
switch (uMsg)
{
case WM_DESTROY:
for (i = 0; i < 1; i++)
DeleteObject(hBrush[i]);
PostQuitMessage(0);
break;
case WM_CREATE:
ShowWindow(hWnd, SW_SHOW);
hBrush[0] = CreateSolidBrush(RGB(0xFF, 0, 0xFF));
break;
case WM_LBUTTONDOWN: //マウスの左ボタンが押された
InvalidateRect(hWnd, NULL, TRUE); //クライアント領域を無効化する
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
SelectObject(hdc, hBrush[0]);
Polygon(hdc, pt1, 3);
color = GetPixel(hdc, 150, 150);
R = GetRValue(color);
G = GetGValue(color);
B = GetBValue(color);
EndPaint(hWnd, &ps);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0L;
}
int APIENTRY wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR ipCmdLine,
_In_ int nCmdShow
) {
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("TestWindow");
wcex.hIconSm = NULL;
RegisterClassExW(&wcex);
HWND hWnd = CreateWindowEx(0UL, TEXT("TestWindow"), TEXT("日本"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
〇実行結果
三角形を描画しています。内部は色(0xFF,0,0xFF)で塗りつぶされています。