📄 notebookdemo.c
字号:
{ 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_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);}static 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 = 0; pCreateInfo->ty = 0; pCreateInfo->rx = 320; pCreateInfo->by = 240; 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); NewNoteBook(NULL); return 0;}#include <minigui/dti.c>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -