下面是一个简单的英文文本编辑器的代码,其中实现了基本功能和部分附加功能:Ctrl+A实现选中全部文字并变蓝色,Ctrl+C和Ctrl+V实现复制和粘贴功能,Ctrl+S实现保存到文件,R、G、B分别实现文字颜色的变化,方向键实现光标的移动。
```cpp
#include <windows.h>
#include <string>
#include <fstream>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
const int ID_EDIT = 1;
bool ctrlPressed = false;
bool rPressed = false;
bool gPressed = false;
bool bPressed = false;
COLORREF textColor = RGB(0, 0, 0);
COLORREF bgColor = RGB(255, 255, 255);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
const char* CLASS_NAME = "SimpleTextEditor";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hWnd = CreateWindowEx(
0,
CLASS_NAME,
"Simple Text Editor",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
nullptr,
nullptr,
hInstance,
nullptr
);
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
static HWND hEdit;
switch (message) {
case WM_CREATE:
hEdit = CreateWindowEx(
WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
0, 0, 0, 0,
hWnd,
(HMENU)ID_EDIT,
GetModuleHandle(nullptr),
nullptr
);
break;
case WM_SIZE:
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
case WM_COMMAND:
if (LOWORD(wParam) == ID_EDIT) {
if (HIWORD(wParam) == EN_UPDATE) {
// text has been updated
}
}
break;
case WM_KEYDOWN:
if (wParam == VK_CONTROL) {
ctrlPressed = true;
}
else if (ctrlPressed) {
if (wParam == 'A') {
SendMessage(hEdit, EM_SETSEL, 0, -1);
SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor);
SendMessage(hEdit, EM_SETTEXTCOLOR, 0, RGB(0, 0, 255));
}
else if (wParam == 'C') {
SendMessage(hEdit, WM_COPY, 0, 0);
}
else if (wParam == 'V') {
SendMessage(hEdit, WM_PASTE, 0, 0);
}
else if (wParam == 'S') {
char buffer[MAX_PATH];
OPENFILENAME ofn = {};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = buffer;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&ofn)) {
std::ofstream file(ofn.lpstrFile);
int length = GetWindowTextLength(hEdit);
char* text = new char[length + 1];
GetWindowText(hEdit, text, length + 1);
file << text;
delete[] text;
}
}
else if (wParam == 'R') {
rPressed = true;
gPressed = false;
bPressed = false;
textColor = RGB(255, 0, 0);
SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor);
SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor);
}
else if (wParam == 'G') {
rPressed = false;
gPressed = true;
bPressed = false;
textColor = RGB(0, 255, 0);
SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor);
SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor);
}
else if (wParam == 'B') {
rPressed = false;
gPressed = false;
bPressed = true;
textColor = RGB(0, 0, 255);
SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor);
SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor);
}
}
else if (wParam == VK_LEFT) {
int pos = SendMessage(hEdit, EM_GETSEL, 0, 0);
SendMessage(hEdit, EM_SETSEL, pos - 1, pos - 1);
}
else if (wParam == VK_RIGHT) {
int pos = SendMessage(hEdit, EM_GETSEL, 0, 0);
SendMessage(hEdit, EM_SETSEL, pos + 1, pos + 1);
}
else if (wParam == VK_UP) {
int line = SendMessage(hEdit, EM_LINEFROMCHAR, -1, 0);
int pos = SendMessage(hEdit, EM_LINEINDEX, line - 1, 0);
SendMessage(hEdit, EM_SETSEL, pos, pos);
}
else if (wParam == VK_DOWN) {
int line = SendMessage(hEdit, EM_LINEFROMCHAR, -1, 0);
int pos = SendMessage(hEdit, EM_LINEINDEX, line + 1, 0);
SendMessage(hEdit, EM_SETSEL, pos, pos);
}
break;
case WM_KEYUP:
if (wParam == VK_CONTROL) {
ctrlPressed = false;
}
else if (wParam == 'R') {
if (!gPressed && !bPressed) {
textColor = RGB(0, 0, 0);
SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor);
SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor);
}
rPressed = false;
}
else if (wParam == 'G') {
if (!rPressed && !bPressed) {
textColor = RGB(0, 0, 0);
SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor);
SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor);
}
gPressed = false;
}
else if (wParam == 'B') {
if (!rPressed && !gPressed) {
textColor = RGB(0, 0, 0);
SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, bgColor);
SendMessage(hEdit, EM_SETTEXTCOLOR, 0, textColor);
}
bPressed = false;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
```