记事本代码

发布时间:2018-01-04 20:36:46   来源:文档文库   
字号:

/*C语言Windows程序设计 -> 简易文本编辑器 -> 演示*/

#include

#include

#include "resource.h"

/*各控件所使用的ID*/

#define ID_EDITBOX 1 //文本编辑框控件

#define ID_TXTPATH 2 //路径编辑框控件

LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); //关于简易记事本弹窗的关闭函数声明。

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ) ;

int CreateChildWindow(HWND, HWND *, LPARAM ) ; //创建将使用到的子窗口控件

int SavaInputContent( TCHAR *, TCHAR * ) ;

TCHAR *openbook(TCHAR *path); //保存输入的文字到文件

int WINAPI WinMain( HINSTANCE hInstance, //第一个参数hInstance表示该程序当前运行的实例的句柄

HINSTANCE hPrevInstance, //第二个参数hPrevInstance表示当前实例的前一个实例的句柄。

PSTR szCmdLine, int iCmdShow ) //第三个参数lpCmdLine是一个以空终止的字符串,指定传递给应用程序的命令行参数?

{

static TCHAR szAppName[] = TEXT( "demo" ) ;

HWND hwnd ;

MSG msg ;

WNDCLASS wndclass ;

wndclass.lpfnWndProc = WndProc ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;

wndclass.hInstance = hInstance ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = 0 ;

wndclass.hbrBackground = CreateSolidBrush(RGB(236, 233, 216)) ;

wndclass.hCursor = LoadCursor( NULL, IDC_ARROW ) ;

wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION ) ;

wndclass.lpszClassName = szAppName ;

wndclass.lpszMenuName = (LPCSTR)IDC_MY;

if( !RegisterClass(&wndclass) ){

MessageBox( NULL, TEXT("无法注册窗口类!"), TEXT("错误"), MB_OK | MB_ICONERROR ) ;

return 0 ;

}

hwnd = CreateWindow( szAppName, TEXT("记事本"), WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0,

CW_USEDEFAULT, 0,

NULL, NULL, hInstance, NULL ) ;

ShowWindow( hwnd, iCmdShow ) ; //窗口显示函数,iCmdShow参数代表了窗体的显示状态。

UpdateWindow( hwnd ) ; //窗口更函数。

while( GetMessage(&msg, NULL, 0, 0) ){

TranslateMessage( &msg ) ;

DispatchMessage( &msg ) ;

}

return msg.wParam ;

}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )

{

int wmId, wmEvent;

static HWND hwndChild[5] ;

HDC hdc ;

PAINTSTRUCT ps ;

RECT rect ;

static TCHAR *szBuffer ; //缓冲区

static TCHAR szPath[256] ; //文本路径

static TCHAR szLineNum[32] ;

static TCHAR szCharNum[32] ;

static int iLength ;

int iLineCount, iCharCount ;

switch( message ){

case WM_CREATE:

CreateChildWindow( hwnd, hwndChild, lParam ) ;

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 ) ; //调整文本路径输入框

return 0 ;

case WM_PAINT:

GetClientRect(hwnd, &rect) ;

hdc = BeginPaint( hwnd, &ps ) ;

TextOut( hdc, 20, rect.bottom-30, TEXT("输入名称:"), lstrlen(TEXT("输入名称:")) ) ;

TextOut( hdc, 800, rect.bottom-30, szLineNum, lstrlen(szLineNum) ) ;

EndPaint( hwnd, &ps ) ;

return 0 ;

case WM_COMMAND:

wmId =LOWORD(wParam);

wmEvent =HIWORD(wParam);

switch(wmId){

case IDM_EXIT:

DestroyWindow(hwnd);

case IDM_SAVE:

iLength = GetWindowTextLength(hwndChild[ID_EDITBOX]) ;

szBuffer = (char *) malloc(sizeof(char) * GetWindowTextLength(hwndChild[ID_EDITBOX]) + 1);

GetWindowText( hwndChild[ID_EDITBOX], szBuffer, GetWindowTextLength(hwndChild[ID_EDITBOX]) + 1 ) ;

if(GetWindowText( hwndChild[ID_TXTPATH], szPath, 256 ) < 1){

MessageBox(NULL, TEXT("路径不能为空"), TEXT("提示"), MB_OK | MB_ICONINFORMATION) ;

return -1 ;

}

SavaInputContent( szPath, szBuffer ) ;

return 0 ;

case IDM_NEW:

SetWindowText( hwndChild[ID_EDITBOX], TEXT("") ) ;

SetWindowText( hwndChild[ID_TXTPATH], TEXT("") ) ;

return 0;

case IDM_OPEN:

GetWindowText( hwndChild[ID_TXTPATH], szPath, 256 );

SetWindowText( hwndChild[ID_EDITBOX], openbook(szPath) ) ;

return 0;

case IDM_ABOUT:

DialogBox(NULL, (LPCTSTR)IDD_ABOUTBOX, hwnd, (DLGPROC)About);

break;

}

switch(LOWORD(wParam)){

case ID_EDITBOX:

switch(HIWORD(wParam)){

case EN_UPDATE:

iLineCount = SendMessage( hwndChild[ID_EDITBOX], EM_GETLINECOUNT, 0, 0 ) ;

iCharCount = GetWindowTextLength( hwndChild[ID_EDITBOX] ) ;

wsprintf(szLineNum, "行数: %i 字符数量: %i", iLineCount, iCharCount) ;

InvalidateRect(hwnd, NULL, FALSE) ;

break ;

default:

break ;

}

return 0 ;

default:

break ;

}

return 0 ;

case WM_DESTROY:

PostQuitMessage(0) ;

return 0 ;

}

return DefWindowProc( hwnd, message, wParam, lParam ) ;

}

int CreateChildWindow(HWND hwnd, HWND *hwndChild, LPARAM lParam){

HINSTANCE hInst = ((LPCREATESTRUCT) lParam) -> hInstance ;

//创建编辑区

hwndChild[ID_EDITBOX] = CreateWindow( TEXT("edit"), NULL,

WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |

ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,

CW_USEDEFAULT , 0, CW_USEDEFAULT, 0,

hwnd, (HMENU)ID_EDITBOX, hInst, NULL ) ;

//路径输入框

hwndChild[ID_TXTPATH] = CreateWindow( TEXT("edit"), NULL,

WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,

0, 0, 0, 0,

hwnd, (HMENU)ID_TXTPATH, hInst, NULL ) ;

return 0 ;

}

int SavaInputContent( TCHAR *path, TCHAR *content )

{

FILE *fSvae ;

strcat(path,".txt");

fSvae = fopen( path, "wb" ) ;

if(fSvae == NULL)

{

MessageBox(NULL, TEXT("文件创建失败!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION) ;

return -1 ;

}

else{

fwrite(content, lstrlen(content), sizeof(char), fSvae);

MessageBox(NULL, TEXT("保存成功!"), TEXT("成功"), MB_OK | MB_ICONINFORMATION) ;

}

fclose(fSvae) ;

return 0 ;

}

TCHAR *openbook(TCHAR *path){

FILE *Fopen;

TCHAR *ch;

strcat(path,".txt");

Fopen = fopen( path, "rb" ) ;

if(Fopen==NULL)

{

MessageBox(NULL, TEXT("没找到你要的文件!"), TEXT("提示"), MB_OK | MB_ICONINFORMATION) ;

return 0;

}

else

{

fseek(Fopen,0, SEEK_END);

int fileLen = ftell(Fopen);

ch = (char *) malloc(sizeof(char) * fileLen);

fseek(Fopen, 0, SEEK_SET);

fread(ch,lstrlen(ch),sizeof(char),Fopen) ;

fclose(Fopen);

ch[fileLen]='\0';

return ch;

}

}

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case WM_INITDIALOG:

return TRUE;

case WM_COMMAND:

if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)

{

EndDialog(hDlg, LOWORD(wParam));

return TRUE;

}

break;

}

return FALSE;

}

本文来源:https://www.2haoxitong.net/k/doc/0857a7187ed5360cba1aa8114431b90d6d85890e.html

《记事本代码.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式