코스웨어/15년 스마트컨트롤러
20151117_안향진_API_4
알 수 없는 사용자
2015. 11. 18. 01:26
==========================================================================================
API
==========================================================================================
<API>
=5-4 액셀러레이터
수정전 | 수정후 |
Menu1 | Menu&1\tCtrl+A |
Menu2 | Menu&2\tCtrl+B |
Exit | &Exit\tCtrl+C |

HACCEL hAccel; hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1)); while (GetMessage(&Message, 0, 0, 0)) { if (!TranslateAccelerator(hWnd, hAccel, &Message)) { TranslateMessage(&Message); DispatchMessage(&Message); } } |
1.
Alt+F => 메뉴
1,2,E 입력 => 메뉴1, 메뉴2, Exit 실행 됨
2.
Ctrl+A
Ctrl+B
Ctrl+E ==> 바로 실행 됨
=5-5 문자열 테이블

case WM_PAINT: hdc = BeginPaint(hWnd, &ps); LoadStringW(g_hInst, IDS_STRING101, tStr, 256); TextOut(hdc, 10, 10, tStr, lstrlen(tStr)); EndPaint(hWnd, &ps); return 0; |
=6-1-가. GDI오브젝트
GDI 오브젝트 | 핸들 타입 | 설명 | 디폴트 |
펜 | HPEN | 선을 그을 때 사용된다. | 검정색의 가는 선 |
브러시 | HBRUSH | 면을 채울 때 사용된다. | 흰색 |
폰트 | HFONT | 문자 출력에 사용되는 글꼴 | 시스템 글꼴 |
비트맵 | HBITMAP | 비트맵 이미지 | 선택되지 않음 |
팔레트 | HPALETTE | 팔레트 | 선택되지 않음 |
영역 | HRGN | 영역 | 선택되지 않음 |
=6-1-나. 스톡 오브젝트
HGDIOBJ GetStockObject( int fnObject ); => 자동으로 관리(생성, 파괴)
fnObject | 설명 |
BLACK_BRUSH | 검정색 브러시 |
GRAY_BRUSH | 회색 브러시 |
NULL_BRUSH | 투명 브러시 |
WHITE_BRUSH | 흰색 브러시 |
DKGRAY_BRUSH | 짙은 회색 브러시 |
LTGRAY_BRUSH | 옅은 회색 브러시 |
BLACK_PEN | 검정색 펜 |
WHITE_PEN | 흰색 펜 |
NULL_PEN | 투명 펜 |
ANSI_FIXED_FONT | 고정폭 폰트 |
ANSI_VAR_FONT | 가변폭 폰트 |
DEFAULT_PALETTE | 시스템 팔레트 |
OldBrush = (HBRUSH)SelectObject(hdc,MyBrush); |
첫번째 인수로 DC의 핸들을 주고 두번째 인수로 GDI 오브젝트의 핸들을 주면 DC에 해당 오브젝트를 선택해 준다. 이후부터 GDI는 그래픽을 출력할 때 선택된 오브젝트를 사용하게 된다. SelectObject가 리턴하는 값은 새로 선택되는 오브젝트 이전에 선택되어 있던 같은 종류의 오브젝트 핸들이다
HDC hdc; PAINTSTRUCT ps; HBRUSH MyBrush; HBRUSH OldBrush; switch (iMessage) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); MyBrush = (HBRUSH)GetStockObject(GRAY_BRUSH); OldBrush = (HBRUSH)SelectObject(hdc,MyBrush); Rectangle(hdc, 50, 50, 300, 200); SelectObject(hdc,OldBrush); EndPaint(hWnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } |

=C => Win32API + C++ ==> MFC APP
+MFC구조 구조
=6-1-다. 색상

#define RGB(r,g,b) ((COLORREF)(((BYTE)(r) | ((WORD)((BYTE)(g))<<8)) | (((DWORD)(BYTE)(b))<<16))) |
#define GetRValue(rgb) ((BYTE)(rgb)) #define GetGValue(rgb) ((BYTE)(((WORD)(rgb)) >> 8)) #define GetBValue(rgb) ((BYTE)((rgb)>>16)) |
=6-1-라. 펜

MyPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 255)); |

HPEN MyPen; HPEN OldPen;
switch (iMessage) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); MyBrush = (HBRUSH)GetStockObject(GRAY_BRUSH); OldBrush = (HBRUSH)SelectObject(hdc,MyBrush); MyPen = CreatePen(PS_DASH, 1, RGB(0, 0, 255)); OldPen = (HPEN)SelectObject(hdc, MyPen); Rectangle(hdc, 50, 50, 300, 200); SelectObject(hdc, OldPen); DeleteObject(MyPen); SelectObject(hdc,OldBrush); EndPaint(hWnd, &ps); return 0; |

