📄 myeditor.cpp
字号:
// myeditor.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "edit.h"
#include "myeditor.h"
#define MAX_LOADSTRING 100
// Global Variables:
edit e; //类变量声明
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Warning(HWND, UINT, WPARAM, LPARAM);
HWND hWnd;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MYEDITOR, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MYEDITOR);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(hWnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MYEDITOR);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MYEDITOR;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW| WM_HSCROLL| WM_VSCROLL,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
e.gethwnd(hWnd); //把窗口句柄传到类
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
static HWND hWndEdit;
RECT rect;
//判断文件是否被修改的变量
static BOOL fChange=FALSE;
//文件操作所用变量
static TCHAR szFileName[MAX_PATH], szTitleName[MAX_PATH] ;
static OPENFILENAME ofn ;
//字体操作所用变量
static HFONT hSetFont;
static CHOOSEFONT cf; //对非模态对话框必须定义为static
//搜索所用变量
static FINDREPLACE fr; // 对非模态对话框必须定义为static
LPFINDREPLACE pfr;
static int iOffset ;
static UINT messageFindReplace ;
static HWND hDlgModeless;
//注册查找的窗口消息
messageFindReplace= RegisterWindowMessage(FINDMSGSTRING);
//处理窗口消息
switch (message)
{
case WM_CREATE: //初始化通用对话框
//获得窗口大小
GetClientRect(hWnd,&rect);
//建立编辑框
hWndEdit=CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
WS_BORDER | ES_MULTILINE |
ES_NOHIDESEL | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, rect.right-rect.left, rect.bottom-rect.top,
hWnd, (HMENU)IDC_EDIT, hInst, NULL) ;
e.FontInitialize(hWndEdit); //字体初始化
e.gethwndedit(hWndEdit); //把编辑框句柄传到类
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
//处理菜单消息
switch (wmId)
{
case IDM_ABOUT: // 处理编辑菜单
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_NEW: // 处理新建菜单
if(fChange==TRUE) //判断文件是否被修改
{
//如果被修改了,发送是否保存当前文件消息并处理
switch(MessageBox(hWnd,TEXT ("是否保存当前文件?"),"警告",MB_YESNOCANCEL))
{
case IDYES: //保存文件并新建
if (ofn.lpstrFileTitle == NULL)
{
e.FileInitialize(&ofn);
e.FileSaveDlg(&ofn ,szFileName ,szTitleName);
}
e.FileWrite(ofn.lpstrFile);
SetWindowText(hWnd,"");
SetWindowText(hWndEdit,"");
InvalidateRect(hWndEdit,NULL,TRUE);
fChange=FALSE;
break;
case IDNO: //不保存,新建
SetWindowText(hWnd,"");
SetWindowText(hWndEdit,"");
InvalidateRect(hWndEdit,NULL,TRUE);
fChange=FALSE;
break;
case IDCANCEL: //取消操作
fChange=TRUE;
break;
}
}
else //新建文件
{
SetWindowText(hWnd,"");
SetWindowText(hWndEdit,"");
InvalidateRect(hWndEdit,NULL,TRUE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -