C语言的记事本程序

C语言的记事本程序,第1张

下面是扰世c的记事本小程序,for windows:

加入到工程中,再加上rc资源菜单缓迟肢,用dev c++编译即可实现:

1、C程序:

#include <windows.h>

#define CM_FILE_SAVEAS9072

#define CM_FILE_EXIT9071

#define CM_FILE_OPEN9070

#define CM_ABOUT9069

static char g_szClassName[] = "MyWindowClass"

static HINSTANCE g_hInst = NULL

#define IDC_MAIN_TEXT 1001

BOOL 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// Null terminator

if(SetWindowText(hEdit, pszFileText))

bSuccess = TRUE// It worked!

}

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)/旦李/ No need to bother if there's no text.

{

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 = "Text Files (*.txt)\0*.txt\0All Files (*.*)\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, "Save file failed.", "Error",

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, "Load of file failed.", "Error",

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, "File Editor for Windows !\n Using the Win32 API" , "About...", 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, "Window Registration Failed!", "Error!",

MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL)

return 0

}

hwnd = CreateWindowEx(

WS_EX_CLIENTEDGE,

g_szClassName,

"File Editor Example Program",

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,

NULL, NULL, g_hInst, NULL)

if(hwnd == NULL)

{

MessageBox(0, "Window Creation Failed!", "Error!",

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

}

2、RC资源菜单程序:

#define CM_FILE_SAVEAS9072

#define CM_FILE_EXIT9071

#define CM_FILE_OPEN9070

#define CM_ABOUT9069

MAINMENU MENU

{

POPUP "&File"

{

MENUITEM "&Open...", CM_FILE_OPEN

MENUITEM "Save &As...", CM_FILE_SAVEAS

MENUITEM SEPARATOR

MENUITEM "E&xit", CM_FILE_EXIT

}

POPUP "&Help"

{

MENUITEM "&About", CM_ABOUT

}

}

已经在dev c++调试通过

全实现,程序太长,发不上去,先实现基本的 图形用户界面

import java.awt.*

import java.awt.event.*

public class TestMenu {

public static void main (String[] args) {

new MenuFrame("新建"+" "+"文本文档"+".txt"+" "+"-"+" "+"记事本").launchFrame()

}

}

class MenuFrame extends Frame {

MenuBar mb = null

MenuFrame (String s) {

super (s)

}

public void launchFrame() {

Menu file = new Menu ("文件")

Menu edit = new Menu ("编辑")

Menu format = new Menu ("格式")

Menu help = new Menu ("帮助")

MenuItem newItem = new MenuItem ("新建")

newItem.addActionListener (new ActionListener () {

public void actionPerformed(ActionEvent e) {

final Frame ff = new Frame ("记事本")

ff.setMenuBar(mb)

ff.setBounds (300,300,400,200)

ff.setVisible (true)

ff. addWindowListener (new WindowAdapter () {

public void windowClosing(WindowEvent e) {

ff.setVisible (false)

}

} )

}

})

MenuItem saveItem = new MenuItem ("保存罩行")

MenuItem exitItem = new MenuItem ("退出")

MenuItem helpTitle = new MenuItem ("帮助主题")

MenuItem line = new MenuItem ("-")

MenuItem about = new MenuItem ("关于记戚稿事本")

MenuItem copy = new MenuItem ("粘贴")

MenuItem serach = new MenuItem ("查找高闷孝")

edit.add (copy)

edit.add (serach)

help.add (helpTitle)

help.add (line)

help.add (about)

exitItem.addActionListener (new ActionListener () {

public void actionPerformed(ActionEvent e) {

System.exit (0)

}

} )

file.add (newItem)

file.add (saveItem)

file.add (exitItem)

mb = new MenuBar()

mb.add (file)

mb.add (edit)

mb.add (format)

mb.add (help)

addWindowListener (new WindowAdapter () {

public void windowClosing(WindowEvent e) {

System.exit (0)

}

} )

setLayout (new FlowLayout())

setMenuBar (mb)

setBounds (300,300,400,200)

setVisible (true)

}

}

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Linq

using System.Text

using System.Windows.Forms

using System.IO

namespace Mickey记事本

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent()

}

// 用于存储当前 *** 作的文件的名称

private string textFileName = ""

private string filePath = ""大消

private void 新建_Click(object sender, EventArgs e)

{

textFileName = ""

// 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存”

if (inputInfo.Text != string.Empty)

{

// 若用户选择“是”,应d出保存文件的对话框

if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)

== DialogResult.Yes)

{

// 保存文件

Save()

Text = "新建蔽昌-Mickey记事本"

inputInfo.Text = ""

}

else if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)

== DialogResult.No)

{

// 用户选择不保宏仿扒存时将输入框中的内容清除

inputInfo.Text = ""

}

}

}

private void 打开_Click(object sender, EventArgs e)

{

OpenFileDialog openFile = new OpenFileDialog()

openFile.Filter = "文本文件(*.txt)|*.txt"

if (openFile.ShowDialog() == DialogResult.OK)

{

StreamReader sr = new StreamReader(openFile.FileName)

inputInfo.Text = sr.ReadToEnd()

sr.Close()

FileInfo fileInfo = new FileInfo(openFile.FileName)

// 把标题改为打开的文件的名称

Text = "*" + fileInfo.Name + "-Mickey记事本"

textFileName = fileInfo.Name

}

}

