汇编写的记事本源码

#include <resource.h> #define ICO_MAIN 1000 #define IDA_MAIN 2000 #define IDM_MAIN 2000 #define IDM_NEW 2101 #define IDM_OPEN 2102 #define IDM_SAVE 2103 #define IDM_SAVEAS 2104 #define IDM_PAGESET 2105 #define IDM_EXIT 2106 #define IDM_UNDO 2201 #define IDM_CUT 2202 #define IDM_COPY 2203 #define IDM_PASTE 2204 #define IDM_DELETE 2205 #define IDM_FIND 2206 #define IDM_FINDNEXT 2207 #define IDM_REPLACE 2208 #define IDM_GOTO 2209 #define IDM_SELECTALL 2210 #define IDM_DATETIME 2211 #define IDM_AUTONEWLINE 2301 #define IDM_FONT 2302 #define IDM_STATUSBAR 2401 #define IDM_HELPTHEME 2501 #define IDM_ABOUT 2502 ICO_MAIN ICON "notepad.ico" IDM_MAIN menu discardable BEGIN popup "文件(&F)" BEGIN menuitem "新建(&N)/tCtrl+N", IDM_NEW menuitem "打开(&O).../tCtrl+O", IDM_OPEN menuitem "保存(&S)/tCtrl+S", IDM_SAVE menuitem "另存为(&A)...", IDM_SAVEAS menuitem separator menuitem "页面设置(&P)...", IDM_PAGESET menuitem separator menuitem "退出(&X)" , IDM_EXIT END popup "编辑(&E)" BEGIN menuitem "撤销(&U)/tCtrl+Z", IDM_UNDO, GRAYED menuitem separator menuitem "剪切(&T)/tCtrl+X", IDM_CUT, GRAYED menuitem "复制(&C)/tCtrl+C", IDM_COPY, GRAYED menuitem "粘贴(&P)/tCtrl+V", IDM_PASTE, GRAYED menuitem "删除(&L)/tDel", IDM_DELETE, GRAYED menuitem separator menuitem "查找(&F).../tCtrl+F", IDM_FIND menuitem "查找下一个(&N)/tF3", IDM_FINDNEXT menuitem "替换(&R).../tCtrl+H", IDM_REPLACE menuitem "转到(&G).../tCtrl+G", IDM_GOTO, GRAYED menuitem separator menuitem "全选(&A)/tCtrl+A", IDM_SELECTALL menuitem "时间/日期(&D)/tF5", IDM_DATETIME END popup "格式(&O)" BEGIN menuitem "自动换行(&W)", IDM_AUTONEWLINE, CHECKED menuitem "字体(&F)...", IDM_FONT END popup "查看(&V)" BEGIN menuitem "状态栏(&S)", IDM_STATUSBAR, GRAYED END popup "帮助(&H)" BEGIN menuitem "帮助主体(&H)", IDM_HELPTHEME menuitem "关于记事本(&A)", IDM_ABOUT END END IDA_MAIN accelerators BEGIN VK_F3, IDM_FINDNEXT, VIRTKEY VK_F5, IDM_DATETIME, VIRTKEY "N", IDM_NEW, VIRTKEY, CONTROL "O", IDM_OPEN, VIRTKEY, CONTROL "S", IDM_SAVE, VIRTKEY, CONTROL "Z", IDM_UNDO, VIRTKEY, CONTROL "X", IDM_CUT, VIRTKEY, CONTROL "C", IDM_COPY, VIRTKEY, CONTROL "V", IDM_PASTE, VIRTKEY, CONTROL VK_DELETE, IDM_DELETE, VIRTKEY "F", IDM_FIND, VIRTKEY, CONTROL "H", IDM_REPLACE, VIRTKEY, CONTROL "G", IDM_GOTO, VIRTKEY, CONTROL "A", IDM_SELECTALL, VIRTKEY, CONTROL END

写的不够完整。基本上是按照windows自带记事本写的。但是还稍微有点缺陷。

.386 .model flat, stdcall option casemap: none include windows.inc include user32.inc includelib user32.lib include kernel32.inc includelib kernel32.lib include comdlg32.inc includelib comdlg32.lib ICO_MAIN equ 1000 IDA_MAIN equ 2000 IDM_MAIN equ 2000 IDM_NEW equ 2101 IDM_OPEN equ 2102 IDM_SAVE equ 2103 IDM_SAVEAS equ 2104 IDM_PAGESET equ 2105 IDM_EXIT equ 2106 IDM_UNDO equ 2201 IDM_CUT equ 2202 IDM_COPY equ 2203 IDM_PASTE equ 2204 IDM_DELETE equ 2205 IDM_FIND equ 2206 IDM_FINDNEXT equ 2207 IDM_REPLACE equ 2208 IDM_GOTO equ 2209 IDM_SELECTALL equ 2210 IDM_DATETIME equ 2211 IDM_AUTONEWLINE equ 2301 IDM_FONT equ 2302 IDM_STATUSBAR equ 2401 IDM_HELPTHEME equ 2501 IDM_ABOUT equ 2502 .data? hInstance dd ? hWinMain dd ? hMenu dd ? hDllInstance dd ? hWinEdit dd ? idFindMessage dd ? hFile dd ? hFindDialog dd ? szFindText db 100 dup (?) szReplaceText db 100 dup (?) szFileName db MAX_PATH dup (?) szFileTitle db MAX_PATH dup (?) stLogFont LOGFONT <?> .data stFind FINDREPLACE <sizeof FINDREPLACE, 0, 0, FR_DOWN, szFindText, 0, sizeof szFindText, 0, 0, 0, 0> .const FINDMSGSTRING db 'commdlg_FindReplace', 0 szNotFound db '字符串未找到!', 0 szCaptionMain db 'notepad', 0 szClassName db 'Notepad', 0 szDllEdit db 'RichEd20.dll', 0 szClassEdit db 'RichEdit20A', 0 szFont db '宋体',0 szModify db '文件%s已修改,是否保存?', 0 szFilter db 'Text Files(*.txt)', 0, '*.txt', 0 db 'All Files(*.*)', 0, '*.*', 0, 0 szDefaultExt db '.txt', 0 szErrOpenFile db '打开文件失败', 0 szValue db 'eax的值为%d', 0 szErrPage db '不存在打印机,请安装打印机后再设置', 0 szDateTime db '%d:%d %d-%d-%d', 0 .code ;RichiEdit的流操作 _ProcStream proc uses ebx edi esi _dwCookie, _lpBuffer, _dwBytes, _lpBytes .if _dwCookie invoke ReadFile, hFile, _lpBuffer, _dwBytes, _lpBytes, 0 .else invoke WriteFile, hFile, _lpBuffer, _dwBytes, _lpBytes, 0 .endif xor eax, eax ret _ProcStream endp ;保存文件,由于流操作会输出全部的内容,所以首先将内容清空 _SaveFile proc LOCAL @stES:EDITSTREAM invoke SetFilePointer, hFile, 0, 0, FILE_BEGIN invoke SetEndOfFile, hFile mov @stES.dwCookie, FALSE mov @stES.pfnCallback, offset _ProcStream invoke SendMessage, hWinEdit, EM_STREAMOUT, SF_TEXT, addr @stES invoke SendMessage, hWinEdit, EM_SETMODIFY, FALSE, 0 ret _SaveFile endp ;另存为即输出文件 _SaveAs proc LOCAL @stOF:OPENFILENAME invoke RtlZeroMemory, addr @stOF, sizeof OPENFILENAME mov @stOF.lStructSize, sizeof OPENFILENAME push hWinMain pop @stOF.hwndOwner mov @stOF.lpstrFile, offset szFileName mov @stOF.lpstrFilter, offset szFilter mov @stOF.nMaxFile, MAX_PATH mov @stOF.Flags, OFN_PATHMUSTEXIST mov @stOF.lpstrDefExt, offset szDefaultExt mov @stOF.lpstrFileTitle, offset szFileTitle mov @stOF.nMaxFileTitle, MAX_PATH invoke GetSaveFileName, addr @stOF .if eax invoke CreateFile, addr szFileName, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, / 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 .else ret .endif mov hFile, eax call _SaveFile ;改变记事本的标题 invoke SetWindowText, hWinMain, addr szFileTitle ret _SaveAs endp ;检查是否被更改过 _CheckModify proc LOCAL @dwReturn LOCAL @szBuf[256]:byte mov @dwReturn, TRUE invoke SendMessage, hWinEdit,EM_GETMODIFY, 0, 0 .if eax invoke wsprintf, addr @szBuf, addr szModify , addr szFileTitle invoke MessageBox, hWinMain, addr @szBuf, addr szCaptionMain, MB_YESNOCANCEL or MB_ICONQUESTION .if eax == IDYES .if hFile call _SaveFile .else call _SaveAs ;多注意返回值判断即能取消退出整个程序 mov @dwReturn,eax .endif .elseif eax == IDCANCEL mov @dwReturn, FALSE .endif .endif mov eax, @dwReturn ret _CheckModify endp ;打开文件及输入文件 _OpenFile proc LOCAL @stOF:OPENFILENAME LOCAL @stES:EDITSTREAM ;打开对话框的通用控件 invoke RtlZeroMemory, addr @stOF, sizeof @stOF mov @stOF.lStructSize, sizeof @stOF push hWinMain pop @stOF.hwndOwner mov @stOF.lpstrFile, offset szFileName mov @stOF.lpstrFilter, offset szFilter mov @stOF.nMaxFile, MAX_PATH mov @stOF.lpstrFileTitle, offset szFileTitle mov @stOF.nMaxFileTitle, MAX_PATH mov @stOF.Flags, OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST mov @stOF.lpstrDefExt, offset szDefaultExt invoke GetOpenFileName, addr @stOF .if eax ;创建文件 invoke CreateFile, addr szFileName, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, / 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 .if eax == INVALID_HANDLE_VALUE invoke MessageBox, hWinMain, addr szErrOpenFile, NULL, MB_OK or MB_ICONSTOP ret .endif push eax .if hFile invoke CloseHandle, hFile .endif pop eax mov hFile,eax ;读取文件 mov @stES.dwCookie, TRUE mov @stES.dwError, NULL mov @stES.pfnCallback, offset _ProcStream invoke SendMessage, hWinEdit, EM_STREAMIN, SF_TEXT, addr @stES invoke SendMessage, hWinEdit, EM_SETMODIFY, FALSE, 0 .endif invoke SetWindowText, hWinMain, addr szFileTitle ret _OpenFile endp ;页面设置通用对话框 _PageSetup proc LOCAL @stPS:PAGESETUPDLG invoke RtlZeroMemory, addr @stPS, sizeof @stPS mov @stPS.lStructSize, sizeof @stPS push hWinMain pop @stPS.hwndOwner invoke PageSetupDlg, addr @stPS ret _PageSetup endp ;根据情况来改变菜单状态 _SetStatus proc LOCAL @stRange:CHARRANGE ;得到选中的区域,根据是否选中决定是否灰化剪切复制菜单 invoke SendMessage, hWinEdit, EM_EXGETSEL, 0, addr @stRange mov eax, @stRange.cpMin .if eax == @stRange.cpMax invoke EnableMenuItem, hMenu, IDM_COPY, MF_GRAYED invoke EnableMenuItem, hMenu, IDM_CUT, MF_GRAYED invoke EnableMenuItem, hMenu, IDM_DELETE, MF_GRAYED .else invoke EnableMenuItem, hMenu, IDM_COPY, MF_ENABLED invoke EnableMenuItem, hMenu, IDM_CUT, MF_ENABLED invoke EnableMenuItem, hMenu, IDM_DELETE, MF_ENABLED .endif ;检查是否可以粘贴 invoke SendMessage, hWinEdit, EM_CANPASTE, 0, 0 .if eax invoke EnableMenuItem, hMenu, IDM_PASTE, MF_ENABLED .else invoke EnableMenuItem, hMenu, IDM_PASTE, MF_GRAYED .endif ;检查是否可以撤销上一步操作 invoke SendMessage, hWinEdit, EM_CANUNDO, 0, 0 .if eax invoke EnableMenuItem, hMenu, IDM_UNDO, MF_ENABLED .else invoke EnableMenuItem, hMenu, IDM_UNDO, MF_GRAYED .endif ret _SetStatus endp ;查找文字 _FindText proc LOCAL @stFindText:FINDTEXTEX LOCAL @dwReturn mov @dwReturn, TRUE ;设置查找范围 invoke SendMessage, hWinEdit, EM_EXGETSEL, 0, addr @stFindText.chrg .if stFind.Flags & FR_DOWN push @stFindText.chrg.cpMax pop @stFindText.chrg.cpMin .endif mov @stFindText.chrg.cpMax, -1 ;设置查找选项 mov @stFindText.lpstrText, offset szFindText mov ecx, stFind.Flags and ecx, FR_MATCHCASE or FR_DOWN or FR_WHOLEWORD ;查找并且把光标设置到查找的文本上 invoke SendMessage, hWinEdit, EM_FINDTEXTEX, ecx, addr @stFindText .if eax == -1 mov ecx, hWinMain .if hFindDialog mov ecx, hFindDialog .endif invoke MessageBox, ecx, addr szNotFound, NULL, MB_OK or MB_ICONINFORMATION mov eax, FALSE ret .endif invoke SendMessage, hWinEdit, EM_EXSETSEL, 0, addr @stFindText.chrgText invoke SendMessage, hWinEdit, EM_SCROLLCARET, NULL, NULL mov eax, @dwReturn ret _FindText endp _Init proc ;字体设置 LOCAL @stCf:CHARFORMAT ;注册查找对话框消息, 初始化查找对话框的结构 ;如果想要调用替换成功,必须设置FINDTEXT里面的替换文本 push hWinMain pop stFind.hwndOwner mov stFind.lStructSize, sizeof stFind mov stFind.Flags, FR_DOWN mov stFind.lpstrFindWhat, offset szFindText mov stFind.wFindWhatLen, sizeof szFindText mov stFind.lpstrReplaceWith, offset szReplaceText mov stFind.wReplaceWithLen, sizeof szReplaceText invoke RegisterWindowMessage, addr FINDMSGSTRING mov idFindMessage, eax ;建立输出文本窗口 invoke CreateWindowEx, WS_EX_WINDOWEDGE, offset szClassEdit, NULL, WS_CHILD or WS_VISIBLE / or WS_VSCROLL or WS_HSCROLL or ES_MULTILINE or ES_NOHIDESEL or ES_DISABLENOSCROLL , / 0, 0, 0, 0, hWinMain, 0, hInstance, NULL mov hWinEdit, eax ;字体等属性设置 invoke SendMessage, hWinEdit, EM_SETTEXTMODE, TM_PLAINTEXT, NULL invoke RtlZeroMemory, addr @stCf, sizeof @stCf mov @stCf.cbSize, sizeof @stCf mov @stCf.yHeight, 9 * 30 mov @stCf.dwMask, CFM_FACE or CFM_SIZE or CFM_BOLD invoke lstrcpy, addr @stCf.szFaceName, addr szFont invoke SendMessage, hWinEdit, EM_SETCHARFORMAT, 0, addr @stCf ;扩充文本大小的 invoke SendMessage, hWinEdit, EM_EXLIMITTEXT, 0, -1 ret _Init endp ;字体设置对话框 _FontSet proc LOCAL @stCF:CHOOSEFONT invoke RtlZeroMemory, addr @stCF, sizeof @stCF mov @stCF.lStructSize, sizeof @stCF push hWinMain pop @stCF.hwndOwner mov @stCF.lpLogFont, offset stLogFont mov @stCF.Flags, CF_SCREENFONTS or CF_INITTOLOGFONTSTRUCT or CF_EFFECTS invoke ChooseFont, addr @stCF ret _FontSet endp _ProcWinMain proc uses ebx edi esi hWnd, uMsg, wParam, lParam LOCAL @stRect:RECT LOCAL @stRange:CHARRANGE LOCAL @stTM:SYSTEMTIME LOCAL @szBuf[100]:byte LOCAL @dw1, @dw2, @dw3, @dw4 LOCAL @hdc:dword mov eax, uMsg .if eax == WM_COMMAND mov eax, wParam movzx eax, ax .if eax == IDM_SAVE .if hFile call _SaveFile .else call _SaveAs .endif .elseif eax == IDM_OPEN call _CheckModify call _OpenFile .elseif eax == IDM_SAVEAS call _SaveAs .elseif eax == IDM_NEW call _CheckModify .if eax invoke SetWindowText,hWinEdit, NULL .endif .elseif eax == IDM_PAGESET invoke _PageSetup .elseif eax == IDM_EXIT invoke _CheckModify .if eax invoke DestroyWindow, hWnd invoke PostQuitMessage, NULL .endif .elseif eax == IDM_UNDO invoke SendMessage, hWinEdit, EM_UNDO, 0, 0 .elseif eax == IDM_CUT invoke SendMessage, hWinEdit, WM_CUT, 0, 0 .elseif eax == IDM_COPY invoke SendMessage, hWinEdit, WM_COPY, 0, 0 .elseif eax == IDM_PASTE invoke SendMessage, hWinEdit, WM_PASTE, 0, 0 .elseif eax == IDM_DELETE invoke SendMessage, hWinEdit, EM_REPLACESEL, TRUE, NULL .elseif eax == IDM_FIND ;FR_DIALOGTERM是取消按钮 and stFind.Flags, not FR_DIALOGTERM invoke FindText, addr stFind .if eax mov hFindDialog, eax .endif .elseif eax == IDM_FINDNEXT or stFind.Flags, FR_DOWN and stFind.Flags, not FR_DIALOGTERM invoke FindText, addr stFind .if eax mov hFindDialog, eax .endif .elseif eax == IDM_REPLACE and stFind.Flags, not FR_DIALOGTERM invoke ReplaceText, addr stFind .elseif eax == IDM_SELECTALL mov @stRange.cpMin, 0 mov @stRange.cpMax, -1 invoke SendMessage,hWinEdit, EM_EXSETSEL, 0, addr @stRange .elseif eax == IDM_DATETIME ;系统时间的值是word必须用movzx把其变成dword invoke GetLocalTime, addr @stTM movzx eax, @stTM.wHour mov @dw1,eax movzx eax, @stTM.wMinute mov @dw2, eax movzx eax, @stTM.wYear mov @dw3, eax movzx eax, @stTM.wMonth mov @dw4, eax movzx eax, @stTM.wDay invoke wsprintf, addr @szBuf, addr szDateTime, @dw1, @dw2, @dw3, @dw4, eax invoke SendMessage, hWinEdit,EM_REPLACESEL, TRUE, addr @szBuf .elseif eax == IDM_AUTONEWLINE invoke GetMenuState, hMenu, eax, MF_BYCOMMAND .if eax == MF_CHECKED invoke SendMessage, hWinEdit,EM_SETTARGETDEVICE, 0, 1 mov eax, MF_UNCHECKED .else invoke GetDC, hWinEdit mov @hdc, eax invoke SendMessage,hWinEdit, EM_SETTARGETDEVICE, eax, 0 invoke ReleaseDC, hWinEdit, @hdc mov eax, MF_CHECKED .endif invoke CheckMenuItem, hMenu, IDM_AUTONEWLINE, eax .elseif eax == IDM_FONT call _FontSet invoke SendMessage, hWinEdit, EM_SETCHARFORMAT, 0, addr @stCf .endif ;获得焦点 .elseif eax == WM_ACTIVATE mov eax, wParam .if (ax == WA_CLICKACTIVE) || (ax == WA_ACTIVE) invoke SetFocus, hWinEdit .endif .elseif eax == WM_CREATE push hWnd pop hWinMain call _Init .elseif eax == WM_CLOSE invoke _CheckModify .if eax invoke DestroyWindow, hWnd invoke PostQuitMessage, NULL .endif .elseif eax == WM_SIZE invoke GetClientRect, hWinMain, addr @stRect invoke MoveWindow, hWinEdit, 0, 0, @stRect.right, @stRect.bottom, TRUE .elseif eax == WM_INITMENU call _SetStatus ;处理查找消息的返回消息 .elseif eax == idFindMessage .if stFind.Flags & FR_DIALOGTERM mov hFindDialog, 0 .elseif stFind.Flags & FR_FINDNEXT invoke _FindText .elseif stFind.Flags & FR_REPLACE invoke SendMessage, hWinEdit, EM_EXGETSEL, 0, addr @stRange mov eax, @stRange.cpMin .if eax != @stRange.cpMax invoke SendMessage, hWinEdit, EM_REPLACESEL,TRUE, addr szReplaceText .endif invoke _FindText .elseif stFind.Flags & FR_REPLACEALL .while TRUE invoke _FindText ;invoke wsprintf, addr @szBuf, addr szValue, eax ;invoke MessageBox, hWinMain, addr @szBuf, NULL, MB_OK .break .if eax == FALSE invoke SendMessage, hWinEdit, EM_REPLACESEL,TRUE, addr szReplaceText .endw .endif .else invoke DefWindowProc, hWnd, uMsg, wParam, lParam ret .endif xor eax, eax ret _ProcWinMain endp _WinMain proc LOCAL @stWndClass:WNDCLASSEX LOCAL @stMsg:MSG LOCAL @hAccelerator ;载入RichEdit控件 invoke LoadLibrary, addr szDllEdit mov hDllInstance, eax ;得到窗口句柄 invoke GetModuleHandle, NULL mov hInstance, eax ;载入菜单和加速键 invoke LoadMenu, hInstance, IDM_MAIN mov hMenu, eax invoke LoadAccelerators, hInstance, IDA_MAIN mov @hAccelerator, eax ;注册窗口 invoke RtlZeroMemory, addr @stWndClass, sizeof @stWndClass invoke LoadIcon, hInstance, ICO_MAIN mov @stWndClass.hIcon, eax mov @stWndClass.hIconSm, eax invoke LoadCursor, 0, IDC_ARROW mov @stWndClass.hCursor, eax push hInstance pop @stWndClass.hInstance mov @stWndClass.cbSize, sizeof WNDCLASSEX mov @stWndClass.style, CS_HREDRAW or CS_VREDRAW mov @stWndClass.lpfnWndProc, offset _ProcWinMain mov @stWndClass.hbrBackground, COLOR_BTNFACE + 1 mov @stWndClass.lpszClassName, offset szClassName invoke RegisterClassEx, addr @stWndClass ;建立并显示窗口 invoke CreateWindowEx, NULL, offset szClassName, offset szCaptionMain, WS_OVERLAPPEDWINDOW or WS_BORDER,CW_USEDEFAULT , CW_USEDEFAULT,700, 500,NULL,hMenu, hInstance, NULL mov hWinMain, eax invoke ShowWindow, hWinMain, SW_SHOWNORMAL invoke UpdateWindow, hWinMain .while TRUE invoke GetMessage, addr @stMsg, NULL, 0, 0 .break .if eax == 0 invoke TranslateAccelerator, hWinMain, @hAccelerator, addr @stMsg .if eax == 0 invoke TranslateMessage, addr @stMsg invoke DispatchMessage, addr @stMsg .endif .endw ;释放装入的Dll库文件 invoke LoadLibrary, addr szDllEdit ret _WinMain endp start: call _WinMain invoke ExitProcess, NULL end start

上面是资源文件

 

你可能感兴趣的:(汇编,File,null,delete,Path,include)