c语言的记事本源代码

合集下载

记事本代码

记事本代码
HINSTANCE hPrevInstance,//第二个参数hPrevInstance表示当前实例的前一个实例的句柄。
PSTR szCmdLine, int iCmdShow )//第三个参数lpCmdLine是一个以空终止的字符串,指定传递给应用程序的命令行参数?
{
static TCHAR szAppName[] = TEXT( "demo" ) ;
return 0 ;
case WM_SIZE:
GetClientRect(hwnd, &rect) ;
MoveWindow( hwndChild[ID_EDITBOX], 0, 0, rect.right, rect.bottom-50, TRUE ) ;//调整文本编辑区
MoveWindow( hwndChild[ID_TXTPATH], 100, rect.bottom-31, 400, 20, TRUE ) ;//调整文本路径输入框
TextOut( hdc, 800, rect.bottom-30, szLineNum, lstrlen(szLineNum) ) ;
EndPaint( hwnd, &ps ) ;
return 0 ;
case WM_COMMAND:
wmId=LOWORD(wParam);
wmEvent=HIWORD(wParam);
return -1 ;
}
else{
fwrite(content, lstrlen(content), sizeof(char), fSvae);
MessageBox(NULL, TEXT("保存成功!"), TEXT("成功"), MB_OK | MB_ICONINFORMATION) ;

C_实验-记事本(带源码)

C_实验-记事本(带源码)

记事本实验报告一、实验目的创建一个Windows窗体应用程序,实现记事本的基本功能,具体包括新建文件、打开文件、保存文件、查找等功能。

该实验的目的是掌握:(一)窗体程序的开发(二)常用控件的使用(三)常用事件的处理(四)对话框的设计和使用(五)文件访问的基本方法二、实验内容(一)主窗口Form1图1 主窗口主窗口界面如图1所示,功能包括基本编辑操作、主菜单和其它快捷键功能。

1、编辑功能用文本框实现。

2、窗口标题与文件名相一致。

未打开文件时为“无标题”,打开文件(另存为)后为文件名。

3、支持菜单的热键和快捷键。

二者的区别是前者是激活菜单且显示出该菜单项时有效,后者在任何时候有效。

4、实现新建、打开、保存和查找功能。

5、支持F3(查找下一个)。

表1 Form1控件列表using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace WindowsFormsApplication1{public partial class Form1 : Form{public Form2 fm2 = null;public string searchText = "";public Form1(){InitializeComponent();}private void saveFile(){if (textBox1.Text.Length > 0 && textBox1.Modified) {if (MessageBox.Show("想保存文件吗?", "记事本",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning) == DialogResult.Yes){SaveFileDialog d = new SaveFileDialog();d.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";if (d.ShowDialog() == DialogResult.OK){FileStream fs = File.OpenWrite(d.FileName);StreamWriter sr = new StreamWriter(fs);sr.Write(textBox1.Text);sr.Flush();fs.Close();}}}}private void文件NToolStripMenuItem_Click(object sender, EventArgs e) {saveFile();textBox1.Text = "";Text = "无标题 - 记事本";}private void OpenFile(){OpenFileDialog d = new OpenFileDialog();d.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";if (d.ShowDialog() == DialogResult.OK){FileStream fs = File.OpenRead(d.FileName);StreamReader sr = new StreamReader(fs);string s;string s1 = "";while ((s = sr.ReadLine()) != null){s1 += s;}textBox1.Text = s1;string fname = d.FileName.Substring(stIndexOf("\\") + 1);Text = fname + " - 记事本";}}private void打开OToolStripMenuItem_Click(object sender, EventArgs e) {saveFile();OpenFile();}private void保存SToolStripMenuItem_Click(object sender, EventArgs e) {saveFile();}private void查找FToolStripMenuItem_Click(object sender, EventArgs e) {if (fm2 == null){fm2 = new Form2();fm2.fm1 = this;Form2.textBox2 = textBox1;fm2.Show();}elsefm2.Activate();}private void textBox1_KeyDown(object sender, KeyEventArgs e){if (e.KeyCode == Keys.A && e.Control && !e.Shift && !e.Alt){textBox1.SelectAll();e.Handled = true;}else if (e.KeyCode == Keys.F3 && !e.Control && !e.Shift && !e.Alt) {Form2.findNext();}}}}(二)查找对话框图2 查找对话框查找对话框的界面(图2)与记事本的相同。

c语言课设实验报告-记事本程序(含源码)

c语言课设实验报告-记事本程序(含源码)

C语言课设实验报告[记事本程序]班级:学号:姓名:指导老师:C语言课设实验报告#define key_down 80#define key_up 72#define key_esc 1#define key_alt_f 33#define key_alt_x 45#define key_enter 28#define key_alt_e 18int get_key();void box(int startx,int stary,int high,int width);{int i,key,x,y,l;char *w=NULL,*d=NULL,*s=NULL;FILE *fp;char *menu[]={ "File ", "Edit ","Format","View","Help"};char *red[]={ "F", "E","F","V","H"};char *f[]={ "New ", "Open" , "Save", "Another","Quit alt_x"};char *e[]={"Search","Search next","Replace","Date"};char buf[16*10*2],buf1[16*2];textbackground(3);clrscr();window(1,1,80,1);textbackground(WHITE);textcolor(BLUE);clrscr();window(1,1,80,2);for(i=0, l=0; i <5;i++){x=wherex();y=wherey();textcolor(BLACK);cprintf( " %s ",menu[i]);l=strlen(menu[i]);gotoxy(x,y); x.y等的头文件*//*功能键的扫描码的定义*//* i为循环次数的序数,key为从键盘输入的功能键,x,y 为光标的坐标,l为菜单栏中菜单的字符长度*//*w是存放输入的内容(记录的事情)的指针,*s是存放输入的文件名的指针,*d是存放输入要定时的文件的文件名的指针*/ /*菜单栏中各个菜单的定义*//*菜单栏中菜单开头要标颜色的字母的定义*//* file 子菜单中项目的定义*//*edit子菜单的定义*/ /*缓冲区的定义*//*整个大窗口的背景颜色设置*//*创建菜单栏窗口*/ /*设置菜单栏窗口背景颜色为白色,字体颜色为蓝色*//*利用循环输出menu菜单栏,有多少个菜单循环多少次。

听说高手都用记事本写C语言代码?那你知道怎么编译运行吗?

听说高手都用记事本写C语言代码?那你知道怎么编译运行吗?

听说⾼⼿都⽤记事本写C语⾔代码?那你知道怎么编译运⾏吗?
坊间传闻⾼⼿都喜欢⽤记事本写代码,那么问题来了,我们以C语⾔为例,如何⽤记事本编译运⾏呢?其实最简单的⽅式就是安装GCC编译器,在记事本编写C语⾔程序,然后再在命令⾏⽤GCC编译运⾏,下⾯我简单介绍⼀下实现过程,感兴趣的朋友可以尝试⼀下:
安装GCC编译器
这⾥推荐直接安装MinGW,⼀个集成编译调试⼯具,集成了常见的GCC、G++、GDB等命令,可以轻松编译运⾏C语⾔程序,下载的话,直接到官⽹上下载即可,⼀个exe⽂件,直接双击安装就⾏,安装后主⽬录如下,这⾥需要将bin⽬录添加到环境变量中,后⾯在cmd窗⼝就可以直接使⽤GCC命令编译C语⾔程序:
打开cmd窗⼝,输⼊“gcc -v”命令,如果输出以下信息,则说明GCC命令配置成功:
编译运⾏程序
GCC命令配置完成后,接着就是编译运⾏C语⾔程序了,这⾥为了⽅便演⽰,我⽤记事本新建了⼀个C⽂件,测试代码如下,⾮常简单,后⾯就是⽤GCC编译运⾏这个程序:
接着在cmd窗⼝切换到⽂件所在⽬录,运⾏命令“gcc -o test test.c”就可直接编译程序,成功编译后会在当前⽬录下⽣成⼀个exe程序,可以直接运⾏:
⾄此,我们就完成了记事本C语⾔程序的编译和运⾏。

总的来说,整个过程⾮常简单,主要就是配置GCC编译器(和Linux⾮常相似),只要你熟悉⼀下操作过程,就很容易实现。

当然还有其他很多⽅法实现,⼤家可以⾃⼰去了解⼀下。

希望以上分享的内容对⼤家有所帮助,感谢耐⼼阅读!
C/C++的学习裙【七⼀⼆⼆⼋四七零五】,⽆论你是⼩⽩还是进阶者,是想转⾏还是想⼊⾏都可以来了解⼀起进步⼀起学习!裙内有开发⼯具,很多⼲货和技术资料分享!。

记事本源代码

记事本源代码

记事本源代码先上效果图。

这个记事本操作简便,功能强⼤,在记事本的基础上添加了将内容发送短信和发送邮件的功能。

这个应⽤也已经功过了微软的认证。

115⽹盘⾥⾯的是最新的。

QQ:29992379下载地址:Memo.xap实体类1: public class Note2: {3: public string NoteGuid { get; set; }4: public string NoteContent { get; set; }5: public string NoteTime { get; set; }6: }在独⽴存储中⽣成存储结构。

1: if (!IsolatedStorageSettings.ApplicationSettings.Contains("Notes"))2: {3: List<Note> notes = new List<Note>();4: IsolatedStorageSettings.ApplicationSettings["Notes"] = notes as List<Note>;5: IsolatedStorageSettings.ApplicationSettings.Save();6:7: }绑定⽂章的列表,并按编号倒排序。

1: public partial class MainPage : PhoneApplicationPage2: {3: // 构造函数4: public MainPage()5: {6: InitializeComponent();7: BingData();8: }9: List<Note> notes = new List<Note>();10: private void BingData()11: {12: notes = IsolatedStorageSettings.ApplicationSettings["Notes"] as List<Note>;13:14: var descInfo = from i in notes orderby i.NoteTime descending select i;//linq对⽂章列表的排序15:16: MainListBox.ItemsSource = descInfo;17: }18:19: private void ApplicationBarIconButton_Click(object sender, EventArgs e)20: {21: NavigationService.Navigate(new Uri("/Add.xaml", UriKind.RelativeOrAbsolute));22: }23:24: protected override void OnBackKeyPress(ponentModel.CancelEventArgs e)25: {26: e.Cancel = true;27: App.Quit();28: base.OnBackKeyPress(e);29: }30:31: private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)32: {33: NavigationService.Navigate(new Uri("/About.xaml", UriKind.RelativeOrAbsolute));34: }35:36: private void StackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)37: {38: string noteguid = ((TextBlock)(((StackPanel)sender).Children.First())).Tag.ToString();39: NavigationService.Navigate(new Uri("/DetailsPage.xaml?noteguid=" + noteguid, UriKind.Relative));40: }41: }⽂章显⽰的页⾯以及⼀系列功能1: public partial class DetailsPage : PhoneApplicationPage2: {3: // 构造函数4: public DetailsPage()5: {6: InitializeComponent();7: }8: string noteguid;9: protected override void OnNavigatedTo(NavigationEventArgs e)10: {11: BingData();12: noteguid = NavigationContext.QueryString["noteguid"].ToString();13: foreach (var item in notes)14: {15: if (item.NoteGuid==noteguid)16: {17: ContentText.Text = item.NoteContent;18: TimeText.Text = item.NoteTime;19: return;20: }21: }22: }23:24: List<Note> notes = new List<Note>();25: private void BingData()26: {27: notes = IsolatedStorageSettings.ApplicationSettings["Notes"] as List<Note>;28: }29:30: private void Edit_Click(object sender, EventArgs e)31: {32: NavigationService.Navigate(new Uri("/Edit.xaml?noteguid=" + noteguid.ToString(), UriKind.RelativeOrAbsolute)); 33: }34:35: protected override void OnBackKeyPress(ponentModel.CancelEventArgs e)36: {37: e.Cancel = true;38: NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));39: base.OnBackKeyPress(e);40: }41:42: private void Del_Click(object sender, EventArgs e)43: {44: for (int i = 0; i < notes.Count; i++)45: {46: if (notes[i].NoteGuid==noteguid)47: {48: notes.RemoveAt(i);49: }50: }51: IsolatedStorageSettings.ApplicationSettings["Notes"] = notes as List<Note>;52: IsolatedStorageSettings.ApplicationSettings.Save();53: NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));54: }55:56: private void Email_Click(object sender, EventArgs e)57: {58: EmailComposeTask email = new EmailComposeTask();59: email.Body = ContentText.Text.ToString();60: email.Show();61: }62:63: private void Message_Click(object sender, EventArgs e)64: {65: SmsComposeTask sms = new SmsComposeTask();66: sms.Body = ContentText.Text.ToString();67: sms.Show();68: }69: }⽂章的编辑页⾯代码1: public partial class Edit : PhoneApplicationPage2: {3: public Edit()4: {5: InitializeComponent();6: }7:8: private void ApplicationBarIconButton_Click(object sender, EventArgs e)9: {10: foreach (var item in notes)11: {12: if (item.NoteGuid == noteguid)13: {14: item.NoteContent = ContentText.Text;15: item.NoteTime=TimeText.Text;16: }17: }18:19: IsolatedStorageSettings.ApplicationSettings["Notes"] = notes as List<Note>;20: IsolatedStorageSettings.ApplicationSettings.Save();21: NavigationService.Navigate(new Uri("/DetailsPage.xaml?noteguid=" + noteguid.ToString(), UriKind.RelativeOrAbsolute)); 22: }23: string noteguid;24: protected override void OnNavigatedTo(NavigationEventArgs e)25: {26: BingData();27: noteguid = NavigationContext.QueryString["noteguid"].ToString();28: foreach (var item in notes)29: {30: if (item.NoteGuid==noteguid)31: {32: ContentText.Text = item.NoteContent;33: TimeText.Text = item.NoteTime;34: return;35: }36: }37: }38:39: List<Note> notes = new List<Note>();40: private void BingData()41: {42: notes = IsolatedStorageSettings.ApplicationSettings["Notes"] as List<Note>;43: }44: }。

纯C++代码写的记事本

纯C++代码写的记事本

#include "windows.h"#include "wyyapi.h"#include "inttypes.h"#include <string>#include "stdio.h"LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);HMENU wyymenu=NULL;int i=0;BOOL CreateTopMenu(){wyymenu=CreateMenu();HMENU wyypopup=CreateMenu();HMENU wangpopup=CreateMenu();AppendMenu(wyypopup,MF_STRING,IDM_OPEN,"&打开");AppendMenu(wyypopup,MF_STRING,IDM_EXIT,"&退出");AppendMenu(wangpopup,MF_STRING,IDM_NEW,"&刷新");AppendMenu(wangpopup,MF_STRING,IDM_SAVE,"&保存");AppendMenu(wyymenu,MF_POPUP,(intptr_t)wyypopup,"&王云勇");AppendMenu(wyymenu,MF_POPUP,(intptr_t)wangpopup,"&王云");return true;}int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){char wclassname[]="wyy";WNDCLASSEX wc;wc.cbSize=sizeof(WNDCLASSEX);wc.style=CS_HREDRAW|CS_VREDRAW|CS_NOCLOSE;wc.lpfnWndProc=MainWndProc;wc.cbClsExtra=0;wc.cbWndExtra=0;wc.hInstance=hInstance;wc.hIcon=LoadIcon(hInstance,(LPSTR)IDI_MAIN);wc.hCursor=LoadCursor(NULL,IDC_ARROW);wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);wc.lpszMenuName=NULL;wc.lpszClassName=wclassname;wc.hIconSm=LoadIcon(hInstance,(LPSTR)IDI_MAIN);RegisterClassEx(&wc);CreateTopMenu();HWND hwnd=CreateWindowEx(WS_EX_LEFT,wclassname,"王云勇首富",WS_CAPTION,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,wyymenu,hInstance,NULL);if(hwnd==NULL){MessageBox(NULL,"窗口出错","error",MB_OK);return -1;}ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);MSG msg;while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){FILE *wyywy;static int i=0;HDC hdc;PAINTSTRUCT ps;static std::string str;static char *sf;static wchar_t *sfwyy;OPENFILENAME ofn;static char szFile[256];static char szFileTitle[256];memset(&ofn,0,sizeof(ofn));ofn.lStructSize=sizeof(ofn);ofn.hwndOwner=hwnd;ofn.lpstrFilter="保存的文件(*.txt)\0*.txt\0\0";ofn.nFilterIndex=1;ofn.lpstrFile=szFile;ofn.nMaxFile=sizeof(szFile);ofn.lpstrFileTitle=szFileTitle;ofn.nMaxFileTitle=sizeof(szFileTitle);ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_EXPLORER;switch(message){case WM_COMMAND:switch(LOWORD(wParam))MessageBox(hwnd,TEXT("王云勇"),TEXT("首富"),MB_OK); break;case IDM_EXIT:exit(0);break;case IDM_NEW:{str.clear();i=i+8;InvalidateRect(hwnd,NULL,1);}break;case IDM_SAVE:{if(GetSaveFileName(&ofn)){sf=ofn.lpstrFile;strcat(sf,".txt");size_t len=strlen(sf)+1;size_t con=0;sfwyy=(wchar_t*)malloc(len*sizeof(wchar_t));mbstowcs_s(&con,sfwyy,len,sf,_TRUNCATE);if((wyywy=_wfopen(sfwyy,L"wt+"))==NULL){MessageBox(hwnd,TEXT("出错"),TEXT("首富"),MB_OK); MessageBox(hwnd,TEXT("返回"),TEXT("首富"),MB_OK); break;}_wsetlocale(0,L"chs");if(fwprintf(wyywy,L"%s",str)>0){MessageBox(hwnd,TEXT("保存成功"),TEXT("首富"),MB_OK);} if(fclose(wyywy)==0){MessageBox(hwnd,TEXT("王云勇成功"),TEXT("首富"),MB_OK);} }}default:break;}break;case WM_PAINT:{hdc=BeginPaint(hwnd,&ps);if(i==0){TextOut(hdc,0,0,str.c_str(),str.length());}EndPaint(hwnd,&ps);}break;{i=0;str=str+char(wParam);InvalidateRect(hwnd,NULL,0);return 0;}case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd,message,wParam,lParam); }。

用C#语言实现记事本(代码)

用C#语言实现记事本(代码)

一记事本本章介绍如何使用Visual C# 2013设计一个Windows应用程序——记事本,通过本章的学习,可以进一步掌握MenuStrip(菜单)、ToolStrip(工具栏)、RichTextBox(高级文本框)和StatusStrip(状态栏控件)等控件的使用,以及如何使用CommonDialog(公共对话框)实现对文本的存取、格式设置等操作。

(说明:所有代码必须在英文状态下使用!)1.1 记事本简介记事本是一种常用的软件,在微软的Windows中,自带了一个记事本软件,Windows 7下的记事本软件如图1所示。

图1 Windows自带的记事本本章介绍的记事本,实现了Windows自带的记事本的部分功能外,并且还可以任意更改字体的字体类型、大小和颜色,并在状态栏中显示时间。

为了方便用户的操作,还在程序的窗体上放置了一个工具栏。

本章介绍的记事本程序具有文件的新建、打开、保存功能;文字的复制、粘贴、删除功能;字体类型、格式的设置功能;查看日期时间等功能,并且用户可以根据需要显示或者隐藏工具栏和状态栏。

接下来将详细的介绍记事本程序的设计与实现的步骤和方法。

1.2 记事本界面设计新建一个Windows窗体应用程序,并命名为“Notepad”。

本节介绍记事本程序的界面设计以及界面上各控件的属性设置。

1.打开VS2013 单击文件→新建→项目2.选择模版→Visual C# →windows→windows窗体应用程序在下面的名称写Notepad出现界面如图所示3 更改窗体名称单击窗体,→右下角属性→text 修改为“记事本”如图所示控件类型控件名称属性设置结果Form Form1 Name frmNotepadText 记事本StartPosition(起始位置)CenterScreen(中央屏幕)Size 600, 450 Anchor (抛锚,使固定)(1)界面设计新建好“Notepad”项目后,定位到记事本程序的窗体设计器窗口,然后依次在窗体上放置以下控件(各1个):(1)M enuStrip(菜单控件)。

c#记事本 源代码

c#记事本  源代码

private ponentModel.Container components = null;
public Form1()
{
InitializeComponent();
this.menuItem3.Index = 1;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
namespace NotePad
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem6.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
this.menuItem6.Text = "新建(&N)";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);

C#实现简单记事本程序

C#实现简单记事本程序

C#实现简单记事本程序本⽂实例为⼤家分享了使⽤C#写出⼀个简单的记事本程序,供⼤家参考,具体内容如下编程语⾔: C#编程环境: Visual Studio 2013运⾏环境: .NET Framework 4.5预览:功能:标题栏显⽰⽂件标题菜单栏各类菜单命令⽂件- 新建- 打开- 保存- 另存为- 页⾯设置- 打印- 退出编辑 - 撤销- 剪切- 复制- 粘贴- 全选- 时间/⽇期格式 - ⾃动换⾏- 字体视图 - 状态栏- ⼯具栏- 全屏模式帮助- 开源许可- 查看帮助- 关于⼯具栏常⽤⼯具集合标签栏⽂件标签显⽰⼯作区编辑区状态栏显⽰⽂件状态⽂本状态(新建/已修改)字符个数光标坐标功能实现本程序有两个窗体,分别为Form1和AboutBox1所有⽂件如下:对于Form1:需要引⼊的类库:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;窗体及空间声明代码:private System.Windows.Forms.MenuStrip menuStrip1;private System.Windows.Forms.ToolStripMenuItem ⽂件ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 新建ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 打开ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 保存ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 另存为ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 编辑ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 格式ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 关于ToolStripMenuItem;private System.Windows.Forms.TextBox editBox1;private System.Windows.Forms.ToolStripMenuItem 撤销ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 剪切ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 复制ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 粘贴ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 删除ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 全选AToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem ⽇期DToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 格式ToolStripMenuItem1;private System.Windows.Forms.ToolStripMenuItem 字体ToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 查看VToolStripMenuItem;private System.Windows.Forms.ToolStripMenuItem 状态栏ToolStripMenuItem;private System.Windows.Forms.StatusStrip statusStrip1;private System.Windows.Forms.SaveFileDialog saveFileDialog1;private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;private System.Windows.Forms.ToolStripSeparator toolStripMenuItem3;private System.Windows.Forms.ToolStripSeparator toolStripMenuItem4;private System.Windows.Forms.ToolStripMenuItem 页⾯设置UToolStripMenuItem;private System.Drawing.Printing.PrintDocument printDocument1;private System.Windows.Forms.PageSetupDialog pageSetupDialog1;private System.Windows.Forms.ToolStripMenuItem 打印PToolStripMenuItem;private System.Windows.Forms.PrintDialog printDialog1;private System.Windows.Forms.ToolStripMenuItem 查看帮助HToolStripMenuItem;private System.Windows.Forms.ToolStripSeparator toolStripMenuItem5;private System.Windows.Forms.ToolStripMenuItem ⾃动换⾏ToolStripMenuItem;private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;public System.Windows.Forms.Timer timer1;private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel2;private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel3;private System.Windows.Forms.ToolStripMenuItem 全屏模式ToolStripMenuItem;private System.Windows.Forms.ToolStrip toolStrip1;private System.Windows.Forms.ToolStripButton newButton;private System.Windows.Forms.ToolStripButton openButton;private System.Windows.Forms.ToolStripButton saveButton;private System.Windows.Forms.ToolStripButton saveAsButton;private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;private System.Windows.Forms.ToolStripButton cutButton;private System.Windows.Forms.ToolStripButton copyButton;private System.Windows.Forms.ToolStripButton pasteButton;private System.Windows.Forms.ToolStripButton deleteButton;private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;private System.Windows.Forms.ToolStripButton timeButton;private System.Windows.Forms.ToolStripButton fullButton;private System.Windows.Forms.ToolStripButton textButton;private System.Windows.Forms.ToolStripMenuItem ⼯具栏ToolStripMenuItem;private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel4;private System.Windows.Forms.ToolStripMenuItem 开源许可OToolStripMenuItem;private System.Windows.Forms.TabControl tabControl1;private System.Windows.Forms.TabPage tabPage1;private System.Windows.Forms.TabPage tabPage2;private System.Windows.Forms.TextBox editBox2;1窗体载⼊:private void Form1_Load(object sender, EventArgs e){this.editBox1.ScrollBars = ScrollBars.Both;this.editBox2.ScrollBars = ScrollBars.Both;this.Text = textFileName + " - " + programeName;//显⽰⽂件名this.timer1.Start();editBox1.Focus();}⾸先进⾏⼀些固定变量声明:private string textFileName = "⽆标题";private string programeName = "Icey";private string filePath = "";private string asFilePath = "";private string selecteText = "";private string helpUrl = "https:///icey";private string openSourceUrl = "https:///mayuko2012/icey";private string wrongMessage = "你好像遇到了错误...";private string fileFormat = "⽂本⽂件(*.txt)|*.txt|Icey⽂件(*.ice)|*.ice|C++⽂件(*.cpp)|*.cpp|C⽂件(*.c)|*.c|所有⽂件(*.*)|(*.*)"; private string tabFileName1 = "⽆标题1 - Icey";private string tabFileName2 = "⽆标题2 - Icey";Boolean saveFileStatus1 = false;Boolean textChanged1 = false;Boolean saveFileStatus2 = false;Boolean textChanged2 = false;private void saveFile()//保存{if (!textFileName.Equals("")){SaveFileDialog saveFile = new SaveFileDialog();saveFile.Filter = fileFormat;saveFile.FileName = "*.txt";if (saveFile.ShowDialog() == DialogResult.OK){filePath = saveFile.FileName;StreamWriter sw = new StreamWriter(filePath, false, Encoding.Default);sw.Write((editBox1.Focused) ? editBox1.Text : editBox2.Text);sw.Close();if (editBox1.Focused){tabFileName1 = saveFile.FileName + " - " + programeName;saveFileStatus1 = true;}else if (editBox2.Focused){tabFileName2 = saveFile.FileName + " - " + programeName;saveFileStatus2 = true;}}}toolStripStatusLabel4.Text = "已保存";}另存为采⽤函数形式:private void saveAsFile()//另存为{SaveFileDialog saveAsFile = new SaveFileDialog();saveAsFile.Filter = fileFormat;saveAsFile.FileName = "*.txt";if (saveAsFile.ShowDialog() == DialogResult.OK){asFilePath = saveAsFile.FileName;StreamWriter sw = new StreamWriter(asFilePath, false, Encoding.Default);sw.WriteLine((editBox1.Focused) ? editBox1.Text : editBox2.Text);sw.Close();FileInfo fileInfo = new FileInfo(saveAsFile.FileName);textFileName = ;}toolStripStatusLabel4.Text = "已保存";}新建函数:private void newFile()//新建{if (editBox1.Focused){if (editBox1.Text != String.Empty && saveFileStatus1 == false && textChanged1 == true)//如果⽂本框不为空{DialogResult result = MessageBox.Show("是否将窗⼝1已编辑⽂件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, rmation); if (result == DialogResult.Yes){saveFile();Application.Exit();}else if (result == DialogResult.No){editBox1.Text = "";}}elseeditBox1.Text = "";}else if (editBox2.Focused){if (editBox2.Text != String.Empty && saveFileStatus2 == false && textChanged2 == true)//如果⽂本框不为空{DialogResult result = MessageBox.Show("是否将窗⼝2已编辑⽂件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, rmation); if (result == DialogResult.Yes){saveFile();Application.Exit();}else if (result == DialogResult.No){editBox2.Text = "";}}elseeditBox2.Text = "";}}打开⽂件函数:private void openFile()//打开OpenFileDialog openFile = new OpenFileDialog();openFile.Filter = fileFormat;if (openFile.ShowDialog() == DialogResult.OK){StreamReader sr = new StreamReader(openFile.FileName, Encoding.Default);if (editBox1.Focused){editBox1.Text = sr.ReadToEnd();}else if(editBox2.Focused){editBox2.Text = sr.ReadToEnd();}sr.Close();FileInfo fileInfo = new FileInfo(openFile.FileName);if (editBox1.Focused){tabFileName1 = + " - " + programeName;saveFileStatus1 = true;}else if (editBox2.Focused){tabFileName2 = + " - " + programeName;saveFileStatus2 = true;}textFileName = ;}}全屏模式函数:private void fullScreen()//全屏{if (全屏模式ToolStripMenuItem.Checked == false){this.WindowState = FormWindowState.Maximized;全屏模式ToolStripMenuItem.Checked = true;this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;}else{this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;this.WindowState = FormWindowState.Normal;全屏模式ToolStripMenuItem.Checked = false;}}退出菜单命令:private void 退出ToolStripMenuItem_Click(object sender, EventArgs e){if (editBox1.Text != String.Empty || saveFileStatus1 == false && textChanged1 == true){this.tabPage1.Show();this.editBox1.Focus();DialogResult result = MessageBox.Show("是否将窗⼝1已编辑⽂件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, rmation); if (result == DialogResult.Yes){saveFile();Application.Exit();}else if (result == DialogResult.No){Application.Exit();}}else if (editBox2.Text != String.Empty || saveFileStatus2 == false && textChanged2 == true){this.tabPage2.Show();this.editBox2.Focus();DialogResult result = MessageBox.Show("是否将窗⼝2已编辑⽂件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, rmation); if (result == DialogResult.Yes){saveFile();Application.Exit();}else if (result == DialogResult.No){Application.Exit();}}elseApplication.Exit();}Bool变量,⽤于判断TextBox是否发⽣变化:private void textBox1_TextChanged(object sender, EventArgs e){textChanged2 = true;toolStripStatusLabel4.Text = "已修改";}private void editBox2_TextChanged(object sender, EventArgs e)textChanged2 = true;toolStripStatusLabel4.Text = "已修改";}新建菜单命令:private void 新建ToolStripMenuItem_Click(object sender, EventArgs e){newFile();}打开菜单命令:private void 打开ToolStripMenuItem_Click(object sender, EventArgs e){openFile();}字体菜单命令:private void 字体ToolStripMenuItem_Click(object sender, EventArgs e){FontDialog fontDialog = new FontDialog();if (fontDialog.ShowDialog() == DialogResult.OK){if (editBox1.Focused){editBox1.Font = fontDialog.Font;}elseeditBox2.Font = fontDialog.Font;}}退出动作(当⽤户点击窗体右上⾓退出按钮时执⾏此操作):private void Form1_FormClosing(object sender, FormClosingEventArgs e){if (editBox1.Text != String.Empty && e.CloseReason == erClosing || saveFileStatus1 == false && textChanged1 == true)//如果⽂本框不为空&&触发关闭按钮事件{this.tabPage1.Show();this.editBox1.Focus();DialogResult result = MessageBox.Show("是否将窗体1已编辑⽂件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, rmation);if (result == DialogResult.Yes){saveFile();e.Cancel = false;}else if (result == DialogResult.No){e.Cancel = false;}else if (result == DialogResult.Cancel){e.Cancel = true;}}else if (editBox2.Text != String.Empty && e.CloseReason == erClosing || saveFileStatus2 == false && textChanged2 == true)//如果⽂本框不为空&&触发关闭按钮事件 {this.tabPage2.Show();this.editBox2.Focus();DialogResult result = MessageBox.Show("是否将窗⼝2已编辑⽂件保存到 " + textFileName, wrongMessage, MessageBoxButtons.YesNoCancel, rmation);if (result == DialogResult.Yes){saveFile();e.Cancel = false;}else if (result == DialogResult.No){e.Cancel = false;}else if (result == DialogResult.Cancel){e.Cancel = true;}}elseApplication.Exit();}状态栏命令(状态栏是否显⽰):private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e){if (状态栏ToolStripMenuItem.Checked == true){状态栏ToolStripMenuItem.Checked = false;statusStrip1.Visible = false;}else{状态栏ToolStripMenuItem.Checked = true;statusStrip1.Visible = true;}编辑命令:private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e) {if ((editBox1.SelectedText.Equals(""))){剪切ToolStripMenuItem.Enabled = false;复制ToolStripMenuItem.Enabled = false;删除ToolStripMenuItem.Enabled = false;}else{剪切ToolStripMenuItem.Enabled = true;复制ToolStripMenuItem.Enabled = true;删除ToolStripMenuItem.Enabled = true;}}全选命令:private void 全选AToolStripMenuItem_Click(object sender, EventArgs e) {if (editBox1.Focused){this.editBox1.SelectAll();}elsethis.editBox2.SelectAll();}剪切复制粘贴删除命令:private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) {if (editBox1.Focused){selecteText = editBox1.SelectedText;this.editBox1.Cut();}else{selecteText = editBox2.SelectedText;this.editBox2.Cut();}}private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e) {if (editBox1.Focused){this.editBox1.Undo();}elsethis.editBox2.Undo();}private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) {if (editBox1.Focused){this.editBox1.Copy();}elsethis.editBox2.Copy();}private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) {if (editBox1.Focused){this.editBox1.Paste();}elsethis.editBox2.Paste();}private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {if (editBox1.Focused){this.editBox1.SelectedText = "";}elsethis.editBox2.SelectedText = "";}保存命令:private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) {saveFile();}private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e){saveAsFile();}时间戳命令:private void ⽇期DToolStripMenuItem_Click(object sender, EventArgs e){if (editBox1.Focused){editBox1.AppendText(System.DateTime.Now.ToString());}elseeditBox2.AppendText(System.DateTime.Now.ToString());}页⾯设置命令:private void 页⾯设置UToolStripMenuItem_Click(object sender, EventArgs e){pageSetupDialog1.Document = printDocument1;this.pageSetupDialog1.AllowMargins = true;this.pageSetupDialog1.AllowOrientation = true;this.pageSetupDialog1.AllowPaper = true;this.pageSetupDialog1.AllowPrinter = true;this.pageSetupDialog1.Document = this.printDocument1;pageSetupDialog1.ShowDialog();}打印命令:private void 打印PToolStripMenuItem_Click(object sender, EventArgs e){this.printDialog1.Document = this.printDocument1;this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings;if (this.printDialog1.ShowDialog() == DialogResult.OK){try{this.printDocument1.Print();}catch (Exception ex){MessageBox.Show(ex.Message, wrongMessage, MessageBoxButtons.OK, MessageBoxIcon.Error);}}}查看帮助关于命令:private void 查看帮助HToolStripMenuItem_Click(object sender, EventArgs e){System.Diagnostics.Process.Start(helpUrl);}private void 关于ToolStripMenuItem_Click(object sender, EventArgs e){AboutBox1 about = new AboutBox1();about.StartPosition = FormStartPosition.CenterScreen;about.Show();about.Owner = this;//timer1.Stop();}⾃动换⾏命令:private void ⾃动换⾏ToolStripMenuItem_Click(object sender, EventArgs e){if (⾃动换⾏ToolStripMenuItem.Checked == true){editBox1.WordWrap = false;editBox2.WordWrap = false;⾃动换⾏ToolStripMenuItem.Checked = false;}else{editBox1.WordWrap = true;editBox2.WordWrap = true;⾃动换⾏ToolStripMenuItem.Checked = true;}}计时器(100 ms刷新频率):private void timer1_Tick(object sender, EventArgs e){toolStripStatusLabel1.Text = (editBox1.Focused) ? editBox1.Text.Length.ToString() + " 个字符" : editBox2.Text.Length.ToString() + " 个字符";int totalline = (editBox1.Focused) ? editBox1.GetLineFromCharIndex(editBox1.Text.Length) + 1 : editBox2.GetLineFromCharIndex(editBox2.Text.Length) + 1;//得到总⾏数int index = (editBox1.Focused) ? editBox1.GetFirstCharIndexOfCurrentLine() : editBox2.GetFirstCharIndexOfCurrentLine();//得到当前⾏第⼀个字符的索引int line = (editBox1.Focused) ? editBox1.GetLineFromCharIndex(index) + 1 : editBox2.GetLineFromCharIndex(index) + 1;//得到当前⾏的⾏号int col = (editBox1.Focused) ? editBox1.SelectionStart - index + 1 : editBox2.SelectionStart - index + 1;//.SelectionStart得到光标所在位置的索引 - 当前⾏第⼀个字符的索引 = 光标所在的列数 toolStripStatusLabel2.Text = "第" + line + "⾏,第" + col + "列";cutButton.Enabled = false;copyButton.Enabled = false;deleteButton.Enabled = false;}else{cutButton.Enabled = true;copyButton.Enabled = true;deleteButton.Enabled = true;}if (editBox1.Focused){editBox1.Focus();this.Text = tabFileName1;}else{editBox2.Focus();this.Text = tabFileName2;}if (editBox2.Focused){editBox2.Focus();this.Text = tabFileName2;}else{editBox1.Focus();this.Text = tabFileName1;}}⼯具栏命令(⼯具栏是否显⽰):private void ⼯具栏ToolStripMenuItem_Click(object sender, EventArgs e){if (⼯具栏ToolStripMenuItem.Checked == false){toolStrip1.Visible = true;⼯具栏ToolStripMenuItem.Checked = true;}else if(⼯具栏ToolStripMenuItem.Checked == true){toolStrip1.Visible = false;⼯具栏ToolStripMenuItem.Checked = false;}}开源许可命令:private void 开源许可OToolStripMenuItem_Click(object sender, EventArgs e) {System.Diagnostics.Process.Start(openSourceUrl);}⼯具栏的各个按钮:private void newButton_Click(object sender, EventArgs e){newFile();}private void openButton_Click(object sender, EventArgs e){openFile();}private void saveButton_Click(object sender, EventArgs e){saveFile();}private void saveAsButton_Click(object sender, EventArgs e){saveAsFile();}private void cutButton_Click(object sender, EventArgs e){if (editBox1.Focused){selecteText = editBox1.SelectedText;this.editBox1.Cut();}else{selecteText = editBox2.SelectedText;this.editBox2.Cut();}}private void copyButton_Click(object sender, EventArgs e){if (editBox1.Focused)this.editBox1.Copy();}elsethis.editBox2.Copy();}private void pasteButton_Click(object sender, EventArgs e) {if (editBox1.Focused){this.editBox1.Paste();}elsethis.editBox2.Paste();}private void deleteButton_Click(object sender, EventArgs e) {if (editBox1.Focused){this.editBox1.SelectedText = "";}elsethis.editBox2.SelectedText = "";}private void timeButton_Click(object sender, EventArgs e) {if (editBox1.Focused){editBox1.AppendText(System.DateTime.Now.ToString()); }elseeditBox2.AppendText(System.DateTime.Now.ToString()); }private void textButton_Click(object sender, EventArgs e) {FontDialog fontDialog = new FontDialog();if (fontDialog.ShowDialog() == DialogResult.OK){if (editBox1.Focused){editBox1.Font = fontDialog.Font;}elseeditBox2.Font = fontDialog.Font;}}private void fullButton_Click(object sender, EventArgs e) {fullScreen();}标签栏:private void tabPage1_Click(object sender, EventArgs e){editBox1.Focus();this.Text = tabPage1.Text;if (textChanged1 == false){toolStripStatusLabel4.Text = "新建";}}private void tabPage2_Click(object sender, EventArgs e) {editBox2.Focus();this.Text = tabPage2.Text;if (textChanged2 == false){toolStripStatusLabel4.Text = "新建";}}对于AboutBox:using System;using System.Collections.Generic;using ponentModel;using System.Drawing;using System.Linq;using System.Reflection;using System.Threading.Tasks;using System.Windows.Forms;namespace Notepad{partial class AboutBox1 : Form{public AboutBox1(){this.Text = String.Format("关于 {0}", AssemblyTitle);belProductName.Text = AssemblyProduct;belVersion.Text = String.Format("版本 {0}", AssemblyVersion);belCopyright.Text = AssemblyCopyright;belCompanyName.Text = AssemblyCompany;this.textBoxDescription.Text = AssemblyDescription;}#region 程序集特性访问器public string AssemblyTitle{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);if (attributes.Length > 0){AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];if (titleAttribute.Title != ""){return titleAttribute.Title;}}return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);}}public string AssemblyVersion{get{return Assembly.GetExecutingAssembly().GetName().Version.ToString();}}public string AssemblyDescription{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length == 0){return "";}return ((AssemblyDescriptionAttribute)attributes[0]).Description;}}public string AssemblyProduct{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);if (attributes.Length == 0){return "";}return ((AssemblyProductAttribute)attributes[0]).Product;}}public string AssemblyCopyright{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (attributes.Length == 0){return "";}return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;}}public string AssemblyCompany{get{object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if (attributes.Length == 0){return "";}return ((AssemblyCompanyAttribute)attributes[0]).Company;}}#endregionprivate void AboutBox1_Load(object sender, EventArgs e){}private void okButton_Click(object sender, EventArgs e)}private void AboutBox1_FormClosing(object sender, FormClosingEventArgs e){Form1 frm1 = (Form1)this.Owner;frm1.timer1.Start();}}}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

用C编写的记事本程序

用C编写的记事本程序

最近想好好研究C,于是便拿记事本这个小程序练练手。

自己从网上找了一个挺不错的,就从这个开始学习吧。

一:代码先建立一文件夹->方便管理与查看拿我来说,在d盘根目录下新建note文件夹1.新建一note.rc资源文件,代码如下。

#include "sample.h"A ICON MOVEABLE PURE LOADONCALL DISCARDABLE "sample.ico" //LOADONCALL 调入装入MAINMENU MENU{POPUP "文件(&F)"{MENUITEM "打开(&O)...", CM_FILE_OPENMENUITEM "另存为(&S)...", CM_FILE_SAVEASMENUITEM SEPARATORMENUITEM "关闭", CM_FILE_EXIT}POPUP "帮助(&H)"{MENUITEM "关于(&A)", CM_ABOUT}}2.新建sample.h,代码如下#define CM_FILE_SAVEAS 9072#define CM_FILE_EXIT 9071#define CM_FILE_OPEN 9070#define CM_ABOUT 90693.新建源程序note.c ,代码如下#include <windows.h>#include "sample.h"static char g_szClassName[] = "MyWindowClass";static HINSTANCE g_hInst = NULL;#define IDC_MAIN_TEXT 1001BOOL LoadFile(HWND hEdit, LPSTR pszFileName){HANDLE hFile;BOOL bSuccess = FALSE;hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);if(hFile != INVALID_HANDLE_VALUE){DWORD dwFileSize;dwFileSize = GetFileSize(hFile, NULL);if(dwFileSize != 0xFFFFFFFF){LPSTR pszFileText;pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);if(pszFileText != NULL){DWORD dwRead;if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL)){pszFileText[dwFileSize] = 0;if(SetWindowText(hEdit, pszFileText))bSuccess = TRUE;}GlobalFree(pszFileText);}}CloseHandle(hFile);}return bSuccess;}BOOL SaveFile(HWND hEdit, LPSTR pszFileName){HANDLE hFile;BOOL bSuccess = FALSE;hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);if(hFile != INVALID_HANDLE_VALUE){DWORD dwTextLength;dwTextLength = GetWindowTextLength(hEdit);if(dwTextLength > 0)LPSTR pszText;pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);if(pszText != NULL){if(GetWindowText(hEdit, pszText, dwTextLength + 1)){DWORD dwWritten;if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))bSuccess = TRUE;}GlobalFree(pszText);}}CloseHandle(hFile);}return bSuccess;}BOOL DoFileOpenSave(HWND hwnd, BOOL bSave){OPENFILENAME ofn;char szFileName[MAX_PATH];ZeroMemory(&ofn, sizeof(ofn));szFileName[0] = 0;ofn.lStructSize = sizeof(ofn);ofn.hwndOwner = hwnd;ofn.lpstrFilter = "文本文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0\0";ofn.lpstrFile = szFileName;ofn.nMaxFile = MAX_PATH;ofn.lpstrDefExt = "txt";if(bSave){ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;if(GetSaveFileName(&ofn))if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName)){MessageBox(hwnd, "保存文件失败.", "错误信息",MB_OK | MB_ICONEXCLAMATION);return FALSE;}}}else{ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;if(GetOpenFileName(&ofn)){if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName)){MessageBox(hwnd, "打开文件失败.", "错误信息",MB_OK | MB_ICONEXCLAMATION);return FALSE;}}}return TRUE;}LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {switch(Message){case WM_CREATE:CreateWindow("EDIT", "",WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));break;case WM_SIZE:if(wParam != SIZE_MINIMIZED)MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam),HIWORD(lParam), TRUE);break;case WM_SETFOCUS:SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));break;case WM_COMMAND:switch(LOWORD(wParam)){case CM_FILE_OPEN:DoFileOpenSave(hwnd, FALSE);break;case CM_FILE_SAVEAS:DoFileOpenSave(hwnd, TRUE);break;case CM_FILE_EXIT:PostMessage(hwnd, WM_CLOSE, 0, 0);break;case CM_ABOUT:MessageBox (NULL, "一个Windows C 记事本程序" , "关于...", 0);}break;case WM_CLOSE:DestroyWindow(hwnd);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, Message, wParam, lParam);}return 0;}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){WNDCLASSEX WndClass;HWND hwnd;MSG Msg;g_hInst = hInstance;WndClass.cbSize = sizeof(WNDCLASSEX);WndClass.style = 0;WndClass.lpfnWndProc = WndProc;WndClass.cbClsExtra = 0;WndClass.cbWndExtra = 0;WndClass.hInstance = g_hInst;WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); WndClass.lpszMenuName = "MAINMENU";WndClass.lpszClassName = g_szClassName;WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);if(!RegisterClassEx(&WndClass)){MessageBox(0, "注册窗口失败", "错误信息",MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);return 0;}hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,"一个记事本程序",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,NULL, NULL, g_hInst, NULL);if(hwnd == NULL){MessageBox(0, "创建窗口失败", "错误信息",MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);return 0;}ShowWindow(hwnd, nCmdShow);UpdateWindow(hwnd);while(GetMessage(&Msg, NULL, 0, 0)){TranslateMessage(&Msg);DispatchMessage(&Msg);}return Msg.wParam;}二:编译,连接运行在编译之前需要一个sample.ico图标.打开运行,输入cmd,回车,进入dos命令行进入d:\note下1.用dir命令列一下当前目录包含文件。

记事本程序源代码汇总

记事本程序源代码汇总

记事本程序源代码汇总下面是一个简单的记事本程序的源代码:```#include <iostream>#include <fstream>#include <string>using namespace std;void showMencout << "**********************" << endl; cout << " 记事本程序 " << endl; cout << "**********************" << endl; cout << "请选择以下操作:" << endl;cout << "1. 新建记事本文件" << endl;cout << "2. 打开已有记事本文件" << endl; cout << "3. 查看记事本文件内容" << endl; cout << "4. 添加文本到记事本文件" << endl; cout << "5. 退出程序" << endl;cout << "**********************" << endl; void createFilstring filename;cout << "请输入新建记事本文件的文件名:";cin >> filename;//在当前目录创建一个新文件ofstream outFile(filename);outFile.close(;cout << "成功创建记事本文件:" << filename << endl; void openFilstring filename;cout << "请输入要打开的记事本文件的文件名:";cin >> filename;//尝试打开已存在的文件ifstream inFile(filename);if (!inFile)cout << "无法打开文件:" << filename << endl;} elsestring content;getline(inFile, content, '\0');cout << "记事本文件内容:" << endl;cout << content << endl;inFile.close(;}void viewFilstring filename;cout << "请输入要查看的记事本文件的文件名:"; cin >> filename;//尝试打开已存在的文件ifstream inFile(filename);if (!inFile)cout << "无法打开文件:" << filename << endl; } elsestring line;cout << "记事本文件内容:" << endl;while (getline(inFile, line))cout << line << endl;}inFile.close(;}void appendToFilstring filename;cout << "请输入要添加文本的记事本文件的文件名:";cin >> filename;//尝试打开已存在的文件ofstream outFile(filename, ios::app);if (!outFile)cout << "无法打开文件:" << filename << endl;} elsestring content;cout << "请输入要添加的文本内容(以#结束):" << endl; while (true)getline(cin, content);if (content == "#")break;}outFile << content << endl;}outFile.close(;cout << "成功添加文本到记事本文件:" << filename << endl; }int maiint choice;doshowMenu(;cout << "请输入您的选择:";cin >> choice;switch (choice)case 1:createFile(;break;case 2:openFile(;break;case 3:viewFile(;break;case 4:appendToFile(;break;case 5:cout << "感谢使用记事本程序,再见!" << endl;break;default:cout << "无效的选择,请重新输入!" << endl;}} while (choice != 5);return 0;```这个记事本程序通过命令行界面提供了以下操作:1.新建记事本文件:用户输入一个文件名后,在当前目录下创建一个新文件作为记事本。

记事本源代码(c)

记事本源代码(c)
GetConsoleScreenBufferInfo(hOut,&bInfo);
COORD coord = bInfo.dwCursorPosition; //将显存中光标信息赋予coord
*x = coord.X;
*y = coord.Y;
}
{
printf("%c",__strInput[__curStrInput]);
__curStrInput++;
}
else if(__cInput == 27) //EXC退出
{
__curStrInput = 0;
__nLastCur = 0; //字符位数标志
while(1)
{
wherexy(&x,&y); //获得当前光标位置
__cInput = getch();
if(__cInput > 47 && __cInput < 58 && __nLastCur < M && (__cInput != 48 | | __curStrInput != 0))
}
void gotoxy(int x,int y)
{
COORD coord; //windows.h中描述点的结构体
coord.X = x;
coord.Y = y;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获得标准输出设备句柄
gotoxy(x + 1,y);
}
else
{
lse if(__cInput == 13) //回车处理

记事本程序(C语言)[1]

记事本程序(C语言)[1]

#include <stdio.h>#include <string.h>#include <conio.h>#include <time.h>#include <stdlib.h>#include <windows.h>#define hh 70 //设置自动换行的位置char file_name_now[100]; //全局保存加载文件路径typedef struct char_node //结点,date存储字符数据,c存储汉字、英文识别码..汉字为1,英文为0{char date;int c;struct char_node *next; //保存下一结点地址struct char_node *up; //保存上一结点地址}node;typedef struct xy_node //x保存换行的位置{int x;struct xy_node *next;struct xy_node *up;}xy;time_t now; //实例化time_t结构struct tm *timenow;void gotoxy(int x, int y) //自定义函数,跳转光标。

{COORD pos = {x,y};HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOut, pos);}void wherexy(int *x,int *y) //自定义函数,获取光标当前位置。

{HANDLE h1;CONSOLE_SCREEN_BUFFER_INFO scrInfo;h1=GetStdHandle(STD_OUTPUT_HANDLE);GetConsoleScreenBufferInfo(h1,&scrInfo);*x=scrInfo.dwCursorPosition.X;*y=scrInfo.dwCursorPosition.Y;}char main_win() //主界面选择函数{char choose;system("if not exist C:\\我的记事本md C:\\我的记事本");system("cls");printf("\t\t\t****************************\n");printf("\t\t\t 记事本\n");printf("\t\t\t****************************\n\n");printf("\t\t\t 1.新建我的记事本\n\n");printf("\t\t\t 2.加载我的记事本\n\n");printf("\t\t\t 3.删除我的记事本\n\n");printf("\t\t\t 4.退出记事本程序\n\n");printf("\t\t\t *请输入你的选择:\n\n\t\t\t\t ");scanf("%s",&choose);return choose;}void new_file_win() //新建记事本{int flag;FILE *fp;FILE *date;char txt[5]=".txt";char input[30];char re[30];void new_file();for(flag=0;flag==0;){system("cls");printf("\t\t\t****************************\n");printf("\t\t\t 新记事本\n");printf("\t\t\t****************************\n\n");strcpy(re,"c:\\我的记事本\\");printf("\t\t\t *请输入记事本名称:\n\t\t\t ");scanf("%s",input);strcat(re,input);strcat(re,txt);if((fp=fopen(re,"rb"))!=NULL){printf("\n\n\n\t\t - - - - - - - - - - - - - - - - - - - - - - -\n\t\t 新记事本和原有记事本名称重复,请重新命名!\n\t\t\t ");fclose(fp);system("pause");flag=0;}else{system("cls");printf("\t\t\t****************************\n");printf("\t\t\t 新记事本\n");printf("\t\t\t****************************\n\n");fp=fopen(re,"ab+");printf("\t\t\t已创建记事本:%s\n\n\t\t\t ",re);flag=1;fclose(fp);system("pause");}}strcpy(file_name_now,re);new_file();}void new_file() //新记事本编辑{FILE *fp;node *h,*p,*s; //p指向当前节点的前一个节点,s指向当前节点xy *h1,*p1,*s1; //存储光标位置int x,y,x_temp=0; //光标坐标x_temp用于自动换行int c[2],hz[3]; //存储用户即时输入的字符int flag=0,flag_hz=0; //flag_hz标记获取的字符是汉字还是英文char empty='\0';int huanhang=10; //换行system("cls");time(&now);timenow = localtime(&now);printf("*打开记事本时间:%s*当前记事本:%s\t\t ENTER:换行ESC:退出、保存\n",asctime(timenow),file_name_now);printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");if((fp=fopen(file_name_now,"ab+"))==NULL){printf("不能连接%s\n",file_name_now);system("pause");return;}if((h=(node *)malloc(sizeof(node)))==NULL){printf("不能分配内存!\n");system("pause");return;}h->date='\0';h->c=0;h->next=NULL;h->up=NULL;p=h;if((h1=(xy *)malloc(sizeof(xy)))==NULL) //创建存储x轴坐标的链表{printf("不能分配内存!\n");system("pause");return;}h1->x=0;h1->next=NULL;h1->up=NULL;p1=h1;while(c[0]!=27) //监测ESC键--退出保存{c[0]=getch();if(x_temp==(hh-1) || x_temp>=hh) //这里是汉字换行的关键,防止汉字的两个字节分别存储在两行上{if(!(x_temp==(hh-1) && flag_hz==1)){if((s=(node *)malloc(sizeof(node)))==NULL){printf("不能分配内存!\n");system("pause");return;}p->next=s;s->date=10;s->c=0;s->next=NULL;s->up=p;p=s;printf("%c",s->date);if((s1=(xy *)malloc(sizeof(xy)))==NULL){printf("不能分配内存!\n");system("pause");return;}p1->next=s1;s1->x=x_temp;s1->next=NULL;s1->up=p1;p1=s1;x_temp=0;}}if(c[0]==13) //监测回车--把回车改为换行,并保存{c[0]=10;wherexy(&x,&y);if((s=(node *)malloc(sizeof(node)))==NULL){printf("不能分配内存!\n");system("pause");return;}p->next=s;s->date=c[0];s->c=0;s->next=NULL;s->up=p;p=s;printf("%c",10);x_temp=0;if((s1=(xy *)malloc(sizeof(xy)))==NULL){printf("不能分配内存!\n");system("pause");return;}p1->next=s1;s1->x=x; //记录回车时的位置s1->next=NULL;s1->up=p1;p1=s1;}else if(c[0]==0xE0) //监测方向键等扩展键,可插入字符(可扩展很多功能,待实现。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
case 4: i=0;
if((fp=fopen("tongxunlu.txt","a+"))==NULL)
{ printf(" 无法打开文件~~~\n");
}
while(!feof(fp))
{ i++;
p2=p1;
p1=(link *)malloc(LEN);
printf("Thank your use\n");
else
if(identify=='n'||identify=='N')
exit(0);
else
printf(" 请输入(Y/N):");
{ printf(" 无法打开文件~~~\n");
exit(0);
} //打开文件,并将文件中内容复制到链表中,若文件中无记录,则链表头指针为空
char identify;
// char fullname[10];
link *head,*p,*p1,*p2,*p3;
int choose;
char absorb;
int i;
head=NULL;
welcome();
head=NULL;
p2->next=NULL;
fclose(fp);
if(head==NULL)
p1=p2=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
head=p1;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct linker)
typedef struct linker
{ char xuehao[21];
char name[20];
fclose(fp);
search(head);
break;
case 3: i=0;
}
while(!feof(fp))
{ i++;
p2=p1;
p1=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
p2->next=p1;
}
if(i==1)
void search(link *); //查找记录,支持模糊查找
link *delet(link *); //删除一条记录
link *SortNum(link *head);
void saves(link *); //将记录保存到文件中
else
change(head);
saves(head);
break;
scanf("%c",&absorb);
switch(choose)
{ case 1: head=creat(); //创建链表,并将链表中的信息输入到文件中
saves(head);
if((fp=fopen("tongxunlu.txt","a+"))==NULL)
{ printf(" 无法打开文件~~~\n");
fread(p1,LEN,1,fp);
head=p1; } ຫໍສະໝຸດ while(!feof(fp))
do //确定输入是否为y或Y或n或N
{
scanf("%c",&identify);
scanf("%c",&absorb);
if(identify=='y'||identify=='Y')
}while(identify!='y'&&identify!='Y'&&identify!='n'&&identify!='N');
while(1)
{
menu();
scanf("%d",&choose);
char banji[21];
char tel[12];
char addr[11];
struct linker *next;
}link;
void main()
{ FILE *fp;
void welcome(); //欢迎界面
exit(0);
}
if(!feof(fp))
{ i++;
p2=p1;
fread(p1,LEN,1,fp);
p2->next=p1;
}
if(i==1)
exit(0);
}
if(!feof(fp))
{ i++;
p2=p1;
void menu(); //菜单界面
link *creat(); //创建一个链表
link *print(link *); //输出全部记录
link *insert(link *); //添加一条新记录
link *change(link *); //改变记录中的信息
{ i++;
p2=p1;
p1=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
if(!feof(fp))
{ i++;
p2=p1;
p1=p2=(link *)malloc(LEN);
{
printf("\n");
printf(" 通讯录中无记录,请返回~~~");
}
head=NULL;
p2->next=NULL;
fclose(fp);
break;
case 2: i=0;
if((fp=fopen("tongxunlu.txt","a+"))==NULL)
p2->next=p1;
}
if(i==1)
head=NULL;
p2->next=NULL;
p1=p2=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
head=p1;
相关文档
最新文档