📄 neatpad.c
字号:
//
// Neatpad - Simple Text Editor application
//
// www.catch22.net
// Written by J Brown 2004
//
// Freeware
//
#define _CRT_SECURE_NO_DEPRECATE
#define _WIN32_WINNT 0x501
#define STRICT
#include <windows.h>
#include <tchar.h>
#include <commctrl.h>
#include <uxtheme.h>
#include "Neatpad.h"
#include "..\TextView\TextView.h"
#include "resource.h"
#if !defined(UNICODE)
#error "Please build as Unicode only!"
#endif
#pragma comment(lib, "uxtheme.lib")
TCHAR g_szAppName[] = APP_TITLE;
HWND g_hwndMain;
HWND g_hwndTextView;
HWND g_hwndStatusbar;
HWND g_hwndSearchDlg;
HFONT g_hFont;
TCHAR g_szFileName[MAX_PATH];
TCHAR g_szFileTitle[MAX_PATH];
BOOL g_fFileChanged = FALSE;
TCHAR *g_szEditMode[] = { _T("READ"), _T("INS"), _T("OVR") };
// support 'satellite' resource modules
HINSTANCE g_hResourceModule;
#pragma comment(linker, "/OPT:NOWIN98")
//
// Set the main window filename
//
void SetWindowFileName(HWND hwnd, TCHAR *szFileName, BOOL fModified)
{
TCHAR ach[MAX_PATH + sizeof(g_szAppName) + 4];
TCHAR mod[4] = _T("");
if(fModified)
lstrcpy(mod, _T(" *"));
wsprintf(ach, _T("%s - %s%s"), szFileName, g_szAppName, mod);
SetWindowText(hwnd, ach);
}
//
// About dialog-proc
//
LRESULT CALLBACK AboutDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HICON hIcon;
HFONT hFont;
RECT rect;
HWND hwndUrl;
HWND hwndStatic;
switch(msg)
{
case WM_INITDIALOG:
SendMessage(hwnd, WM_SETFONT, (WPARAM)g_hFont, 0);
CenterWindow(hwnd);
//
// Set the dialog-icon
//
hIcon = (HICON)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 48, 48, 0);
SendDlgItemMessage(hwnd, IDC_HEADER2, STM_SETIMAGE, IMAGE_ICON, (WPARAM)hIcon);
//
// Get the current font for the dialog and create a BOLD version,
// set this as the AppName static-label's font
//
hFont = CreateBoldFontFromHwnd(hwnd);
SendDlgItemMessage(hwnd, IDC_ABOUT_APPNAME, WM_SETFONT, (WPARAM)hFont, 0);
//
// Locate the existing static-control which displays our homepage
// Create a SysLink control right over the top of it (assuming current
// version of Windows supports it)
//
hwndStatic = GetDlgItem(hwnd, IDC_ABOUT_URL);
GetClientRect(hwndStatic, &rect);
MapWindowPoints(hwndStatic, hwnd, (POINT *)&rect, 2);
hwndUrl = CreateWindow(WC_LINK, SYSLINK_STR,
WS_TABSTOP|WS_CHILD|WS_VISIBLE,
rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
hwnd, 0, 0, 0
);
if(hwndUrl)
{
SendMessage(hwndUrl, WM_SETFONT, (WPARAM)hFont, 0);
ShowWindow(hwndStatic, SW_HIDE);
}
return TRUE;
case WM_NOTIFY:
// Spawn the default web-browser when the SysLink control is clicked
switch(((NMHDR *)lParam)->code)
{
case NM_CLICK: case NM_RETURN:
ShellExecute(hwnd, _T("open"), WEBSITE_URL, 0, 0, SW_SHOWNORMAL);
return 0;
}
break;
case WM_CLOSE:
EndDialog(hwnd, TRUE);
return TRUE;
case WM_COMMAND:
if(LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
EndDialog(hwnd, TRUE);
break;
}
return FALSE;
}
//
// Display the About dialog-box
//
void ShowAboutDlg(HWND hwndParent)
{
DialogBoxParam(0, MAKEINTRESOURCE(IDD_ABOUT), hwndParent, AboutDlgProc, 0);
//DialogBoxWithFont(0, MAKEINTRESOURCE(IDD_ABOUT), hwndParent, AboutDlgProc, 0, _T("MetaCondNormal-Roman"), 9);//_T("Bell MT Bold"));
}
//
// WM_NOTIFY handler for the TextView notification messages
//
UINT TextViewNotifyHandler(HWND hwnd, NMHDR *nmhdr)
{
switch(nmhdr->code)
{
// document has changed due to text input / undo / redo, update
// the main window-title to show an asterisk next to the filename
case TVN_CHANGED:
if(g_szFileTitle[0])
{
BOOL fModified = TextView_CanUndo(g_hwndTextView);
if(fModified != g_fFileChanged)
{
SetWindowFileName(hwnd, g_szFileTitle, fModified);
g_fFileChanged = fModified;
}
}
break;
// cursor position has changed, update the statusbar info
case TVN_CURSOR_CHANGE:
SetStatusBarText(g_hwndStatusbar, 1, 0, _T(" Ln %d, Col %d"),
TextView_GetCurLine(g_hwndTextView) + 1,
TextView_GetCurCol(g_hwndTextView) + 1 );
break;
// edit/insert mode changed, update statusbar info
case TVN_EDITMODE_CHANGE:
SetStatusBarText(g_hwndStatusbar, 2, 0,
g_szEditMode[TextView_GetEditMode(g_hwndTextView)] );
break;
default:
break;
}
return 0;
}
//
// Generic WM_NOTIFY handler for all other messages
//
UINT NotifyHandler(HWND hwnd, NMHDR *nmhdr)
{
NMMOUSE *nmmouse;
UINT nMode;
switch(nmhdr->code)
{
case NM_DBLCLK:
// statusbar is the only window at present which sends double-clicks
nmmouse = (NMMOUSE *)nmhdr;
// toggle the Readonly/Insert/Overwrite mode
if(nmmouse->dwItemSpec == 2)
{
nMode = TextView_GetEditMode(g_hwndTextView);
nMode = (nMode + 1) % 3;
TextView_SetEditMode(g_hwndTextView, nMode);
SetStatusBarText(g_hwndStatusbar, 2, 0, g_szEditMode[nMode]);
}
break;
default:
break;
}
return 0;
}
//
// WM_COMMAND message handler for main window
//
UINT CommandHandler(HWND hwnd, UINT nCtrlId, UINT nCtrlCode, HWND hwndFrom)
{
RECT rect;
switch(nCtrlId)
{
case IDM_FILE_NEW:
// reset to an empty file
SetWindowFileName(hwnd, _T("Untitled"), FALSE);
TextView_Clear(g_hwndTextView);
g_szFileTitle[0] = '\0';
g_fFileChanged = FALSE;
return 0;
case IDM_FILE_OPEN:
// get a filename to open
if(ShowOpenFileDlg(hwnd, g_szFileName, g_szFileTitle))
{
DoOpenFile(hwnd, g_szFileName, g_szFileTitle);
}
return 0;
case IDM_FILE_SAVE:
MessageBox(hwnd, _T("Not implemented"), APP_TITLE, MB_ICONINFORMATION);
return 0;
case IDM_FILE_SAVEAS:
// does nothing yet
if(ShowSaveFileDlg(hwnd, g_szFileName, g_szFileTitle))
{
MessageBox(hwnd, _T("Not implemented"), APP_TITLE, MB_ICONINFORMATION);
}
return 0;
case IDM_FILE_PRINT:
DeleteDC(
ShowPrintDlg(hwnd)
);
return 0;
case IDM_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
return 0;
case IDM_EDIT_UNDO: case WM_UNDO:
SendMessage(g_hwndTextView, WM_UNDO, 0, 0);
return 0;
case IDM_EDIT_REDO:
SendMessage(g_hwndTextView, TXM_REDO, 0, 0);
return 0;
case IDM_EDIT_COPY: case WM_COPY:
SendMessage(g_hwndTextView, WM_COPY, 0, 0);
return 0;
case IDM_EDIT_CUT: case WM_CUT:
SendMessage(g_hwndTextView, WM_CUT, 0, 0);
return 0;
case IDM_EDIT_PASTE: case WM_PASTE:
SendMessage(g_hwndTextView, WM_PASTE, 0, 0);
return 0;
case IDM_EDIT_DELETE: case WM_CLEAR:
SendMessage(g_hwndTextView, WM_CLEAR, 0, 0);
return 0;
case IDM_EDIT_FIND:
ShowFindDlg(hwnd, FIND_PAGE);
return 0;
case IDM_EDIT_REPLACE:
ShowFindDlg(hwnd, REPLACE_PAGE);
return 0;
case IDM_EDIT_GOTO:
ShowFindDlg(hwnd, GOTO_PAGE);
return 0;
case IDM_EDIT_SELECTALL:
TextView_SelectAll(g_hwndTextView);
return 0;
case IDM_VIEW_OPTIONS:
ShowOptions(hwnd);
return 0;
case IDM_VIEW_LINENUMBERS:
g_fLineNumbers = !g_fLineNumbers;
TextView_SetStyleBool(g_hwndTextView, TXS_LINENUMBERS, g_fLineNumbers);
return 0;
case IDM_VIEW_LONGLINES:
g_fLongLines = !g_fLongLines;
TextView_SetStyleBool(g_hwndTextView, TXS_LONGLINES, g_fLongLines);
return 0;
case IDM_VIEW_STATUSBAR:
g_fShowStatusbar = !g_fShowStatusbar;
ShowWindow(g_hwndStatusbar, SW_HIDE);
GetClientRect(hwnd, &rect);
PostMessage(hwnd, WM_SIZE, 0, MAKEWPARAM(rect.right, rect.bottom));
return 0;
case IDM_VIEW_SAVEEXIT:
g_fSaveOnExit = !g_fSaveOnExit;
return 0;
case IDM_VIEW_SAVENOW:
SaveRegSettings();
return 0;
case IDM_HELP_ABOUT:
ShowAboutDlg(hwnd);
return 0;
default:
return 0;
}
}
//
// Main window procedure
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -