📄 main.c
字号:
"新记事本大小", "请指定新记事本的窗口大小", 240, 150, FALSE, entries, buttons); col = atoi (newcols); row = atoi (newrows); free (newcols); free (newrows); if (result != IDOK) return; if (col < MIN_COLS || col > MAX_COLS || row < MIN_ROWS || row > MAX_ROWS) { MessageBox (hWnd, "请指定有效的记事本大小值.", "记事本", MB_OK | MB_ICONINFORMATION); return; } break; } default: return; } pNoteInfo->winType = cmd_id;}static BOOL SetCharset (HWND hWnd, PNOTEINFO pNoteInfo, WPARAM cmd_id){ LOGFONT log_font; PLOGFONT sys_font; char* charset; if (pNoteInfo->editCharset == cmd_id) return FALSE; switch (cmd_id) { case IDM_ANSI: charset = "ISO8859-1"; break; case IDM_GB2312: charset = "GB2312-80"; break; case IDM_BIG5: charset = "BIG5"; break; case IDM_DEFAULT: charset = NULL; break; default: return FALSE; } if (cmd_id == IDM_DEFAULT) pNoteInfo->log_font = NULL; else { sys_font = GetSystemFont (SYSLOGFONT_WCHAR_DEF); memcpy (&log_font, sys_font, sizeof (LOGFONT)); if (pNoteInfo->log_font) DestroyLogFont (pNoteInfo->log_font); strcpy (log_font.charset, charset); pNoteInfo->log_font = CreateLogFontIndirect (&log_font); } pNoteInfo->editCharset = cmd_id; SetWindowFont (pNoteInfo->hMLEditWnd, pNoteInfo->log_font); return TRUE;}//modified by leon to fix the saveas bugBOOL NBSaveAs (PNOTEINFO pNoteInfo, HWND hParent, HWND hMLEditWnd){ FILEDLGDATA myWinFileData; FILE * file; int choise=0; int reallength=0; char buffer[102400]; strcpy (myWinFileData.filepath, pNoteInfo->filePath); myWinFileData.IsSave = TRUE; choise = OpenFileDialog (hParent, TRUE, &myWinFileData); if (choise == IDOK) { strcpy(pNoteInfo->fileFullName,myWinFileData.filefullname); DivideFileFullName(pNoteInfo); umask (S_IXGRP | S_IWOTH); if ((file = fopen (pNoteInfo->fileFullName, "w+")) == NULL) { MessageBox (hParent,"文件打开失败","记事簿", MB_OK | MB_ICONSTOP); return FALSE; } reallength = GetWindowTextLength(hMLEditWnd); GetWindowText(hMLEditWnd,buffer,102400); //fprintf(stderr,"\n in saveas:\nreallength:%d\ntext:\n%s",reallength,buffer); if (fwrite(buffer, 1, reallength, file) < 0) MessageBox (hParent,"文件写入错","记事簿", MB_OK | MB_ICONEXCLAMATION); pNoteInfo->isChanged = FALSE; fclose (file); return TRUE; } return FALSE;}BOOL NBSave (PNOTEINFO pNoteInfo, HWND hParent, HWND hMLEditWnd){ FILE *file; char buffer[102400]; long reallength=0; if (strcmp (pNoteInfo->fileName,"未命名.txt")==0) { return NBSaveAs (pNoteInfo, hParent, hMLEditWnd); } file = fopen(pNoteInfo->fileFullName, "w+"); if (file == NULL) { MessageBox (hParent, "文件打开失败","记事簿", MB_OK | MB_ICONSTOP); return FALSE; } reallength = GetWindowTextLength(hMLEditWnd); GetWindowText(hMLEditWnd,buffer,102399); if (fwrite(buffer, 1, reallength, file) < 0) MessageBox (hParent, "文件写入错","记事簿", MB_OK | MB_ICONEXCLAMATION); fclose (file); pNoteInfo->isChanged = FALSE; return TRUE;}BOOL NBOpen(PNOTEINFO pNoteInfo, HWND hParent, HWND hMLEditWnd){ FILEDLGDATA myWinFileData; int choise=0,fd; long reallength=0; char buffer[102400]; strcpy (myWinFileData.filepath, "."); myWinFileData.IsSave = FALSE; if (pNoteInfo->isChanged) { choise = MessageBox (hParent, "文档正文已改动,是否保存?", "记事簿", MB_YESNOCANCEL | MB_ICONQUESTION); if (choise == IDYES) NBSave(pNoteInfo, hParent, hMLEditWnd); else if (choise == IDCANCEL) return FALSE; pNoteInfo->isChanged = FALSE; }// else // fprintf(stderr,"unchanged"); choise = OpenFileDialog (hParent, FALSE, &myWinFileData); if(choise == IDOK) { HCURSOR old_cursor;// fprintf(stderr,"Open File: %s \n",myWinFileData.filefullname); if ( access (myWinFileData.filefullname, F_OK) < 0) MessageBox (hParent, "文件不存在","记事簿", MB_OK | MB_ICONSTOP); else if ( access (myWinFileData.filefullname, R_OK) < 0) MessageBox (hParent, "文件不可读","记事簿", MB_OK | MB_ICONSTOP); else { if ( access (myWinFileData.filefullname, W_OK) < 0) MessageBox (hParent, "文件不可写","记事簿", MB_OK | MB_ICONEXCLAMATION); fd = open(myWinFileData.filefullname,O_RDONLY); if (fd <= 0) { MessageBox (hParent, "文件打开失败","记事簿", MB_OK | MB_ICONSTOP); return FALSE; } old_cursor = SetDefaultCursor (GetSystemCursor (IDC_WAIT)); if ((reallength=read(fd,buffer,102399)) >= 102399) MessageBox (hParent, "文件被截断","记事簿", MB_OK | MB_ICONEXCLAMATION); close (fd); buffer[reallength]=0; SetWindowText (hMLEditWnd, buffer); SetDefaultCursor (old_cursor); strcpy(pNoteInfo->fileFullName,myWinFileData.filefullname); DivideFileFullName(pNoteInfo); return TRUE; } } return FALSE;}BOOL NBNew(PNOTEINFO pNoteInfo, HWND hParent, HWND hMLEditWnd){ if (pNoteInfo->isChanged) NBSave(pNoteInfo, hParent, hMLEditWnd); SetWindowText(hMLEditWnd,""); strcpy(pNoteInfo->fileFullName,"未命名.txt"); return DivideFileFullName(pNoteInfo);}BOOL NBPrint(HWND hMLEditWnd){ char temp [255]; GetWindowTextLength (hMLEditWnd); GetWindowText (hMLEditWnd, temp, 254); return TRUE;}int NoteBookWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam){ HWND hMLEditWnd; RECT client; char title[NAME_MAX + 10]; PNOTEINFO pNoteInfo; pNoteInfo = (PNOTEINFO) GetWindowAdditionalData(hWnd); hMLEditWnd = pNoteInfo->hMLEditWnd; GetClientRect(hWnd,&client); switch (message) { case MSG_CREATE: pNoteInfo->hMLEditWnd = CreateWindow ("medit", "", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL, IDC_MLEDIT, 0, 0, client.right,client.bottom , hWnd, 0); strcpy(title,pNoteInfo->fileName); strcat(title," - 记事簿"); SetWindowText(hWnd,title); break; case MSG_SHOWWINDOW: if (hMLEditWnd != HWND_INVALID) SetFocus (hMLEditWnd); return 0; case MSG_ACTIVEMENU: if (wParam == 2) { CheckMenuRadioItem ((HMENU)lParam, IDM_40X15, IDM_CUSTOMIZE, pNoteInfo->winType, MF_BYCOMMAND); CheckMenuRadioItem ((HMENU)lParam, IDM_DEFAULT, IDM_BIG5, pNoteInfo->editCharset, MF_BYCOMMAND); } break; case MSG_COMMAND: switch (wParam) { case IDM_NEW: if(NBNew(pNoteInfo, hWnd, hMLEditWnd)) { strcpy(title,pNoteInfo->fileName); strcat(title," - 记事簿"); SetWindowText(hWnd,title); } break; case IDM_OPEN: if (NBOpen(pNoteInfo, hWnd, hMLEditWnd)) { strcpy(title,pNoteInfo->fileName); strcat(title," - 记事簿"); SetWindowText(hWnd,title); }; break; case IDM_SAVE: if(NBSave(pNoteInfo, hWnd, hMLEditWnd)) { strcpy(title,pNoteInfo->fileName); strcat(title," - 记事簿"); SetWindowText(hWnd,title); }; break; case IDM_SAVEAS: if(NBSaveAs(pNoteInfo, hWnd, hMLEditWnd)) { strcpy(title,pNoteInfo->fileName); strcat(title," - 记事簿"); SetWindowText(hWnd,title); }; break; case IDM_PRINT: NBPrint(hMLEditWnd); break; case IDM_EXIT: PostMessage (hWnd, MSG_CLOSE,0,0); break; case IDM_40X15 ... IDM_CUSTOMIZE: SetWindowSize (hWnd, pNoteInfo, wParam); break; case IDM_DEFAULT... IDM_BIG5: SetCharset (hWnd, pNoteInfo, wParam); break; case IDM_ABOUT: AboutLaodan(hWnd); break; case IDM_ABOUT_THIS: AboutNotebook(hWnd); break; }; if ((wParam>>16) == EN_CHANGE) { if (hMLEditWnd ==(HWND)lParam) { pNoteInfo->isChanged = TRUE; } }; return 0; case MSG_CLOSE: if (pNoteInfo->isChanged) { int choise = MessageBox (hWnd, "保存修改内容吗?", "记事簿", MB_YESNOCANCEL | MB_ICONQUESTION); if ( choise == IDYES) NBSave(pNoteInfo, hWnd, hMLEditWnd); else if ( choise == IDCANCEL) break; } DestroyWindow (hMLEditWnd); pNoteInfo->hMLEditWnd = HWND_INVALID; DestroyMainWindow (hWnd); PostQuitMessage (hWnd); return 0; } return DefaultMainWinProc (hWnd, message, wParam, lParam);}#if 0static HICON myLoadIcon (const char* filename){ static char res_dir [MAX_PATH + 1] = {'\0'}; char full_path [MAX_PATH + 1]; if (res_dir [0] == '\0') { if (GetValueFromEtcFile (ETCFILEPATH, "appinfo", "apprespath", res_dir, MAX_PATH) < 0) strcpy (res_dir, "res/"); } strcpy (full_path, res_dir); if (full_path [strlen (full_path) - 1] != '/') strcat (full_path, "/"); strcat (full_path, "notebook/"); strcat (full_path, filename); return LoadIconFromFile (HDC_SCREEN, full_path, 0); }#endifstatic void InitNoteBookInfo (PMAINWINCREATE pCreateInfo, PNOTEINFO pNoteInfo){ pCreateInfo->dwStyle = WS_CAPTION | WS_BORDER | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE | WS_SYSMENU; pCreateInfo->dwExStyle = WS_EX_IMECOMPOSE; pCreateInfo->spCaption = "记事簿"; pCreateInfo->hMenu = createmenu(); pCreateInfo->hCursor = GetSystemCursor(0); pCreateInfo->hIcon = LoadIconFromFile (HDC_SCREEN, "res/notebook.ico", 0); pCreateInfo->MainWindowProc = NoteBookWinProc; pCreateInfo->lx = pNoteInfo->lx; pCreateInfo->ty = pNoteInfo->ty; pCreateInfo->rx = pNoteInfo->lx + ClientWidthToWindowWidth (WS_CAPTION | WS_BORDER, pNoteInfo->cols * GetSysCharWidth ()); pCreateInfo->by = pNoteInfo->ty + ClientHeightToWindowHeight (WS_CAPTION | WS_BORDER, pNoteInfo->rows * GetSysCharHeight (), TRUE); pCreateInfo->iBkColor = COLOR_lightgray; pCreateInfo->dwAddData = (DWORD)pNoteInfo; pCreateInfo->hHosting = HWND_DESKTOP;}void* NoteBook (void* data){ MSG Msg; MAINWINCREATE CreateInfo; HWND hMainWnd; PNOTEINFO pNoteInfo; char currentpath [PATH_MAX + 1]; static int x = 0, y = 0; getcwd(currentpath,PATH_MAX); if (data == NULL) { if(!(pNoteInfo = malloc(sizeof(NOTEINFO)))) return NULL;//error!! pNoteInfo->isChanged = FALSE; strcpy(pNoteInfo->fileName , "未命名.txt"); strcpy(pNoteInfo->filePath , currentpath); pNoteInfo->fileSize = 0; pNoteInfo->Buffer = NULL; pNoteInfo->hMLEditWnd = HWND_INVALID; pNoteInfo->lx = x; pNoteInfo->ty = y; x += 20; y += 20; pNoteInfo->cols = VGASTD_NUMBER_COLS; pNoteInfo->rows = VGASTD_NUMBER_ROWS; pNoteInfo->winType = IDM_80X25; pNoteInfo->editCharset = IDM_DEFAULT; pNoteInfo->log_font = NULL; } else pNoteInfo = (PNOTEINFO) data; if (pNoteInfo->filePath [strlen (pNoteInfo->filePath) - 1] != '/') strcat (pNoteInfo->filePath, "/"); InitNoteBookInfo (&CreateInfo, pNoteInfo); hMainWnd = CreateMainWindow (&CreateInfo); if (hMainWnd == HWND_INVALID) return NULL; while (GetMessage (&Msg, hMainWnd) ) { TranslateMessage (&Msg); DispatchMessage (&Msg); } MainWindowThreadCleanup (hMainWnd); if (pNoteInfo->log_font) DestroyLogFont (pNoteInfo->log_font); free (pNoteInfo); return NULL;}#ifndef _LITE_VERSIONvoid* NewNoteBook (PNOTEINFO pNoteInfo){ pthread_t th; CreateThreadForMainWindow (&th, NULL, NoteBook, pNoteInfo); return NULL;}int MiniGUIMain (int args, const char* arg[]){#ifdef _IME_GB2312 GBIMEWindow (HWND_DESKTOP);#endif NoteBook(NULL); return 0;}#include <minigui/dti.c>#elseint MiniGUIMain (int args, const char* arg[]){#ifndef _STAND_ALONE int i; const char* layer = NULL; RECT max_rect = {0, 0, 0, 0}; for (i = 1; i < args; i++) { if (strcmp (arg[i], "-layer") == 0) { layer = arg[i + 1]; break; } } GetLayerInfo (layer, &max_rect, NULL, NULL, NULL); if (JoinLayer (layer, arg[0], max_rect.left, max_rect.top, max_rect.left + 1024, max_rect.top + 768) == INV_LAYER_HANDLE) { printf ("JoinLayer: invalid layer handle.\n"); exit (1); }#endif NoteBook (NULL); return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -