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

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

WIN32 API

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

오다기리 박 2017. 11. 3. 20:53
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#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);
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    HWND hwnd;
    MSG msg;
 
    WNDCLASS WndClass;
    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");
    WndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
 
    RegisterClass(&WndClass);
 
    hwnd = CreateWindow(
        _T("Window Class Name"),
        _T("홍길동의 첫 번째 윈도우"),
        WS_OVERLAPPEDWINDOW,
        100,
        100,
        1000,
        1000,
        NULL,
        NULL,
        hInstance,
        NULL);
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    while (GetMessage(&msg, NULL00))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    HPEN hPen, orgPen;
    static HFONT hFont;
    HBRUSH hBrush, orgBrush;
    CHOOSECOLOR COLOR;
    static CHOOSEFONT FONT;
    static LOGFONT LogFont;
    static COLORREF fColor;
    static RECT rect;
    static SIZE size;
    static COLORREF temp[16], PenColor = RGB(000), FaceColor = RGB(255255255);
    //static TCHAR tempstr[20];
    static bool isClick = false;
    static int mX1, mY1, mX2, mY2, count = 0, line = 0;
    static int Figure = 0;
    static TCHAR str[20][200];
    //static RECT temprect = { 0,0,100,100 };
    //static RECT temprect2 = { 30,30,100,100 };
    //static RECT temprect3 = { 60,60,200,200 };
    switch (iMsg)
    {
    case WM_CREATE:
        break;
    case WM_CHAR:
        hdc = GetDC(hwnd);
        /*
        _stprintf_s(tempstr, _T("%ld"), size.cx);
        DrawText(hdc, tempstr,_tcslen(tempstr),&temprect,DT_CENTER);
        _stprintf_s(tempstr, _T("%d"), FONT.iPointSize);
        DrawText(hdc, tempstr, _tcslen(tempstr), &temprect2, DT_CENTER);
        */
        if (wParam == VK_RETURN)
        {
            //str[line][count] = '\n';
            line++;
            count = 0;
            rect.top += 20;
        }
        else str[line][count++= wParam;
 
        GetTextExtentPoint(hdc, str[line], _tcslen(str[line]), &size);
        size.cx *= FONT.iPointSize / 100.0f;
        if (size.cx >= rect.right - rect.left - FONT.iPointSize/10.0f)
        {
            //str[line][count] = '\n';
            line++;
            count = 0;
            rect.top += 20;
        }
        SetTextColor(hdc, fColor);
        SelectObject(hdc, hFont);
        DrawText(hdc, str[line], _tcslen(str[line]), &rect, DT_LEFT | DT_TOP);
 
        //SetCaretPos(rect.left+size.cx, rect.top+line*20);
        ReleaseDC(hwnd, hdc);
        break;
    case  WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case ID_PENCOLOR:
            for (int i = 0; i < 16; i++)
                temp[i] = RGB(rand() % 256, rand() % 256, rand() % 256);
            memset(&COLOR, 0sizeof(CHOOSECOLOR));
            COLOR.lStructSize = sizeof(CHOOSECOLOR);
            COLOR.hwndOwner = hwnd;
            COLOR.lpCustColors = temp;
            COLOR.Flags = CC_FULLOPEN;
            if (ChooseColor(&COLOR) != 0)
            {
                PenColor = COLOR.rgbResult;
            }
            break;
        case ID_FACECOLOR:
            for (int i = 0; i < 16; i++)
                temp[i] = RGB(rand() % 256, rand() % 256, rand() % 256);
            memset(&COLOR, 0sizeof(CHOOSECOLOR));
            COLOR.lStructSize = sizeof(CHOOSECOLOR);
            COLOR.hwndOwner = hwnd;
            COLOR.lpCustColors = temp;
            COLOR.Flags = CC_FULLOPEN;
            if (ChooseColor(&COLOR) != 0)
            {
                FaceColor = COLOR.rgbResult;
            }
            break;
        case ID_FONT:
            memset(&FONT, 0sizeof(CHOOSEFONT));
            FONT.iPointSize = 100;
            FONT.lStructSize = sizeof(CHOOSEFONT);
            FONT.hwndOwner = hwnd;
            FONT.lpLogFont = &LogFont;
            FONT.Flags = CF_EFFECTS | CF_SCREENFONTS;
            if (ChooseFont(&FONT) != 0)
            {
                fColor = FONT.rgbColors;
                hFont=CreateFont(LogFont.lfHeight, LogFont.lfWidth, LogFont.lfEscapement, 
                    LogFont.lfOrientation, LogFont.lfWeight, LogFont.lfItalic, 
                    LogFont.lfUnderline, LogFont.lfStrikeOut, LogFont.lfCharSet,
                    LogFont.lfOutPrecision, LogFont.lfClipPrecision, LogFont.lfQuality, 
                    LogFont.lfPitchAndFamily, LogFont.lfFaceName);
            }
            break;
        case ID_LINE:
            Figure = 1;
            break;
        case ID_ELLIPSE:
            Figure = 2;
            break;
        case ID_RECTANGLE:
            Figure = 3;
            break;
        case ID_WRITE:
            Figure = 4;
            break;
        }
        break;
    case WM_LBUTTONDOWN:
        //hdc = GetDC(hwnd);
        isClick = true;
        mX1 = LOWORD(lParam);
        mY1 = HIWORD(lParam);
        //ReleaseDC(hwnd, hdc);
        break;
    case WM_LBUTTONUP:
        hdc = GetDC(hwnd);
        isClick = false;
        hPen = CreatePen(PS_SOLID, 1, PenColor);
        SelectObject(hdc, hPen);
        hBrush = CreateSolidBrush(FaceColor);
        SelectObject(hdc, hBrush);
        mX2 = LOWORD(lParam);
        mY2 = HIWORD(lParam);
        switch (Figure)
        {
        case 1:
            MoveToEx(hdc, mX1, mY1, NULL);
            LineTo(hdc, mX2, mY2);
            break;
        case 2:
            Ellipse(hdc, mX1, mY1, mX2, mY2);
            break;
        case 3:
            Rectangle(hdc, mX1, mY1, mX2, mY2);
            break;
        case 4:
 
            orgPen = CreatePen(PS_SOLID, 1, RGB(000));
            SelectObject(hdc, orgPen);
            orgBrush = CreateSolidBrush(FaceColor);
            SelectObject(hdc, orgBrush);
            Rectangle(hdc, mX1, mY1, mX2, mY2);
            rect = RECT{ mX1, mY1, mX2, mY2 };
            for (int i = 0; i < 20; i++)
                for (int j = 0; j < 200; j++)
                    str[i][j] = '\0';
            line = 0;
            count = 0;
            break;
        }
        ReleaseDC(hwnd, hdc);
        break;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
 
        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        HideCaret(hwnd);
        DestroyCaret();
        PostQuitMessage(0);
        break;
 
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
cs