📄 notebook.c
字号:
mii.id = 120;
mii.typedata = (DWORD)"格式";
mii.hsubmenu = createpmenuview ();
InsertMenuItem(hmnu, 2, TRUE, &mii);
mii.type = MFT_STRING;
mii.id = 130;
mii.typedata = (DWORD)"帮助";
mii.hsubmenu = createpmenuabout ();
InsertMenuItem(hmnu, 4, TRUE, &mii);
return hmnu;
}
/*另存为*/
BOOL 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);
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;
}
choise = OpenFileDialog (hParent, FALSE, &myWinFileData);
if(choise == IDOK)
{
HCURSOR old_cursor;
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_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:
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 0
static 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);
}
#endif
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 = 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_lightwhite;
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 = 0; //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_VERSION
void* NewNoteBook (PNOTEINFO pNoteInfo)
{
pthread_t th;
CreateThreadForMainWindow (&th, NULL, NoteBook, pNoteInfo);
return NULL;
}
int CreatNoteBook (HWND hWnd)
{
NoteBook(NULL);
return 0;
}
//#include <minigui/dti.c>
#else
int CreatNoteBook (HWND hWnd)
{
#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 + 320,max_rect.top + 240) == 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 + -