접기
HWND hEdit;
void Open()
{
OPENFILENAME OFN;
TCHAR lpstrFile[MAX_PATH]="";
HANDLE hFile;
DWORD dwRead;
DWORD size;
TCHAR *buf;
if (ConfirmSave() == IDCANCEL) {
return;
}
memset(&OFN, 0, sizeof(OPENFILENAME));
OFN.lStructSize = sizeof(OPENFILENAME);
OFN.hwndOwner=hWndMain;
OFN.lpstrFilter="Text File\0*.txt;*.doc\0Every File(*.*)\0*.*\0";
OFN.lpstrFile=lpstrFile;
OFN.nMaxFile=MAX_PATH;
OFN.lpstrDefExt="txt";
if (GetOpenFileName(&OFN)==FALSE) {
return;
}
hFile=CreateFile(OFN.lpstrFile,GENERIC_READ,0,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile==INVALID_HANDLE_VALUE) {
MessageBox(hWndMain,"파일을 열 수 없습니다.","에러",MB_OK);
return;
}
size=GetFileSize(hFile,NULL);
buf=(TCHAR *)calloc(size+1,1);
ReadFile(hFile,buf,size,&dwRead,NULL);
CloseHandle(hFile);
SetWindowText (hEdit,buf);
free(buf);
SendMessage(hEdit,EM_SETMODIFY,(WPARAM)FALSE,0);
ChangeCaption(OFN.lpstrFile);
}
int ConfirmSave()
{
int result=IDNO;
TCHAR Mes[MAX_PATH+64];
if (SendMessage(hEdit,EM_GETMODIFY,0,0)==TRUE) {
wsprintf(Mes,"%s 파일이 변경되었습니다. 저장하시겠습니까?",NowFile);
result=MessageBox(hWndMain,Mes,"알림",MB_YESNOCANCEL);
if (result == IDCANCEL) {
return IDCANCEL;
}
if (result == IDYES) {
if (Save() == FALSE)
return IDCANCEL;
else
return IDYES;
}
}
return result;
}
BOOL Save()
{
if (lstrcmp(NowFile,"제목없음.txt")==0) {
return SaveAs();
} else {
return SaveToFile(NowFile);
}
}
BOOL SaveAs()
{
OPENFILENAME OFN;
TCHAR lpstrFile[MAX_PATH]="";
memset(&OFN, 0, sizeof(OPENFILENAME));
OFN.lStructSize = sizeof(OPENFILENAME);
OFN.hwndOwner=hWndMain;
OFN.lpstrFilter="Text File\0*.txt;*.doc\0Every File(*.*)\0*.*\0";
OFN.lpstrFile=lpstrFile;
OFN.nMaxFile=MAX_PATH;
OFN.lpstrDefExt="txt";
if (GetSaveFileName(&OFN)==FALSE) {
return FALSE;
}
ChangeCaption(OFN.lpstrFile);
return SaveToFile(NowFile);
}
BOOL SaveToFile(TCHAR *Path)
{
HANDLE hFile;
DWORD dwWritten;
int length;
TCHAR *buf;
hFile=CreateFile(Path,GENERIC_WRITE,0,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile==INVALID_HANDLE_VALUE) {
return FALSE;
}
length=GetWindowTextLength(hEdit);
buf=(TCHAR *)malloc(length+1);
GetWindowText (hEdit,buf,length+1);
WriteFile(hFile,buf,length,&dwWritten,NULL);
CloseHandle(hFile);
free(buf);
SendMessage(hEdit,EM_SETMODIFY,(WPARAM)FALSE,0);
return TRUE;
}
#define MAXBUFFER 1048576
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
static HBRUSH hBrushEdit;
static HFONT hFontEdit;
switch (iMessage) {
case WM_CREATE:
hEdit = CreateWindow (TEXT("edit "), NULL, WS_CHILD|WS_VISIBLE|WS_BORDER|
ES_AUTOHSCROLL , 10, 10, 200, 25, hWnd, (HMENU)ID_EDIT, g_hInst, NULL);
SendMessage(hEdit, EM_LIMITTEXT , (WPARAM)MAXBUFFER,0);
hBrushEdit=CreateSolidBrush(RGB(255,0,0));
// 궁서체의 30픽셀짜리 폰트
hFontEdit=CreateFont(30,0,0,0,0,0,0,0,HANGEUL_CHARSET,
3,2,1,VARIABLE_PITCH | FF_ROMAN,TEXT("궁서"));
SendMessage(hEdit, WM_SETFONT , (WPARAM)hFontEdit, MAKELPARAM(FALSE,0));
return 0;
case WM_DESTROY:
DeleteObject(hBrushEdit);
DeleteObject(hFontEdit);
PostQuitMessage(0);
return 0;
case WM_QUERYENDSESSION:
case WM_CLOSE:
if (ConfirmSave() == IDCANCEL) {
return TRUE;
} else {
break;
}
case WM_INITMENU:
// 취소가 가능한가에 따라 Undo 메뉴 항목을 사용금지/허가한다.
if (SendMessage(hEdit, EM_CANUNDO,0,0)==TRUE)
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_UNDO,MF_ENABLED | MF_BYCOMMAND);
else
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_UNDO,MF_GRAYED | MF_BYCOMMAND);
// 선택 영역이 없으면 Cut, Copy, Clear 메뉴 항목은 사용할 수 없다.
SendMessage(hEdit,EM_GETSEL, (WPARAM)&Start, (LPARAM)&End);
if (Start != End) {
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_CUT,MF_ENABLED | MF_BYCOMMAND);
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_COPY,MF_ENABLED | MF_BYCOMMAND);
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_DEL,MF_ENABLED | MF_BYCOMMAND);
}
else {
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_CUT,MF_GRAYED | MF_BYCOMMAND);
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_COPY,MF_GRAYED | MF_BYCOMMAND);
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_DEL,MF_GRAYED | MF_BYCOMMAND);
}
// 클립보드에 텍스트 자료가 없으면 Paste 항목은 사용할 수 없다.
if (IsClipboardFormatAvailable(CF_TEXT))
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_PASTE,MF_ENABLED | MF_BYCOMMAND);
else
EnableMenuItem(GetMenu(hWnd),IDM_EDIT_PASTE,MF_GRAYED | MF_BYCOMMAND);
return 0;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDM_FILE_EXIT:
SendMessage(hWnd,WM_CLOSE,0,0);
break;
case IDM_FILE_NEW:
if (ConfirmSave() == IDCANCEL) {
break;
}
SetWindowText (hEdit,"");
ChangeCaption("제목없음");
break;
case IDM_FILE_LOAD:
Open();
break;
case IDM_FILE_SAVE:
Save();
break;
case IDM_FILE_SAVEAS:
SaveAs();
break;
case IDM_EDIT_UNDO:
SendMessage(hEdit, EM_UNDO ,0,0);
return 0;
case IDM_EDIT_CUT:
SendMessage(hEdit, WM_CUT ,0,0);
break;
case IDM_EDIT_COPY:
SendMessage(hEdit, WM_COPY ,0,0);
break;
case IDM_EDIT_PASTE:
SendMessage(hEdit, WM_PASTE ,0,0);
break;
case IDM_EDIT_DEL:
SendMessage(hEdit, WM_CLEAR ,0,0);
break;
case IDM_EDIT_SELALL:
SendMessage(hEdit, EM_SETSEL ,0,-1);
break;
case IDM_HELP_ABOUT:
MessageBox(hWnd,"간단한 메모장입니다","소개",MB_OK);
break;
case ID_EDIT:
switch (HIWORD(wParam)) {
case EN_MAXTEXT :
MessageBox(hWnd, "입력 문자수 초과","에러",MB_OK);
break;
case EN_CHANGE :
...
}
}
return 0;
case WM_SIZE:
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam),TRUE);
return 0;
case WM_SETFOCUS:
SetFocus(hEdit);
return 0;
case WM_CTLCOLOREDIT :
if ((HWND)lParam == hEdit) {
SetTextColor((HDC)wParam, RGB(255,255,0));
SetBkColor((HDC)wParam, RGB(255,0,0));
return (LRESULT)hBrushEdit;
}
break;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
접기