Windows API Primer   線と図形と文字の描画

ホーム   目次

Figure

figure.c


#include <windows.h>

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

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         = NULL;
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = TEXT("FIGURE");

  RegisterClass(&wc);

  hwnd             = CreateWindow(TEXT("FIGURE"),
				  TEXT("Windows API Primer"),
				  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
				  CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
				  NULL, NULL, hInstance, NULL);

  while (GetMessage(&msg, NULL, 0, 0))
    {
      DispatchMessage(&msg);
    }

  return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  HDC          hdc;
  RECT         rect;
  PAINTSTRUCT  ps;
  static POINT points[5];
  
  switch (msg)
    {
    case WM_CREATE:
      points[0].x = 110; points[0].y = 110;
      points[1].x = 125; points[1].y = 150;
      points[2].x =  90; points[2].y = 125;
      points[3].x = 130; points[3].y = 125;
      points[4].x =  95; points[4].y = 150;
      break;
    case WM_PAINT:
      hdc = BeginPaint(hwnd, &ps);
      Rectangle(hdc,    10,  10,  70,  50);
      RoundRect(hdc,    80,  10, 140,  50,  10,  10);
      SetRect  (&rect, 150,  10, 210,  50);
      FillRect (hdc, &rect, (HBRUSH)GetStockObject(GRAY_BRUSH));
      SetRect  (&rect, 220,  10, 280,  50);
      FrameRect(hdc, &rect, (HBRUSH)GetStockObject(GRAY_BRUSH));
      Ellipse  (hdc,    10,  60,  70, 100);
      Ellipse  (hdc,    90,  60, 130, 100);
      Arc      (hdc,   150,  60, 210, 100, 200,  70, 200,  90);
      Pie      (hdc,   220,  60, 280, 100, 270,  70, 270,  90);
      Chord    (hdc,    10, 110,  70, 150,  60, 120,  60, 140);
      Polygon  (hdc,   points,     5 );
      MoveToEx (hdc,   150, 110, NULL);
      LineTo   (hdc,   210, 110);
      LineTo   (hdc,   150, 150);
      LineTo   (hdc,   210, 150);
      TextOut  (hdc,   230, 120, TEXT("Hello"), lstrlen(TEXT("Hello")));
      EndPaint(hwnd, &ps);
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
    

実行結果


49431 visits
Posted: Jun. 07, 2020
Update: Jun. 08, 2020

ホーム   目次