오다기리 박의 알고리즘 노트

[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 5장 6번 풀이 본문

WIN32 API

[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 5장 6번 풀이

오다기리 박 2017. 11. 3. 21:31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <Windows.h>
#include<tchar.h>
#include<string.h>
#include<math.h>
#include"resource.h"
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
HINSTANCE hInst;
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    HWND hwnd;
    MSG msg;
    hInst = hInstance;
    WNDCLASS WndClass;
    HACCEL hAccel;
    hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_ACCELERATOR1));
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    WndClass.lpfnWndProc = WndProc;
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hInstance = hInstance;
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.lpszMenuName = NULL;
    WndClass.lpszClassName = _T("Window Class Name");
 
    RegisterClass(&WndClass);
 
    hwnd = CreateWindow(
        _T("Window Class Name"),
        _T("홍길동의 첫 번째 윈도우"),
        WS_OVERLAPPEDWINDOW,
        50,
        50,
        1000,
        700,
        NULL,
        NULL,
        hInstance,
        NULL);
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    while (GetMessage(&msg, NULL00))
    {
        if (!TranslateAccelerator(hwnd, hAccel, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int)msg.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    static HDC hdc, memdc;
    PAINTSTRUCT ps;
    HPEN hPen, oldPen;
    static HBITMAP hBit, oldBit;
    static int pasteX,pasteY,startX, startY, endX, endY;
    static bool regionSelect = false;
 
    switch (iMsg)
    {
    case WM_CREATE:
        break;
    case WM_LBUTTONDOWN:
        if (!regionSelect)
        {
            startX = LOWORD(lParam);
            startY = HIWORD(lParam);
        }
        else
        {
            pasteX = LOWORD(lParam);
            pasteY = HIWORD(lParam);
        }
        break;
    case WM_LBUTTONUP:
        if (!regionSelect)
        {
            endX = LOWORD(lParam);
            endY = HIWORD(lParam);
        }
        break;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case ID_ACCELERATOR40001://복사
            hdc = GetDC(hwnd);
            regionSelect = true;
            hBit = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP2));
            memdc = CreateCompatibleDC(hdc);
            oldBit = (HBITMAP)SelectObject(memdc, hBit);
            //MessageBox(hwnd, _T("복사키눌림"), _T("메시지박스"), MB_OK);
            break;
        case ID_ACCELERATOR40002://붙여넣기
            BitBlt(hdc, pasteX, pasteY, endX - startX, endY - startY, memdc, startX, startY, SRCCOPY);
            SelectObject(memdc, oldBit);
            ReleaseDC(hwnd, hdc);
            regionSelect = false;
            //MessageBox(hwnd, _T("붙여넣기키눌림"), _T("메시지박스"), MB_OK);
            break;
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        hBit = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP2));
        memdc = CreateCompatibleDC(hdc);
        oldBit = (HBITMAP)SelectObject(memdc, hBit);
        BitBlt(hdc, 001024768, memdc, 00, SRCCOPY);
        SelectObject(memdc, oldBit);
        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
 
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
cs