private void 保存_Click(object sender, EventArgs e)

{

Save()

}

// “保存”

private void Save()

{

if (!textFileName.Equals(""))

{

SaveFileDialog saveFile = new SaveFileDialog()

// 默认保存格式

saveFile.Filter = "文本文件(*.txt)|*.txt"

StreamWriter sw = new StreamWriter(filePath, false)

sw.Write(inputInfo.Text)

sw.Close()

MessageBox.Show("文件保存成功!", "Mickey温馨提示")

filePath = saveFile.FileName

// 把标题改为打开的文件的名称

Text = textFileName + "-Mickey记事本"

}

else

{

// 成员变量为“”,说明文件第一次保存,执行“另存为” *** 作

HoldFile()

}

}

private void HoldFile()

{

// 若用户选择另保存文件

SaveFileDialog saveFile = new SaveFileDialog()

// 默认保存格式

saveFile.Filter = "文本文件(*.txt)|*.txt"

if (saveFile.ShowDialog() == DialogResult.OK)

{

StreamWriter sw = new StreamWriter(saveFile.FileName, false)

sw.WriteLine(inputInfo.Text)

sw.Close()

MessageBox.Show("文件保存成功!", "Mickey温馨提示")

filePath = saveFile.FileName

}

// 判断是第一次保存还是第二次

if (textFileName.Equals(""))

{

FileInfo fileInfo = new FileInfo(saveFile.FileName)

Text = fileInfo.Name + "-Mickey记事本"

textFileName = fileInfo.Name

}

else

{

// 把标题改为打开的文件的名称

Text = textFileName + "-Mickey记事本"

}

}

private void 另存为_Click(object sender, EventArgs e)

{

HoldFile()

}

private void 页面设置_Click(object sender, EventArgs e)

{

this.pageSetupDialog1.Document = this.printDocument1

pageSetupDialog1.ShowDialog()

}

private void 打印_Click(object sender, EventArgs e)

{

if (inputInfo.Text.Length <1)

{

MessageBox.Show("请确保要打印的文件的内容不为空!", "Mickey温馨提示")

return

}

else

{

// 设置Document的属性

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, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error)

}

}

}

}

private void 退出_Click(object sender, EventArgs e)

{

// 退出时应提示用户是否保存当前文本文件

DialogResult result = MessageBox.Show("是否将更改保存?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)

if (result == DialogResult.Yes)

{

Save()

Application.Exit()

}

else if (result == DialogResult.No)

{

Application.Exit()

}

}

// 这个成员变量用来存储用户选择进行 *** 作的字符串

private string selectedInfo = ""

private void 编辑_Click(object sender, EventArgs e)

{

if ((inputInfo.SelectedText.Equals("")) &&(selectedInfo.Equals("")))

{

剪切.Enabled = false

复制.Enabled = false

粘贴.Enabled = false

删除.Enabled = false

}

else

{

剪切.Enabled = true

复制.Enabled = true

粘贴.Enabled = true

删除.Enabled = true

}

}

private void 撤销_Click(object sender, EventArgs e)

{

this.inputInfo.Undo()

}

private void 剪切_Click(object sender, EventArgs e)

{

selectedInfo = inputInfo.SelectedText

this.inputInfo.Cut()

}

private void 复制_Click(object sender, EventArgs e)

{

this.inputInfo.Copy()

}

private void 粘贴_Click(object sender, EventArgs e)

{

this.inputInfo.Paste()

}

private void 删除_Click(object sender, EventArgs e)

{

this.inputInfo.SelectedText = ""

}

private void 查找_Click(object sender, EventArgs e)

{

if (inputInfo.Text == string.Empty)

{

MessageBox.Show("请确保要查找的文件的内容不为空!", "Mickey温馨提示")

}

else

{

//Form2 fr2 = new Form2()

//fr2.sender(this)

//fr2.Show()

}

}

private void 查找下一个_Click(object sender, EventArgs e)

{

}

private void 全选_Click(object sender, EventArgs e)

{

this.inputInfo.SelectAll()

//全选_Click(sender,e)

}

private void 时间日期_Click(object sender, EventArgs e)

{

inputInfo.Text += "现在时间是:" + DateTime.Now.ToString()

}

private void 自动换行_Click(object sender, EventArgs e)

{

if (自动换行.Checked == true)

{

inputInfo.WordWrap = true

}

else

{

inputInfo.WordWrap = false

}

}

private void 字体_Click(object sender, EventArgs e)

{

FontDialog fontDialog = new FontDialog()

if (fontDialog.ShowDialog() == DialogResult.OK)

{

inputInfo.Font = fontDialog.Font

}

}

private void 查看_Click(object sender, EventArgs e)

{

if (inputInfo.Text.Length >0)

{

状态栏.Enabled = true

}

else

{

状态栏.Enabled = false

}

}

private void 状态栏_Click(object sender, EventArgs e)

{

if (状态栏.Checked == true)

{

状态栏.Checked = false

statusStrip1.Visible = false

}

else

{

状态栏.Checked = true

statusStrip1.Visible = true

}

}

private void 查看帮助_Click(object sender, EventArgs e)

{

string help = @"C:\Users\狗狗Mickey\Desktop\help.txt"

Help.ShowHelp(this, help)

}

private void 关于记事本_Click(object sender, EventArgs e)

{

AboutBox1 about = new AboutBox1()

about.Show()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8274330.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存