=6-1-마. 브러시
값 | 설명 |
HS_BDIAGONAL | 좌하향 줄무늬 |
HS_CROSS | 바둑판 모양 |
HS_DIACROSS | 좌하향 및 우하향 줄무늬 |
HS_FDIAGONAL | 우하향 줄무늬 |
HS_HORIZONTAL | 수평선 |
HS_VERTICAL | 수직선 |
HBRUSH CreateSolidBrush( COLORREF crColor ); HBRUSH CreateHatchBrush( int fnStyle, COLORREF clrref );
HBRUSH MyBrush; HBRUSH OldBrush; HPEN MyPen; HPEN OldPen;
switch (iMessage) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); MyBrush = CreateHatchBrush(HS_BDIAGONAL, RGB(255,255,0)); OldBrush = (HBRUSH)SelectObject(hdc, MyBrush); MyPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 255)); OldPen = (HPEN)SelectObject(hdc, MyPen); Rectangle(hdc, 50, 50, 300, 200); SelectObject(hdc, OldPen); DeleteObject(MyPen); SelectObject(hdc, OldBrush); EndPaint(hWnd, &ps); return 0; |

=6-2-가. 흑백에서의 그리기 모드
=6-2-나. 그리기 모드의 종류
그리기 모드 | 설명 |
R2_BLACK | 항상 검정색이다. |
R2_WHITE | 항상 흰색이다. |
R2_NOP | 아무런 그리기도 하지 않는다. |
R2_NOT | 원래의 그림을 반전시킨다. |
R2_COPYPEN | 원래의 그림을 덮어버리고 새 그림을 그린다. |
R2_NOTCOPYPEN | 새 그림을 반전시켜 그린다. |
R2_MERGEPEN | OR연산으로 두 그림을 합친다. |
R2_MASKPEN | AND연산으로 겹치는 부분만 그린다. |
R2_XORPEN | XOR연산으로 겹치는 부분만 반전시킨다. |
=6-2-다. Ropmode
static int sx; static int sy; static int oldx; static int oldy; int ex; int ey; static BOOL bNowDraw = FALSE; HDC hdc;
switch (iMessage) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_LBUTTONDOWN: sx = LOWORD(lParam); sy = HIWORD(lParam); oldx = sx; oldy = sy; bNowDraw = TRUE; return 0; case WM_LBUTTONUP: bNowDraw = FALSE; hdc = GetDC(hWnd); MoveToEx(hdc, sx, sy, NULL); LineTo(hdc, oldx, oldy); ReleaseDC(hWnd, hdc); return 0; case WM_MOUSEMOVE: if (bNowDraw) { hdc = GetDC(hWnd); SetROP2(hdc, R2_NOT); MoveToEx(hdc, sx, sy, NULL); LineTo(hdc, oldx, oldy); ex = LOWORD(lParam); ey = HIWORD(lParam); MoveToEx(hdc, sx, sy, NULL); LineTo(hdc, ex, ey); oldx = ex; oldy = ey; ReleaseDC(hWnd, hdc); } return 0; } |


=6-3-가. 윈도우즈의 좌표체계
4/4분면
=6-3-나. 맵핑 모드
=6-3-다. 윈도우와 뷰포트
필요에 따라 원점을 변경시킬 수 있도록 다음 두 함수를 제공
BOOL SetViewportOrgEx( HDC hdc, int X, int Y, LPPOINT lpPoint );//픽셀 단위가 더 사용하기는 쉽다
BOOL SetWindowOrgEx( HDC hdc, int X, int Y, LPPOINT lpPoint );
HDC hdc; PAINTSTRUCT ps; double f; int y;
switch (iMessage) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); SetMapMode(hdc, MM_LOENGLISH); SetViewportOrgEx(hdc, 200, 150, NULL); MoveToEx(hdc, -2000, 0, NULL); LineTo(hdc, 2000, 0); MoveToEx(hdc, 0, -2000, NULL); LineTo(hdc, 0, 2000); for (f = -500; f < 1000; ++f) { y = (int)(sin(f*3.14 / 180) * 100); SetPixel(hdc, (int)f, y, RGB(0, 0, 0)); } EndPaint(hWnd, &ps); return 0; default: return(DefWindowProc(hWnd, iMessage, wParam, lParam)); } |

=6-4-가. bitmap.dsw
#define IDB_BITMAP1 101
올리기 : WM_PAINT
파괴 : WM_DESTROY
=> 속도 빨라짐

