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

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

WIN32 API

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

오다기리 박 2017. 11. 3. 21:59
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
#include <Windows.h>
#include<tchar.h>
#include<string.h>
#include<math.h>
#include"resource.h"
//#pragma comment(linker,"/entry:WinMainCRTStartup /subsystem:console")
LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK ChildWndProc(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;
    WNDCLASS WndClass;
 
    hInst = hInstance;
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    WndClass.lpfnWndProc = FrameWndProc;
    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 = MAKEINTRESOURCE(IDR_MENU1);
    WndClass.lpszClassName = _T("Window Class Name");
    RegisterClass(&WndClass);
 
    WndClass.lpfnWndProc = ChildWndProc;
    WndClass.lpszMenuName = NULL;
    WndClass.lpszClassName = _T("Child Window Class Name");
    RegisterClass(&WndClass);
 
    hwnd = CreateWindow(
        _T("Window Class Name"),
        _T("Main Window Title"),
        WS_OVERLAPPEDWINDOW,
        50,
        50,
        1000,
        700,
        NULL,
        NULL,
        hInstance,
        NULL);
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    while (GetMessage(&msg, NULL00))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
 
HWND ActivatedHwndChild;
 
int selection = -1;
int childcount = 0;
static struct CHILD
{
    HWND hwndChild;
    TCHAR str[10][100= { '\0', };
    int count = 0, line = 0;
};
CHILD child[10];
HWND hwndChild[10= { 0, };
TCHAR str[10][100= { '\0', };
int count = 0, line =0;
LRESULT CALLBACK FrameWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    static HWND hwndClient;
    
 
    CLIENTCREATESTRUCT clientcreate;
    MDICREATESTRUCT mdicreate;
 
    switch (iMsg)
    {
    case WM_CREATE:
        clientcreate.hWindowMenu = GetSubMenu(GetMenu(hwnd), 0);
        clientcreate.idFirstChild = 100;
        hwndClient = CreateWindow(_T("MDICLIENT"), NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
            0000, hwnd, NULL, hInst, (LPSTR)&clientcreate);
        ShowWindow(hwndClient, SW_SHOW);
        break;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case ID_FILENEW:
            mdicreate.szClass = _T("Child Window Class Name");
            mdicreate.szTitle = _T("NoName");
            mdicreate.hOwner = hInst;
            mdicreate.style = 0;
            mdicreate.lParam = 0;
            mdicreate.x = CW_USEDEFAULT;
            mdicreate.y = CW_USEDEFAULT;
            mdicreate.cx = CW_USEDEFAULT;
            mdicreate.cy = CW_USEDEFAULT;
            child[childcount++].hwndChild = (HWND)SendMessage(hwndClient, WM_MDICREATE, 0, (LPARAM)(LPMDICREATESTRUCT)&mdicreate);
 
            break;
        
        }
    case WM_MOUSEACTIVATE:
        ActivatedHwndChild = (HWND)SendMessage(hwndClient, WM_MDIGETACTIVE, 00);
        for (int i = 0; i < childcount; i++)
        {
            if (ActivatedHwndChild == child[i].hwndChild)
            {
                selection = i;
                break;
            }
        }
        break;
        
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
 
    }
    return DefFrameProc(hwnd, hwndClient, iMsg, wParam, lParam);
}
 
LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    TCHAR tmp[20];
    switch (iMsg)
    {
    case WM_CREATE:
        _stprintf_s(tmp, 20, _T("%d"),wParam);
        MessageBox(hwnd, tmp, _T(""), MB_OK);
        break;
    case WM_CHAR:
        if (wParam == VK_RETURN)
        {
            child[selection].line++;
            child[selection].count = 0;
        }
        else if (wParam == VK_BACK)
        {
 
        }
        else
        {
            child[selection].str[child[selection].line][child[selection].count++= wParam;
 
        }
        InvalidateRgn(ActivatedHwndChild, NULL, TRUE);
        break;
    case WM_PAINT:
        hdc = BeginPaint(ActivatedHwndChild, &ps);
        for (int i = 0; i <= child[selection].line; i++)
            TextOut(hdc, 0, i * 20, child[selection].str[i], _tcslen(child[selection].str[i]));
        EndPaint(ActivatedHwndChild, &ps);
        break;
    case WM_DESTROY:
        break;
    }
    return DefMDIChildProc(hwnd, iMsg, wParam, lParam);
}
cs