1 //++++++++++++++++++++++++++
2 // EditorTxt.cpp
3 //++++++++++++++++++++++++++
4
5 #include "EditorTxt.h"//Win32 mode , auto include xxx.h
6 ///#include header
7 #include <windows.h>
8
9 ///declare vairable
10 //const char g_szClassName[] = "TextEditorClass";
11 static char g_szClassName[] = "TextEditorClass";
12 static HINSTANCE g_hInst = NULL;
13
14 ///define ID/IDC
15 #define IDC_MAIN_TEXT 1001
16
17 ///declare CALLBACK
18 LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
19 BOOL LoadFile(HWND hEdit, LPSTR pszFileName);
20 BOOL SaveFile(HWND hEdit, LPSTR pszFileName);
21 BOOL DoFileOpenSave(HWND hwnd, BOOL bSave);
22
23 ///WinMain
24 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
25 LPSTR lpCmdLine, int nCmdShow)
26 {//Handle Instance, LPSTR == char*,
27
28 ///declare struct
29 WNDCLASSEX WndClass; // every window need declare WNDCLASSEX and fill 12 para
30 HWND hwnd; // window handling parameter
31 MSG Msg;
32
33 ///setting
34 g_hInst = hInstance;
35
36 ///Prepare Register WndProc
37 WndClass.cbSize = sizeof(WNDCLASSEX);
38 WndClass.style = 0;
39 WndClass.lpfnWndProc = WndProc;
40 WndClass.cbClsExtra = 0;
41 WndClass.cbWndExtra = 0;
42 WndClass.hInstance = g_hInst;
43 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
44 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
45 WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
46 WndClass.lpszMenuName = "MAINMENU";
47 WndClass.lpszClassName = g_szClassName;
48 WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
49
50 ///register ?
51 if(!RegisterClassEx(&WndClass))
52 {
53 MessageBox(0, "Window Registration Failed!", "Error!",
54 MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
55 return 0;
56 }
57
58 ///Create WindowEx
59 hwnd = CreateWindowEx
60 (
61 WS_EX_CLIENTEDGE, // DWORD dwExStyle,
62 g_szClassName, // LPCSTR lpClassName,
63 "Text Editor Sample Program", // LPCSTR lpWindowName,
64 WS_OVERLAPPEDWINDOW, // DWORD dwStyle,
65 CW_USEDEFAULT, // int X,
66 CW_USEDEFAULT, // int Y,
67 640, // int nWidth,
68 480, // int nHeight,
69 NULL, // HWND hWndParent,
70 NULL, // HMENU hMenu,
71 g_hInst, // HINSTANCE hInstance,
72 NULL // LPVOID lpParam
73 );
74
75 ///create ?
76 if(hwnd == NULL)
77 {
78 MessageBox(0, "Window Creation Failed!", "Error!",
79 MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
80 return 0;
81 }
82
83 ///Show Window
84 ShowWindow(hwnd, nCmdShow); // show the window on screen ,if no, on processing but no screen
85
86 ///Update Window
87 UpdateWindow(hwnd);
88
89 ///Polling GetMsg
90 while(GetMessage(&Msg, NULL, 0, 0) > 0) // =0 mean WM_QUIT, =-1 abnormal
91 {
92 ///Msg for WndProc
93 TranslateMessage(&Msg);// if no, will diable queue from keyboard
94 DispatchMessage(&Msg);// if no , will disable all mouse,key active, Call WndProc
95
96 ///+
97 }//end while
98
99 ///program quit
100 MessageBox(0, "PGM is closed ! ", "Ending",MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
101 return Msg.wParam;
102
103 ///main_end
104 }//end main
105
106 ///WndProc
107 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) //Dispatchmsg(&Msg); will call it indirectly
108 { //main frame open, be called while mouse move within the frame, count also within child frame
109
110 ///switch msg
111 switch(msg)
112 {
113 ///WM_ CREATE
114 case WM_CREATE:
115 CreateWindow
116 (
117 "EDIT", // lpClassName,
118 "start here !", // lpWindowName, ccc is initial data on the client area
119 WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN,
120 CW_USEDEFAULT, // x,
121 CW_USEDEFAULT, // y,
122 CW_USEDEFAULT, // nWidth,
123 CW_USEDEFAULT, // nHeight,
124 hwnd, // hWndParent,
125 (HMENU)IDC_MAIN_TEXT, // hMenu,
126 g_hInst, // hInstance,
127 NULL // lpParam
128 );
129 HFONT hfont;
130 LOGFONT logFont;
131 memset(&logFont, 0, sizeof(logFont));
132 logFont.lfHeight = 24; // see PS
133 logFont.lfWeight = FW_BOLD;
134 strcpy(logFont.lfFaceName, "Courier New");
135 hfont=CreateFontIndirect(&logFont);
136 SendDlgItemMessage
137 (
138 hwnd,
139 IDC_MAIN_TEXT,
140 WM_SETFONT,
141 (WPARAM)hfont,
142 MAKELPARAM(TRUE, 0)
143 );
144 break;
145
146 ///WM_ SIZE
147 case WM_SIZE:
148 if(wParam != SIZE_MINIMIZED) MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
149 break;
150
151 ///WM_SET FOCUS
152 case WM_SETFOCUS:
153 SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
154 break;
155
156 ///WM_ Command
157 case WM_COMMAND:
158 {
159 ///switch LOWORD (wParam)
160 switch(LOWORD(wParam)) //a window macros to single out low 16 bits of 32 bits
161 { //wParam was sent by Dispatchmsg()
162
163 ///CM_ FILE_ OPEN
164 case CM_FILE_OPEN:
165 DoFileOpenSave(hwnd, FALSE);
166 break;
167
168 ///CM_ FILE_ SAVEAS
169 case CM_FILE_SAVEAS:
170 DoFileOpenSave(hwnd, TRUE);
171 break;
172
173 ///CM_ FILE_ EXIT
174 case CM_FILE_EXIT:
175 PostMessage(hwnd, WM_CLOSE, 0, 0);
176 break;
177
178 ///CM_ About
179 case CM_HELP_ABOUT:
180 MessageBox (NULL, "Text Editor for Windows !\n Using the Win32 API" , "About...", 0);
181 break;
182
183 ///default
184 default:
185 break;
186
187 ///+
188 }//end Switch
189
190 ///+
191 }//end Case
192 break;
193
194 ///WM_ CLOSE
195 case WM_CLOSE:
196 DestroyWindow(hwnd);
197 break;
198
199 ///WM_ DESTROY
200 case WM_DESTROY:
201 PostQuitMessage(0);
202 break;
203
204 ///default
205 default: //every key , mouse will enact this ,if not case,then default
206 return DefWindowProc(hwnd, msg, wParam, lParam);
207
208 ///+
209 }//end Switch
210
211 ///+
212 return 0;
213 } // Call back end
214
215 ///DoFlOpSv
216 BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
217 {
218 ///declare vairable
219 OPENFILENAME ofn;
220 char szFileName[MAX_PATH];
221
222 ///Zero Memory
223 ZeroMemory(&ofn, sizeof(ofn));
224
225 ///Setting ofn
226 szFileName[0] = 0;
227
228 ofn.lStructSize = sizeof(ofn);
229 ofn.hwndOwner = hwnd;
230 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
231 ofn.lpstrFile = szFileName;
232 ofn.nMaxFile = MAX_PATH;
233 ofn.lpstrDefExt = "txt";
234
235 ///bSave =TRUE
236 if(bSave)
237 {
238 ///ofn.Flag
239 ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
240 OFN_OVERWRITEPROMPT;
241
242 ///GetSave FileName
243 if(GetSaveFileName(&ofn))
244 {
245 ///SaveFile
246 if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
247 {
248 MessageBox(hwnd, "Save file failed.", "Error", MB_OK | MB_ICONEXCLAMATION);
249 return FALSE;
250 }
251
252 ///+
253 }//end if
254
255 ///+
256 }//end if
257
258 ///else= FALSE
259 else
260 {
261 ///ofn.Flag
262 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
263
264 ///GetOpen FileName
265 if(GetOpenFileName(&ofn))
266 {
267 ///LoadFile
268 if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
269 {
270 MessageBox(hwnd, "Load of file failed.", "Error",MB_OK | MB_ICONEXCLAMATION);
271 return FALSE;
272 }
273
274 ///+
275 }//end if
276
277 ///+
278 }//end else
279
280 ///+
281 return TRUE;
282 }
283
284 ///LoadFile
285 BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
286 {
287 ///declare vairable
288 HANDLE hFile;
289 BOOL bSuccess = FALSE;
290
291 ///CreateFile hFile
292 hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,OPEN_EXISTING, 0, 0);
293
294 ///hFile != INVALID
295 if(hFile != INVALID_HANDLE_VALUE)
296 {
297 ///GetFileSize
298 DWORD dwFileSize;
299 dwFileSize = GetFileSize(hFile, NULL);
300
301 ///FileSize != 0xFFFFFFFF
302 if(dwFileSize != 0xFFFFFFFF)
303 {
304 ///GlobalAlloc
305 LPSTR pszFileText;
306 pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
307
308 ///TxtFile !=NULL
309 if(pszFileText != NULL)
310 {
311 ///dwREAD
312 DWORD dwRead;
313
314 ///ReadFile
315 if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
316 {
317 ///SetText Window
318 pszFileText[dwFileSize] = 0; // eof terminator
319 if(SetWindowText(hEdit, pszFileText)) bSuccess = TRUE; // post string to client
320
321 ///+
322 }//end ReadFile
323
324 ///+
325 GlobalFree(pszFileText);
326 }//end if NULL
327
328 ///+
329 }//end if
330
331 ///CloseHandle hFile
332 CloseHandle(hFile);
333
334 ///+
335 }//end if INVALID
336
337 ///+
338 return bSuccess;
339 }
340
341 ///SaveFile
342 BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
343 {
344 ///declare vairable
345 HANDLE hFile;
346 BOOL bSuccess = FALSE;
347
348 ///CreateFile hFile
349 hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
350
351 ///hFile != INVALID
352 if(hFile != INVALID_HANDLE_VALUE)
353 {
354 ///GetWindow TextLength
355 DWORD dwTextLength;
356 dwTextLength = GetWindowTextLength(hEdit);
357
358 ///TextLength >zero?
359 if(dwTextLength > 0)// avoid blank text file
360 {
361 ///GlobalAlloc
362 LPSTR pszText;
363 pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
364
365 ///pszText !=NULL
366 if(pszText != NULL)
367 {
368 ///GetWndTxt =Not(0)
369 if(GetWindowText(hEdit, pszText, dwTextLength + 1))
370 {
371 ///WriteFile
372 DWORD dwWritten;
373 if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) bSuccess = TRUE;
374
375 ///+
376 }//end GetWindowText
377
378 ///+
379 GlobalFree(pszText);
380 }//end if NULL
381
382 ///+
383 }//end if
384
385 ///CloseHandle hFile
386 CloseHandle(hFile);
387
388 ///+
389 }//end if INVALID
390
391 ///+
392 return bSuccess;
393 }
394
This Blog is dedicated to Sir Johnson Lin
Windows c++ program : simple text editor. 視窗版 c 語言程式 : 簡易文字編輯器
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言