⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 edit.cpp

📁 一个记事本编辑器,自己写的,希望对大家有用,十分感谢
💻 CPP
字号:
// Edit.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "f://MyVcFunction.h"    //定义常用函数
#include "RichEdit.h"        //定义RichEdit控件

#include "ComCtrEx.h"        //定义常用控件

#include "stdlib.h"  
#include "FileNotify.h"  
//#include <afxwin.h>
//#include "Menu.h" 
#define MAX_LOADSTRING 100

HWND hWnd;
//HANDLE hRichFile;//extern 参数 of RichEdit CALLBACK _EditStreamCallback
//HINSTANCE hRich;
//HWND hRichEdit;
HWND hToolbar;
HWND hStatusBar;
HMENU hMenu;
HMENU hSubMenu;
int array[4]={120,600,700,-1};
// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text
TCHAR _FileName[256]={0};
char szLineText[1024]={0};
char szToolText[80]={"nihao"};
	MSG msg;

int line; 
// 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);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.

	HACCEL hAccelTable;
	hRich=LoadRichLib();
	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_EDIT, szWindowClass, MAX_LOADSTRING);
	
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{ShowLastError("");
		return FALSE;
	}

    hMenu=LoadMenu(hInstance,(LPCSTR)IDM_MAIN);
	SetMenu(hWnd,hMenu);
	hSubMenu=GetSubMenu(hMenu,1);
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_EDIT);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
 FreeRichLib(hRich);
	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_EDIT);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;//(LPCSTR)IDM_MAIN);
	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,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }


//SendMessage(hStatusBar,SB_SETPARTS,4,LPARAM(array));
  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
    RECT Rect;
    RECT ToolbarRect;
    RECT StatuBarRect;
	int msgbox;
//	LPPOINT stPos;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	switch (message) 
	{
		    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_OPEN:
                    ZeroMemory(_FileName,sizeof _FileName);
					if(_OpenFile(hWnd,_FileName)==0){break;}
                    hRichFile=CreateFile(_FileName,GENERIC_READ,0,0,OPEN_EXISTING,
		                             FILE_ATTRIBUTE_NORMAL,0);
                    if(hRichFile==INVALID_HANDLE_VALUE){MessageBox(NULL,_FileName,"ERROR OPEN FILE",MB_OK);
				    break;}else{
					 
				    Proc_OpenFile(hRichEdit);
				    CloseHandle( hRichFile);}

                   break;
			
				case IDM_SAVE:


                    hRichFile=CreateFile(_FileName,GENERIC_WRITE,0,0,OPEN_EXISTING,
		                             FILE_ATTRIBUTE_NORMAL,0);
                    if(hRichFile==INVALID_HANDLE_VALUE){MessageBox(NULL,_FileName,"ERROR OPEN FILE",MB_OK);
				    break;}else{
					 
				    Proc_SaveFile(hRichEdit);
				    CloseHandle( hRichFile);}

                   break;
				case IDM_SAVEAS:
					ZeroMemory(_FileName,sizeof _FileName);
                    if(_SaveFile(hWnd,_FileName)==0){break;}

                    hRichFile=CreateFile(_FileName,GENERIC_WRITE,0,0,CREATE_NEW,
		                             FILE_ATTRIBUTE_NORMAL,0);
                    if(hRichFile==INVALID_HANDLE_VALUE){MessageBox(NULL,_FileName,"ERROR OPEN FILE",MB_OK);
				    break;}else{
					 
				    Proc_SaveFile(hRichEdit);
				    CloseHandle( hRichFile);}

                  break;
//				case TBARIDM_NEW:
//				    FillMemory(szLineText,sizeof szLineText,0);
//					GetLineText(hRichEdit,3,szLineText);
//				    SendMessage(hStatusBar,SB_SETTEXT,1,LPARAM("nihao"));
//				break;


				case IDM_EXIT:
					                 msgbox=MessageBox(hWnd,"需要保存文件吗","你确定不保存就退出吗",MB_OKCANCEL);
				 if(msgbox==IDOK){hRichFile=CreateFile(_FileName,GENERIC_WRITE,0,0,OPEN_EXISTING,
		                             FILE_ATTRIBUTE_NORMAL,0);
                    if(hRichFile==INVALID_HANDLE_VALUE){MessageBox(NULL,_FileName,"ERROR OPEN FILE",MB_OK);
				    break;}else{
					 
				    Proc_SaveFile(hRichEdit);
				    CloseHandle( hRichFile);}}
				   DestroyMenu(hMenu);
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
				}
			break;

           case WM_NOTIFY:
	
				if(wParam==(WPARAM)ID_RICHEDIT && ((LPNMHDR)lParam)->code==EN_MSGFILTER )
				{   if(((MSGFILTER *)lParam)->msg==WM_RBUTTONDOWN){
					TrackPopupMenu(hSubMenu,TPM_LEFTALIGN,msg.pt.x ,msg.pt.y,NULL,hWnd,NULL);
					 //GetCursorPos(stPos);
					  //TrackPopupMenu(hSubMenu,TPM_LEFTALIGN,(LPPOINT* stPos).x ,(LPPOINT* stPos).y,NULL,hWnd,NULL);
				     }
				}
			if(wParam==(WPARAM)ID_RICHEDIT && ((LPNMHDR)lParam)->code==EN_SELCHANGE ){


				line=SendMessage(hRichEdit,EM_LINEFROMCHAR,-1,0);
	        SendMessage(hStatusBar,SB_SETTEXT,0,(LPARAM)IntToChar(line+1));
			GetLineText(hRichEdit,line,szLineText);
            SendMessage(hStatusBar,SB_SETTEXT,1,(LPARAM)szLineText);
			}
				
				 break;

		
			case WM_CREATE:
				 hRichEdit=InitRichEdit(hWnd,hInst,ID_RICHEDIT);

 //                hToolbar=InitToolBar(hWnd,ID_TOOLBAR);
                  //if(SetWindowText(hRichEdit,"kkkk")==0){ShowLastError();}
                 hStatusBar=CreateWindowEx(WS_OVERLAPPED,"msctls_statusbar32",NULL,WS_CHILD|WS_VISIBLE |WS_DLGFRAME|SBS_SIZEGRIP ,
			     0,0,0,0,hWnd,(HMENU)ID_STATUSBAR,hInst,NULL);
                 SendMessage(hStatusBar,SB_SETPARTS,4,LPARAM(array));
                 break;

			case WM_SIZE:
                 GetClientRect(hWnd,&Rect);
                 GetClientRect(hStatusBar,&StatuBarRect);

                 MoveWindow(hRichEdit,0,0,Rect.right ,Rect.bottom-25  ,TRUE);
                 MoveWindow(hStatusBar,0,Rect.bottom,Rect.right,Rect.bottom-10,TRUE);
                 SendMessage(hStatusBar,SB_SETPARTS,4,LPARAM(array));
			
				
                 break;


			case WM_DESTROY:

			     PostQuitMessage(0);
			     break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -