?? 記事本.cpp
字號:
#include <windows.h>
#include <commdlg.h>
#include <string.h>
#include "resource.h"
HINSTANCE hInst;
HWND hdlg;
HWND hWndEdit;
HWND hWnd;
HANDLE hFile;
OPENFILENAME ofn;
CHOOSECOLOR colorBox;
CHOOSEFONT fontBox;
PAGESETUPDLG pd1;
PRINTDLG page;
FINDREPLACE find;
char szFile[255];
char szFileTitle[255];
long WINAPI WndProc(HWND hwnd,UINT imessage,UINT wParam,LONG lParam);
//void WINAPI CaretPos(int *xcaret,int *ycaret,HWND hWnd,int nCharWidth,int nArrayPos);
BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);
BOOL ShowFileInfo(HWND hWnd,HDC hDC,HDROP hDropInfo);
//BOOL CALLBACK DlgProcGetFileName(HWND,UINT,WPARAM,LPARAM); //獲取文件名
BOOL FileToEditBox(HWND,LPSTR); //文件到編輯框
BOOL EditBoxToFile(HWND,LPCTSTR); //編輯框到文件
BOOL EditBoxToClip1(HWND); //編輯框到剪貼板(復(fù)制)
BOOL EditBoxToClip2(HWND); //編輯框到剪貼板(剪切)
BOOL ClipToEditBox(HWND); //剪貼板到編輯框
bool check(char* , char* ); //判斷兩個(gè)字符串是否相等
//主函數(shù)
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX wndclass;
MSG Message;
HACCEL hAccel;
char lpszTitle[]="0309014215 王鐸 計(jì)算機(jī)2班";
char lpszClassName[]="大作業(yè)";
wndclass.cbSize=sizeof(wndclass);
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,(LPCSTR)IDI_ICON1);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
// wndclass.hCursor=LoadCursor(hInstance,(LPCSTR)IDC_CURSOR1);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpfnWndProc = WndProc;
wndclass.lpszMenuName=(LPCSTR)IDR_MENU1;
wndclass.lpszClassName=lpszClassName;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.hIconSm=LoadIcon(hInstance,(LPCSTR)IDI_ICON1);
if(!RegisterClassEx(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hWnd= CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
hInst=hInstance;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
//發(fā)送新建消息
SendMessage(hWnd,WM_COMMAND,IDM_NEW,0);
hAccel=LoadAccelerators(hInstance,(LPCSTR)IDR_ACCELERATOR1);
while(GetMessage(&Message,0,0,0))
{
if(!TranslateAccelerator(hWnd,hAccel,&Message))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
return Message.wParam;
}
//窗體消息處理函數(shù)
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{
HDC hDC;
RECT ClientRect;
char *p=NULL;
LPCTSTR lpTemp=""; // 空字符串
HMENU hmenu;
static char FindWhat[]="ccms";
static char ReplaceWith[]="ccms";
static bool flag1=false; //若沒有打開文件,flag=flase
static bool flag2=false; //若沒有新建文件,flag2=false
static bool flag3=false; //用來判斷文件名是否帶了后綴,在 IDM_SAVE 中將會(huì)用到
static bool flag4=false; //用來判斷是否保存了文件
switch (iMessage)
{
case WM_CREATE:
GetClientRect(hWnd,&ClientRect); //獲取客戶區(qū)大小
hWndEdit=CreateWindow("EDIT", //創(chuàng)建編輯框
NULL,
WS_CHILD|WS_VISIBLE|WS_VSCROLL|ES_MULTILINE|WS_BORDER,
ClientRect.left,
ClientRect.top,
ClientRect.right,
ClientRect.bottom,
hWnd,
//(HMENU)ID_EDIT,
NULL,
hInst,
NULL);
EnableWindow(hWndEdit,0); //使編輯框不能接受輸入
DragAcceptFiles(hWnd,TRUE); //API函數(shù):允許hWnd所指的窗口實(shí)現(xiàn)文件拖放
break;
case WM_SIZE: //當(dāng)窗口大小改變的時(shí)候,編輯框與狀態(tài)欄跟著改變
MoveWindow(hWndEdit,0,0,LOWORD(lParam),HIWORD(lParam),true); //編輯框
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_O:
SendMessage(hWndEdit,EM_SETSEL,0,-1); //選中所有文本
SendMessage(hWndEdit,EM_REPLACESEL,wParam,(DWORD)lpTemp); //用空串代碼
EnableWindow(hWndEdit,0); //使編輯框不能接受輸入
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.Flags = OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_FILEMUSTEXIST |OFN_PATHMUSTEXIST ;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFileTitle=szFileTitle;
ofn.lpstrInitialDir=NULL;
ofn.nMaxFileTitle = NULL;
ofn.lpstrFilter="Text Files(*.TXT)\0*.txt\0All Files (*.*)\0*.*\0\0";
ofn.hwndOwner=hWnd;
if (GetOpenFileName(&ofn)==TRUE)
{
EnableWindow(hWndEdit,1); //使編輯框可以接受輸入
FileToEditBox(hWnd,szFile); //文件到編輯框[自定義函數(shù)]
}
EnableWindow(hWndEdit,1); //使編輯框可以接受輸入
flag1=true; //打開了一個(gè)文件,flag=true
flag4=false; //打開了一個(gè)文件,還未保存,flag4=false
return 0;
case IDM_NEW:
//彈出文件名對話框
SendMessage(hWndEdit,EM_SETSEL,0,-1); //選中所有文本
SendMessage(hWndEdit,EM_REPLACESEL,wParam,(DWORD)lpTemp); //用空串代碼
EnableWindow(hWndEdit,1); //允許編輯框輸入
SetFocus(hWndEdit);
flag2=true; //新建了文件,flag2=true
flag4=false; //新建了文件,還未保存,flag4=false
return 0;
case IDM_CLOSE:
SendMessage(hWndEdit,EM_SETSEL,0,-1); //選中所有文本
SendMessage(hWndEdit,EM_REPLACESEL,wParam,(DWORD)lpTemp); //用空串代碼
EnableWindow(hWndEdit,0); //禁止編輯框輸入
flag1=false;
flag2=false;
return 0;
case IDM_S:
//將編輯框中所有內(nèi)容輸出到文件
if(flag1==true || flag2==true)
{
if(flag4==false)
{
ofn.Flags=OFN_ALLOWMULTISELECT|OFN_EXPLORER|OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFileTitle=szFileTitle;
ofn.lpstrInitialDir=NULL;
ofn.nMaxFileTitle = NULL;
ofn.lpstrFilter="Text Files(*.TXT)\0*.txt\0All Files (*.*)\0*.*\0\0";
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrTitle ="另存為...";
ofn.hwndOwner=hWnd; //父窗口句柄
EnableWindow(hWndEdit,0); //使編輯框不能接受輸入
GetSaveFileName(&ofn);
EnableWindow(hWndEdit,1); //使編輯框可以接受輸入
p=szFileTitle;
while(*p!='\0')
{
if(*p=='.') //字符串里有"."號,說明文件名帶了后綴
{
flag3=true; //文件名帶了后綴, flag3=true
if(check(p,".txt") || check(p,".TXT")) //如果后綴是".txt" 或 ".TXT"
break; //OK,跳出循環(huán)
else //如果后綴不是".txt" 或 ".TXT"
strcat(p,".txt"); //在其末尾加上".txt"
}
p++;
}
if(flag3==false)
{
strcat(p,".txt"); //文件名沒有帶后綴,我們給它加上后綴
}
}
EditBoxToFile(hWnd,szFileTitle);
flag4=true;
}
return 0;
case IDM_SERVES:
if(flag1==true || flag2==true)
{
ofn.Flags = OFN_ALLOWMULTISELECT | OFN_EXPLORER |OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFileTitle=szFileTitle;
ofn.lpstrInitialDir=NULL;
ofn.nMaxFileTitle = NULL;
ofn.lpstrFilter="Text Files(*.TXT)\0*.txt\0All Files (*.*)\0*.*\0\0";
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrTitle ="另存為...";
ofn.hwndOwner=hWnd; //父窗口句柄
EnableWindow(hWndEdit,0); //使編輯框不能接受輸入
GetSaveFileName(&ofn);
EnableWindow(hWndEdit,1); //使編輯框可以接受輸入
p=szFileTitle;
while(*p!='\0')
{
if(*p=='.') //字符串里有"."號,說明文件名帶了后綴
{
flag1=true; //設(shè)置flag=false;
if(check(p,".txt") || check(p,".TXT")) //如果后綴是".txt" 或 ".TXT"
break; //OK,返回
else //如果后綴不是".txt" 或 ".TXT"
strcat(p,".txt"); //在其末尾加上".txt"
}
else //若字符串里沒有"."號,說明文件名沒有帶后綴
flag1=false; //設(shè)置flag=false;
p++;
}
if(flag1==false) //若flag=false,說明文件名沒有帶后綴
{
strcat(szFileTitle,".txt"); //給文件名加上后綴".txt"
flag1=true;
}
if(flag1==true)
{
EditBoxToFile(hWnd,szFileTitle);
flag4=true;
}
}
return 0;
case IDM_EXIT:
SendMessage(hWnd,WM_DESTROY,0,0);
return 0;
case IDM_C:
// EditBoxToClip1(hWnd);
SendMessage((HWND)hWndEdit,WM_COPY,0,0);
return 0;
case IDM_X:
// EditBoxToClip2(hWnd);
SendMessage((HWND)hWndEdit,WM_CUT,0,0);
return 0;
case IDM_V:
// ClipToEditBox(hWnd);
SendMessage((HWND)hWndEdit,WM_PASTE,0,0);
return 0;
//關(guān)于
case IDM_ABOUT:
DialogBox(hInst,(LPCSTR)IDD_DIALOG1,hWnd,(DLGPROC)DlgProc);
break;
//頁面設(shè)置
case IDM_U:
pd1.lStructSize=sizeof(PAGESETUPDLG);
pd1.hwndOwner = hWnd;
pd1.hDevMode;
pd1.hDevNames;
pd1.Flags=PSD_DEFAULTMINMARGINS|PSD_INWININIINTLMEASURE|PSD_MINMARGINS|PSD_MARGINS|PSD_INTHOUSANDTHSOFINCHES;
pd1.ptPaperSize;
pd1.rtMinMargin;
pd1.rtMargin;
pd1.hInstance;
pd1.lCustData;
pd1.lpfnPageSetupHook;
pd1.lpfnPagePaintHook;
pd1.lpPageSetupTemplateName;
pd1.hPageSetupTemplate;
PageSetupDlg(&pd1);
break;
//打印
case IDM_P:
page.lStructSize=sizeof(PRINTDLG);
page.hwndOwner = hWnd;
page.hDevMode;
page.hDevNames;
page.hDC=0;
page.Flags=PD_ALLPAGES|PD_SELECTION|PD_PAGENUMS|PD_PRINTSETUP;
page.nFromPage;
page.nToPage;
page.nMinPage;
page.nMaxPage;
page.nCopies;
page.hInstance;
page.lCustData;
page.lpfnPrintHook;
page.lpfnSetupHook;
page.lpPrintTemplateName;
page.lpSetupTemplateName;
page.hPrintTemplate;
page.hSetupTemplate;
// ReleaseDC(hWnd,hdcp);
PrintDlg(&page);
break;
//換行
case IDM_W:
hmenu=GetMenu(hWnd);
if(CheckMenuItem(hmenu,IDM_W,MF_CHECKED))
CheckMenuItem(hmenu,IDM_W,MF_UNCHECKED);
break;
//查找替換
case IDM_F:
find.lStructSize=sizeof(FINDREPLACE);
find.hwndOwner=hWnd;
find.hInstance=NULL;
find.Flags=FR_DOWN|FR_FINDNEXT|FR_MATCHCASE|FR_WHOLEWORD;
find.lpstrFindWhat=FindWhat;
find.lpstrReplaceWith=ReplaceWith;
find.wFindWhatLen=255;
find.wReplaceWithLen=255;
// find.lCustData;
// find.lpfnHook;
// find.lpTemplateName;
FindText(&find);
break;
//替換
case IDM_H:
find.lStructSize=sizeof(FINDREPLACE);
find.hwndOwner=hWnd;
find.hInstance=NULL;
find.Flags=FR_DOWN|FR_FINDNEXT|FR_MATCHCASE|FR_WHOLEWORD;
find.lpstrFindWhat=FindWhat;
find.lpstrReplaceWith=ReplaceWith;
find.wFindWhatLen=255;
find.wReplaceWithLen=255;
// find.lCustData;
// find.lpfnHook;
// find.lpTemplateName;
ReplaceText(&find);
break;
//字體對話框
case IDM_FONT:
HDC hdcf;
hdcf=GetDC(hWnd);
LOGFONT lf;
HFONT hfont;
fontBox.lpTemplateName=NULL;
fontBox.lStructSize=sizeof(CHOOSEFONT);
fontBox.nSizeMax=100;
fontBox.nSizeMin=0;
fontBox.rgbColors=0;
fontBox.lpszStyle=NULL;
fontBox.Flags=CF_EFFECTS|CF_SCREENFONTS;
fontBox.hDC=hdcf;
fontBox.hInstance=NULL;
fontBox.hwndOwner=hWnd;
fontBox.iPointSize=10;
fontBox.lCustData=0;
fontBox.lpfnHook=NULL;
fontBox.nFontType = SCREEN_FONTTYPE;
fontBox.lpLogFont=&lf;
ChooseFont(&fontBox);
hfont=CreateFontIndirect(fontBox.lpLogFont);
// SelectObject(hdcf,hfont);
// SetTextColor(hdcf,fontBox.rgbColors);
// TextOut(hdcf,10,10,"字體對話框設(shè)置返回樣式",strlen("字體對話框設(shè)置返回樣式"));
SendMessage(hWndEdit,WM_SETFONT,(WPARAM)hfont,0);
ReleaseDC(hWnd,hdcf);
InvalidateRect(hWnd,0,TRUE);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -