프로그래밍/WIN32 API

Pen, Brush

redjam0123 2017. 4. 6. 01:00

2015.03.21. 01:13

*BeginPaint/EndPaint 는 WM_PAINT메세지에서만 사용 할 수 있다.

GetDC/ReleaseDC는 그외 메세지에서 사용가능!

둘의 차이점: 무효화영역(다른 윈도우에 의해 가려지거나, 최소화 등)이 발생할때,

BeginPaint는 다시 그려져서 유지되는것처럼 보이고 GetDC는 다시 그려지지 않아서 사라짐​

*Pen: 테두리 Brush:안쪽 채우기​

//////////////////////////////////////////Pen​

​#include <windows.h>
#include <tchar.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR strCmdLine, int nShowCmd)
{
 WNDCLASSEX wcex;
 memset(&wcex, 0, sizeof(wcex));

 wcex.cbSize             = sizeof(wcex);
 wcex.style             = (CS_HREDRAW | CS_VREDRAW);
 wcex.hbrBackground       = (HBRUSH)GetStockObject(WHITE_BRUSH);
 wcex.hCursor          = LoadCursor(NULL, IDC_ARROW);
 wcex.hIcon             = LoadIcon(NULL, IDI_APPLICATION);
 wcex.hInstance          = hInstance;
 wcex.cbClsExtra          = 0;
 wcex.cbWndExtra          = 0;
 wcex.hIconSm          = NULL;
 wcex.lpszMenuName       = NULL;
 wcex.lpfnWndProc       = WndProc;
 wcex.lpszClassName       = L"MyWindow";
 RegisterClassEx(&wcex);

 HWND hWnd = CreateWindow(
  L"MyWindow", L"MyWindow",
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  CW_USEDEFAULT, CW_USEDEFAULT,
  NULL,
  NULL,
  hInstance,
  NULL
  );

 if(hWnd == NULL)
 {
  MessageBox(NULL, L"윈도우 생성 실패", L"에러", MB_ICONERROR | MB_OK);
  return -1;
 }

 ShowWindow(hWnd, SW_SHOW);

 MSG msg;
 memset(&msg, 0, sizeof(msg));
 while(GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
 switch (uMsg)
 {
 case WM_PAINT:
  {
   HDC hDC;
   PAINTSTRUCT ps;
   HPEN hMyPen, hPrevPen;

   hDC = BeginPaint( hWnd, &ps);

   hMyPen = CreatePen(PS_DASHDOTDOT, 1, RGB(150, 100, 200));
   //HPEN CreatePen(INT fnPenStyle, INT nWidth, COLORREF color)
   //(PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT, PS_DASHDOTDOT
   //선의 굵기
   //선의색상)
   hPrevPen = (HPEN)SelectObject(hDC, hMyPen);

   Rectangle(hDC, 50, 50, 200, 200);
   SelectObject(hDC, hPrevPen);
   DeleteObject(hMyPen);

   EndPaint(hWnd, &ps);

   break;
  }

 case WM_DESTROY :
  {
   PostQuitMessage(0);
   break;
  }
 }

 return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

//////////////////////////////////////////Brush

include <windows.h>
#include <tchar.h>

 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR strCmdLine, int nShowCmd)
{
 WNDCLASSEX wcex;
 memset(&wcex, 0, sizeof(wcex));

 wcex.cbSize             = sizeof(wcex);
 wcex.style             = (CS_HREDRAW | CS_VREDRAW);
 wcex.hbrBackground       = (HBRUSH)GetStockObject(WHITE_BRUSH);
 wcex.hCursor          = LoadCursor(NULL, IDC_ARROW);
 wcex.hIcon             = LoadIcon(NULL, IDI_APPLICATION);
 wcex.hInstance          = hInstance;
 wcex.cbClsExtra          = 0;
 wcex.cbWndExtra          = 0;
 wcex.hIconSm          = NULL;
 wcex.lpszMenuName       = NULL;
 wcex.lpfnWndProc       = WndProc;
 wcex.lpszClassName       = L"MyWindow";
 RegisterClassEx(&wcex);

 HWND hWnd = CreateWindow(
  L"MyWindow", L"MyWindow",
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  CW_USEDEFAULT, CW_USEDEFAULT,
  NULL,
  NULL,
  hInstance,
  NULL
  );

 if(hWnd == NULL)
 {
  MessageBox(NULL, L"윈도우 생성 실패", L"에러", MB_ICONERROR | MB_OK);
  return -1;
 }

 ShowWindow(hWnd, SW_SHOW);

 MSG msg;
 memset(&msg, 0, sizeof(msg));
 while(GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
 switch (uMsg)
 {
 case WM_PAINT:
  {
   HDC hDC;
   PAINTSTRUCT ps;
   HBRUSH hMyBrush, hPrevBrush;

   hDC = BeginPaint( hWnd, &ps);

   hMyBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
   hPrevBrush = (HBRUSH)SelectObject(hDC, hMyBrush);

   Rectangle(hDC, 50, 50, 300, 200);
   SelectObject(hDC, hPrevBrush);
   DeleteObject(hMyBrush);

   EndPaint(hWnd, &ps);

   break;
  }
  //case WM_PAINT:
  // {
  //  HDC hDC;
  //  PAINTSTRUCT ps;

  //  hDC = BeginPaint(hWnd, &ps);
  //  //HWND로부터 만들어낸 HDC
  //  //HDC를 얻어올 윈도우핸들
  //  //LPPAINTSTRUCT 무효화영역을 얻어올수 있따

  //  Rectangle(hDC, 50, 50, 300, 200);//네모(Subject, Left:왼쪽부터x좌표, Top:위에서부터y좌표, Right:오른쪽부터x좌표, Bottom:아래부터y좌표)
  //  //hDC -> Pen(BlackColor), Brush(WhiteColor)

  //  EndPaint(hWnd, &ps);
  //  //BOOL 항상False리턴
  //  //hwnd, 반환받을 dc를 받을 hwnd
  //  
  //  break;
  // }

 case WM_DESTROY :
  {
   PostQuitMessage(0);
   break;
  }
 }

 return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

//HBRUSH CreateSolidBrush(COLORREF color)
//채움색
//HBRUSH CreateHatchBrush(int fnStyle, COLORREF color)
//
//채움색​

'프로그래밍 > WIN32 API' 카테고리의 다른 글

1,2일차 기본  (0) 2017.04.06
마우스좌표 나타내기  (0) 2017.04.06
Basic  (0) 2017.04.06
Text Out  (0) 2017.04.06