본문 바로가기

공부/윈도우api

[윈도우 api] 출력

반응형

DC(device context)

윈도우 세가지 동적 라이브러리(DLL) = 윈도우 API함수의 대부분은 DLL에 의해 제공

메모리 관리, 프로그램 실행 = KERNEL

유저 인터페이스와 윈도우를 관리 = USER

화면 처리와 그래픽(그림, 글자)을 담당 = GDI

 

DC(device context) -> 출력에 필요한 모든 정보를 가지는 데이터 구조체, GDI 모듈에 의해 관리됨

출력 함수에서 맨 처음 인자로 사용됨

 

DC 필요한 이유

1. 그리기 함수에서 원점을 정확히 알아야 함

2. 그리기를 할 때 그리기가 금지된 부분에 출력을 하지 말아야함

    (예: 두 윈도우가 겹칠 시 앞의 윈도우가 뒤의 윈도우의 모습을 덮어서 화면에 출력되어야 함)

 

DC 얻는 법

1. HDC GetDC(HWND hWnd);

   int ReleaseDC(HWND hWnd, HDC hDC);

2. HDC BeginPaint(HWND hwnd, LPPAINTSTRUCT lpPaint);

   BOOL EndPaint(HWND hWnd, CONST PAINTSTRUCT *lpPaint);

   (PAINTSTRUCT 구조체는 그림 그리기에 필요한 여러 정보를 가짐, 그리기 속도를 비약적으로 향상시키

    는 정보가 들어 있음) 

(주의 : WM_PAINT 메세지 루틴에서만 사용가능)

 

문자열 출력

UINT SetTextAlign(HDC hdc, UINT fMode);

문자열 정렬 설정

 

BOOL TextOut(HDC hdc, int nXStart, int nYstart, LPCTSTR lpString, int cbString);

문자열 출력

 

int DrwaText(HDC hDC, LPCTSRT lpString, int nCount, LPRECT lpRect, UINT uFormat);

조금 더 복잡한(=기능이 많은) 문자열 출력 함수

 

그래픽 출력

COLORREF SetPixel(hdc, nXPos, nYPos, clrref);

(nXpos, nYPos)좌표에 clrref색상으로 점을 출력

 

DWORD MoveToEx(hdc, x, y, lpPoint);

CP(출력할 커서의 현재 위치)를 지정한 좌표(x, y)로 이동, 이전의 CP좌료를 lpPiont에 대입

 

BOOL LineTo(hdc, xEnd, yEnd)

CP의 위치에서 (xEnd, yEnd)좌표까지 선을 긋음

 

BOOL Rectangle(hdc, nLeftRect, nTopRect, nRightRect, nBottomRect)

지정한 두 점 (nLeftRect, nTopRect)과 (nRightRect, nBottomRect)을 대각선으로 하는 사각형을 그림

 

BOOL Ellipse(hdc, nLeftRect, nTopRect, nRightRect, nBottomRect)

지정한 두 점 (nLeftRect, nTopRect)과 (nRightRect, nBottomRect)을 대각선으로 하는 사각형의 내접하는 타원을 그림

 

메세지 박스

int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

 

메시지 비프

BOOL MessageBeep(UINT uType);

 

소스

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass=TEXT("GraphOut");

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpszCmdParam, int nCmdShow)
{
 HWND hWnd;
 MSG Message;
 WNDCLASS WndClass;
 g_hInst = hInstance;

 WndClass.cbClsExtra = 0;
 WndClass.cbWndExtra = 0;
 WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 WndClass.hInstance = hInstance;
 WndClass.lpfnWndProc = WndProc;
 WndClass.lpszClassName = lpszClass;
 WndClass.lpszMenuName = NULL;
 WndClass.style = CS_HREDRAW | CS_VREDRAW;
 RegisterClass(&WndClass);
 hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HMENU)NULL, hInstance, NULL);
 ShowWindow(hWnd, nCmdShow);

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

 return (int)Message.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps;
 int i = 0;

 switch (iMessage) {
  case WM_DESTROY:
   PostQuitMessage(0);
   return 0;
  case WM_PAINT:
   hdc = BeginPaint(hWnd, &ps);
   for(i = 0; i < 400; i++)
    SetPixel(hdc, i, 200, RGB(255, 0, 0));
   MoveToEx(hdc, 50, 50, NULL);
   LineTo(hdc, 300, 90);
   Rectangle(hdc, 50, 100, 200, 180);
   Ellipse(hdc, 220, 50, 400, 200);
   EndPaint(hWnd, &ps);
   return 0;
 }
 return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}

 

 

 

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("MessageBox");

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
 HWND hWnd;
 MSG Message;
 WNDCLASS WndClass;
 g_hInst = hInstance;

 WndClass.cbClsExtra = 0;
 WndClass.cbWndExtra = 0;
 WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 WndClass.hInstance = hInstance;
 WndClass.lpfnWndProc = WndProc;
 WndClass.lpszClassName = lpszClass;
 WndClass.lpszMenuName = NULL;
 WndClass.style = CS_HREDRAW | CS_VREDRAW;
 RegisterClass(&WndClass);

 hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HMENU)NULL, hInstance, NULL);
 ShowWindow(hWnd, nCmdShow);

 while(GetMessage(&Message, NULL, 0, 0)) {
  TranslateMessage(&Message);
  DispatchMessage(&Message);
 }
 return (int)Message.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
 switch(iMessage) {
  case WM_LBUTTONDOWN:
   MessageBox(hWnd, TEXT("마우스 왼쪽 버튼을 눌렀습니다"), TEXT("메시지 박스"), MB_OK);
   return 0;
  case WM_DESTROY:
   PostQuitMessage(0);
   return 0;
 }
 return (DefWindowProc(hWnd, iMessage, wParam, lParam));
}

반응형