HDC hdc; HDC MemDC; PAINTSTRUCT ps; HBITMAP MyBitmap; HBITMAP OldBitmap; switch (iMessage) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); MemDC = CreateCompatibleDC(hdc); MyBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP1)); OldBitmap = (HBITMAP)SelectObject(MemDC, MyBitmap); BitBlt(hdc, 0, 0, 320, 320, MemDC, 0, 0, SRCCOPY); SelectObject(MemDC, OldBitmap); EndPaint(hWnd, &ps); return 0; case WM_DESTROY: DeleteDC(MemDC); PostQuitMessage(0); return 0; } return(DefWindowProc(hWnd, iMessage, wParam, lParam)); |
=6-4-나. 메모리 DC
-버퍼링
메모리에 먼저 그리고
=6-4-다. BitBlt
값 | 설명 |
BLACKNESS | 대상영역을 검정색으로 가득 채운다. |
DSTINVERT | 화면을 반전시킨다. |
MERGECOPY | 소스 비트맵과 대상 화면을 AND 연산한다. |
MERGEPAINT | 소스 비트맵과 대상 화면을 OR 연산한다. |
SRCCOPY | 소스 영역을 대상 영역에 복사한다. |
WHITENESS | 대상영역을 흰색으로 채운다. |
=6-4-라. StretchBlt
-확대/축소
BOOL StretchBlt( HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, DWORD dwRop ); |
StretchBlt( hdc, 0, 0, 500, 500, MemDC, 50, 50, 220, 220, SRCCOPY); |

=6-4-마.비트맵 만들기




=
1. 경계검사
2. 창모양 고정

hWnd = CreateWindow( lpszClass, lpszClass, //L"Change Name", TEXT("Change Name"), _T("Change Name") (WS_CAPTION | WS_SYSMENU /*| WS_OVERLAPPED | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VSCROLL | WS_HSCROLL*/), 100, 100, 500, 300, NULL, (HMENU)NULL, hInstance, NULL );
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) { HDC hdc; static HDC MemDC; PAINTSTRUCT ps; static HBITMAP MyBitmap; static HBITMAP OldBitmap; static int ixPos = 0; static int iyPos = 0; //static RECT rt;
switch (iMessage) { case WM_CREATE: hdc = GetDC(hWnd); MemDC = CreateCompatibleDC(hdc); MyBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP3)); OldBitmap = (HBITMAP)SelectObject(MemDC, MyBitmap); ReleaseDC(hWnd, hdc); //GetClientRect(hWnd, &rt); //rt.right = LOWORD(lParam); //rt.bottom = HIWORD(lParam); return 0; case WM_KEYDOWN: switch (wParam) { case VK_LEFT: if (0 == ixPos) { return 0; } if (8 > ixPos) { ixPos = 0; } ixPos = ixPos - 8; break; case VK_RIGHT: if (500 -16 - 48 == ixPos) { return 0; } if ((500 -16 - 48 - 8) < ixPos) { ixPos = 500 - 16- 48; } ixPos = ixPos + 8; break; case VK_UP: if (0 == iyPos) { return 0; } if (8 > iyPos) { iyPos = 0; } iyPos = iyPos - 8; break; case VK_DOWN: if ((300 - 38 - 48) == iyPos) { return 0; } if ((300 - 38 - 48 - 8) < iyPos) { iyPos = 300 - 38- 48; } iyPos = iyPos + 8;
break; } InvalidateRect(hWnd, NULL, TRUE); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); BitBlt(hdc, ixPos, iyPos, 48, 48, MemDC, 0, 0, SRCCOPY); EndPaint(hWnd, &ps); return 0; case WM_DESTROY: SelectObject(MemDC, OldBitmap); DeleteObject(MyBitmap); DeleteDC(MemDC); PostQuitMessage(0); return 0; } return(DefWindowProc(hWnd, iMessage, wParam, lParam)); } |

=
배경
FRONT
LEFT
RIGHT
BACK
#define WIDTH (720+16) #define HEIGHT (480+38) #define XTILE 48 #define YTILE 48 #define XMOV 8 #define YMOV 8 |
hWnd = CreateWindow( lpszClass, lpszClass, //L"Change Name", TEXT("Change Name"), _T("Change Name") (WS_CAPTION | WS_SYSMENU /*| WS_OVERLAPPED | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VSCROLL | WS_HSCROLL*/), 100, 100, WIDTH, HEIGHT, NULL, (HMENU)NULL, hInstance, NULL ); |
case WM_KEYDOWN: RtArea.top = iyPos; RtArea.bottom = iyPos + YTILE; RtArea.left = ixPos; RtArea.right = ixPos + XTILE; switch (wParam) { case VK_LEFT: if (XMOV >= ixPos) { RtArea.left = ixPos = 0; } else { RtArea.left = ixPos = ixPos - XMOV; } MyBitmap = LeftBitmap; break; case VK_RIGHT: if ((WIDTH - 16 - XTILE - XMOV) <= ixPos) { ixPos = WIDTH - 16 - XTILE; } else { ixPos = ixPos + XMOV; } RtArea.right = ixPos + XTILE; MyBitmap = RightBitmap; break; case VK_UP: if (YMOV >= iyPos) { RtArea.top = iyPos = 0; } else { RtArea.top = iyPos = iyPos - YMOV; } MyBitmap = BackBitmap; break; case VK_DOWN: if ((HEIGHT - 38 - YTILE - YMOV) <= iyPos) { iyPos = HEIGHT - 38 - YTILE; } else { iyPos = iyPos + YMOV; } RtArea.bottom = iyPos + YTILE; MyBitmap = FrontBitmap; break; } InvalidateRect(hWnd, &RtArea, TRUE); return 0; |
