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

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

WIN32 API

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

오다기리 박 2017. 11. 3. 20:26
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
#include <Windows.h>
#include<tchar.h>
#include<stdio.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//윈도우 위치 x좌표
        100,//윈도우 위치 y좌표
        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;
    OPENFILENAME OFN;
    OPENFILENAME SFN;
    int answer;
    TCHAR filter[] = _T("C++소스와 헤더 파일\0*.cpp;*.h\0Every File(*.*)\0*.*\0Text File\0*.txt;*.doc\0");
    TCHAR lpstrFile[100= _T("");
    static TCHAR str[100];
    static int line = 0;
    FILE *fPtr;
    TCHAR buffer[500];
 
    switch (iMsg)
    {
    case WM_CREATE:
        break;
    case WM_LBUTTONDOWN:
        break;
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case ID_FILENEW:        //새글
            answer = MessageBox(hwnd, _T("새 글을 여시겠습니까?"), _T("새글 선택"), MB_OKCANCEL);
            if (answer == IDOK)
            {
 
            }
            break;
        case ID_FILEOPEN:        //열기                            
 
            memset(&OFN, 0sizeof(OPENFILENAME));
            OFN.lStructSize = sizeof(OPENFILENAME);
            OFN.hwndOwner = hwnd;
            OFN.lpstrFilter = filter;
            OFN.lpstrFile = lpstrFile;
            OFN.nMaxFile = 256;
            OFN.lpstrInitialDir = _T(".");
            if (GetOpenFileName(&OFN) != 0)
            {
                _stprintf_s(str, _T("%s 파일을 여시겠습니까?"), OFN.lpstrFile);
                answer = MessageBox(hwnd, str, _T("열기선택"), MB_OKCANCEL);
                if (answer == IDOK)
                {
                    hdc = GetDC(hwnd);
#ifdef _UNICODE
                    _tfopen_s(&fPtr, OFN.lpstrFile, _T("r,ccs=UNICODE"));        //파일저장하기->파일 제대로 열리면 0반환
                                                                            //filename : 열고자하는 파일이름
#else                                                        //fPtr : 파일주소 저장
                    _tfopen_s(&file, filename, _T("r"));
#endif
                    while (_fgetts(buffer, 100, fPtr) != NULL)                //fPtr파일에서 한 줄을 읽어와 buffer에 저장, 100은 buffer공간크기
                    {                                                        //파일의 끝을 만날 때까지 줄단위로 buffer에 읽어들임
                        if (buffer[_tcsclen(buffer) - 1== _T('\n'))        //buffer의 마지막 문자가 개행문자이면 
                            buffer[_tcsclen(buffer) - 1= NULL;            //NULL로 변경후
                        TextOut(hdc, 0, line * 20, buffer, _tcslen(buffer));//윈도우에 출력
                        line++;
                    }
                    fclose(fPtr);
                    ReleaseDC(hwnd, hdc);
                }
            }
 
            break;
 
        case ID_FILESAVE:        //저장
            hdc = GetDC(hwnd);
            memset(&SFN, 0sizeof(OPENFILENAME));
            SFN.lStructSize = sizeof(OPENFILENAME);
            SFN.hwndOwner = hwnd;
            SFN.lpstrFilter = filter;
            SFN.lpstrFile = lpstrFile; //파일이름
            SFN.nMaxFile = 256;
            SFN.lpstrInitialDir = _T(".");
            if (GetSaveFileName(&SFN) != 0)
            {
                _stprintf_s(str, _T("%s 파일을 저장하시겠습니까?"), SFN.lpstrFile);
                answer = MessageBox(hwnd, str, _T("저장하기 선택"), MB_OKCANCEL);
                if (answer == IDOK)
                {
                    _tfopen_s(&fPtr, SFN.lpstrFile, _T("wt"));
                    if (fPtr == NULL)
                    {
                        MessageBox(hwnd, _T("파일저장에러"), _T("에러"), MB_OK);
                        break;
                    }
                    for (int i = 0; i < 256; i++)
                    {
                        _fputts(&buffer[i], fPtr);
                        if (feof(fPtr) != 0)
                            break;
                    }
                    fclose(fPtr);
                }
            }
            ReleaseDC(hwnd, hdc);
            break;
        case ID_EDITUNDO:        //편집취소
            MessageBox(hwnd, _T("편집을 취소 하시겠습니까?"), _T("편집취소 선택"), MB_OKCANCEL);
            break;
        case ID_EXIT:            //끝내기
            answer = MessageBox(hwnd, _T("끝내시겠습니까?"), _T("끝내기 선택"), MB_OKCANCEL);
            if (answer == IDOK)
                PostQuitMessage(0);
            break;
        }
        break;
 
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        //TextOut(hdc, 0, 0, _T("helloHelloWorld"), _tcslen(_T("helloHelloWorld")));
        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
 
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
cs