📄 neatpad.c
字号:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
int width, height, heightsb;
HIMAGELIST hImgList;
RECT rect;
HDWP hdwp;
NMHDR *nmhdr;
TCHAR msgstr[MAX_PATH+200];
switch(msg)
{
case WM_CREATE:
g_hwndTextView = CreateTextView(hwnd);
g_hwndStatusbar = CreateStatusBar(hwnd);
TextView_SetContextMenu(g_hwndTextView, GetSubMenu(LoadMenu(GetModuleHandle(0),
MAKEINTRESOURCE(IDR_MENU2)), 0));
// load the image list
hImgList = ImageList_LoadImage(
GetModuleHandle(0),
MAKEINTRESOURCE(IDB_BITMAP1),
16, 0,
RGB(255,0,255),
IMAGE_BITMAP,
LR_LOADTRANSPARENT|LR_CREATEDIBSECTION
);
TextView_SetImageList(g_hwndTextView, hImgList);
// highlight specific lines with image-index "1"
//TextView_SetLineImage(g_hwndTextView, 16, 1);
//TextView_SetLineImage(g_hwndTextView, 5, 1);
//TextView_SetLineImage(g_hwndTextView, 36, 1);
//TextView_SetLineImage(g_hwndTextView, 11, 1);
// tell windows that we can handle drag+drop'd files
DragAcceptFiles(hwnd, TRUE);
return 0;
case WM_DROPFILES:
HandleDropFiles(hwnd, (HDROP)wParam);
return 0;
case WM_DESTROY:
SaveFileData(g_szFileName, hwnd);
PostQuitMessage(0);
DeleteObject(g_hFont);
return 0;
//case WM_NCCALCSIZE:
// return NcCalcSize(hwnd, wParam, lParam);
case WM_INITMENU:
CheckMenuCommand((HMENU)wParam, IDM_VIEW_LINENUMBERS, g_fLineNumbers);
CheckMenuCommand((HMENU)wParam, IDM_VIEW_LONGLINES, g_fLongLines);
CheckMenuCommand((HMENU)wParam, IDM_VIEW_SAVEEXIT, g_fSaveOnExit);
CheckMenuCommand((HMENU)wParam, IDM_VIEW_STATUSBAR, g_fShowStatusbar);
//CheckMenuCommand((HMENU)wParam, IDM_VIEW_SEARCHBAR, g_hwndSearchBar ? TRUE : FALSE);
EnableMenuCommand((HMENU)wParam, IDM_EDIT_UNDO, TextView_CanUndo(g_hwndTextView));
EnableMenuCommand((HMENU)wParam, IDM_EDIT_REDO, TextView_CanRedo(g_hwndTextView));
EnableMenuCommand((HMENU)wParam, IDM_EDIT_PASTE, IsClipboardFormatAvailable(CF_TEXT));
EnableMenuCommand((HMENU)wParam, IDM_EDIT_COPY, TextView_GetSelSize(g_hwndTextView));
EnableMenuCommand((HMENU)wParam, IDM_EDIT_CUT, TextView_GetSelSize(g_hwndTextView));
EnableMenuCommand((HMENU)wParam, IDM_EDIT_DELETE, TextView_GetSelSize(g_hwndTextView));
return 0;
//case WM_USER:
// wsprintf(msgstr, _T("%s\n\nThis file has been modified outside of Neatpad.")
// _T("Do you wish to reload it?"), g_szFileName);
// MessageBox(hwnd, msgstr, _T("Neatpad"), MB_ICONQUESTION|MB_YESNO);
//
// return 0;
case WM_ENABLE:
// keep the modeless find/replace dialog in the same enabled state as the main window
EnableWindow(g_hwndSearchDlg, (BOOL)wParam);
return 0;
case WM_MENUSELECT:
StatusBarMenuSelect(hwnd, g_hwndStatusbar, wParam, lParam);
return 0;
case WM_NOTIFY:
nmhdr = (NMHDR *)lParam;
if(nmhdr->hwndFrom == g_hwndTextView)
return TextViewNotifyHandler(hwnd, nmhdr);
else
return NotifyHandler(hwnd, nmhdr);
case WM_COMMAND:
return CommandHandler(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam);
case WM_SETFOCUS:
SetFocus(g_hwndTextView);
return 0;
case WM_CLOSE:
// does the file need saving?
if(TextView_CanUndo(g_hwndTextView))
{
UINT r;
wsprintf(msgstr, _T("Do you want to save changes to\r\n%s?"), g_szFileName);
r = MessageBox(hwnd, msgstr, APP_TITLE, MB_YESNOCANCEL | MB_ICONQUESTION);
if(r == IDCANCEL)
return 0;
}
DestroyWindow(hwnd);
return 0;
case WM_SIZE:
// resize the TextView and StatusBar to fit within the main window's client area
width = (short)LOWORD(lParam);
height = (short)HIWORD(lParam);
GetWindowRect(g_hwndStatusbar, &rect);
heightsb = rect.bottom-rect.top;
hdwp = BeginDeferWindowPos(3);
if(g_fShowStatusbar)
{
DeferWindowPos(hdwp, g_hwndStatusbar, 0, 0, height - heightsb, width, heightsb, SWP_SHOWWINDOW);
// MoveWindow(g_hwndStatusbar, 0, height - heightsb, width, heightsb, TRUE);
height -= heightsb;
}
DeferWindowPos(hdwp, g_hwndTextView, 0, 0, 0, width, height, SWP_SHOWWINDOW);
//MoveWindow(g_hwndTextView, 0, 0, width, height, TRUE);
EndDeferWindowPos(hdwp);
SetStatusBarParts(g_hwndStatusbar);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//
// Register main window class
//
void InitMainWnd()
{
WNDCLASSEX wcx;
HANDLE hInst = GetModuleHandle(0);
// Window class for the main application parent window
wcx.cbSize = sizeof(wcx);
wcx.style = CS_OWNDC;
wcx.lpfnWndProc = WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInst;
wcx.hCursor = LoadCursor (NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)0;
wcx.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcx.lpszClassName = g_szAppName;
wcx.hIcon = LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32, 32, LR_CREATEDIBSECTION);
wcx.hIconSm = LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_CREATEDIBSECTION);
RegisterClassEx(&wcx);
}
//
// Create a top-level window
//
HWND CreateMainWnd()
{
return CreateWindowEx(0,
g_szAppName, // window class name
g_szAppName, // window caption
WS_OVERLAPPEDWINDOW,//|WS_CLIPCHILDREN,
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // use window class menu
GetModuleHandle(0), // program instance handle
NULL); // creation parameters
}
TCHAR **GetArgvCommandLine(int *argc)
{
#ifdef UNICODE
return CommandLineToArgvW(GetCommandLineW(), argc);
#else
*argc = __argc;
return __argv;
#endif
}
TCHAR * GetArg(TCHAR *ptr, TCHAR *buf, int len)
{
int i = 0;
int ch;
// make sure there's something to parse
if(ptr == 0 || *ptr == '\0')
{
*buf = '\0';
return 0;
}
ch = *ptr++;
// skip leading whitespace
while(ch == ' ' || ch == '\t')
ch = *ptr++;
// quoted filenames
if(ch == '\"')
{
ch = *ptr++;
while(i < len - 1 && ch && ch != '\"')
{
buf[i++] = ch;
if(ch = *ptr) *ptr++;
}
}
// grab a token
else
{
while(i < len - 1 && ch && ch != ' ' && ch != '\t')
{
buf[i++] = ch;
if(ch = *ptr) *ptr++;
}
}
buf[i] = '\0';
// skip trailing whitespace
while(*ptr == ' ' || *ptr == '\t')
ptr++;
return ptr;
}
//
// Entry-point for text-editor application
//
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int iShowCmd)
{
MSG msg;
HACCEL hAccel;
TCHAR **argv;
int argc;
//TCHAR *pszCmdlineFile = 0;
TCHAR arg[MAX_PATH];
//
// get the first commandline argument
//
TCHAR *pszCmdline = GetArg(GetCommandLineW(), arg, MAX_PATH);
argv = GetArgvCommandLine(&argc);
// check if we have any options
if(pszCmdline && *pszCmdline == '-')
{
pszCmdline = GetArg(pszCmdline, arg, MAX_PATH);
// do the user-account-control thing
if(lstrcmpi(arg, _T("-uac")) == 0)
{
//return 0;
}
// image-file-execute-options
else if(lstrcmpi(arg, _T("-ifeo")) == 0)
{
// skip notepad.exe
pszCmdline = GetArg(pszCmdline, arg, MAX_PATH);
}
// unrecognised option
else
{
}
}
// has a prior instance elevated us to admin in order to
// modify some system-wide settings in the registry?
if(argv && argc == 4 && lstrcmpi(argv[1], _T("-uac")) == 0)
{
g_fAddToExplorer = _ttoi(argv[2]);
g_fReplaceNotepad = _ttoi(argv[3]);
if(SetExplorerContextMenu(g_fAddToExplorer) &&
SetImageFileExecutionOptions(g_fReplaceNotepad))
{
SaveRegSysSettings();
return 0;
}
else
{
return ERROR_ACCESS_DENIED;
}
}
// default to the built-in resources
g_hResourceModule = hInst;
OleInitialize(0);
// initialize window classes
InitMainWnd();
InitTextView();
LoadRegSettings();
// create the main window!
g_hwndMain = CreateMainWnd();
// open file specified on commmand line
if(pszCmdline && *pszCmdline)
{
// check to see if it's a quoted filename
if(*pszCmdline == '\"')
{
GetArg(pszCmdline, arg, MAX_PATH);
pszCmdline = arg;
}
NeatpadOpenFile(g_hwndMain, pszCmdline);
LoadFileData(pszCmdline, g_hwndMain);
}
// automatically create new document if none specified
else
{
PostMessage(g_hwndMain, WM_COMMAND, IDM_FILE_NEW, 0);
}
ApplyRegSettings();
ShowWindow(g_hwndMain, iShowCmd);
//
// load keyboard accelerator table
//
hAccel = LoadAccelerators(g_hResourceModule, MAKEINTRESOURCE(IDR_ACCELERATOR1));
//
// message-loop
//
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
if(!TranslateAccelerator(g_hwndMain, hAccel, &msg))
{
if((!IsWindow(g_hwndSearchDlg) || !IsDialogMessage(g_hwndSearchDlg, &msg)))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
if(g_fSaveOnExit)
SaveRegSettings();
OleUninitialize();
ExitProcess(0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -