仅供参考:
CEdit Class Members
Construction
Attributes
Operations
Clipboard Operations
Construction
CEdit Constructs a CEdit control object.
Create Creates the Windows edit control and attaches it to the CEdit object.
CEdit Attributes
CanUndo Determines whether an edit-control operation can be undone.
GetLineCount Retrieves the number of lines in a multiple-line edit control.
GetModify Determines whether the contents of an edit control have been modified.
SetModify Sets or clears the modification flag for an edit control.
GetRect Gets the formatting rectangle of an edit control.
GetSel Gets the starting and ending character positions of the current selection in an edit control.
GetHandle Retrieves a handle to the memory currently allocated for a multiple-line edit control.
SetHandle Sets the handle to the local memory that will be used by a multiple-line edit control.
SetMargins Sets the left and right margins for this CEdit.
GetMargins Gets the left and right margins for this CEdit.
SetLimitText Sets the maximum amount of text this CEdit can contain.
GetLimitText Gets the maximum amount of text this CEdit can contain.
PosFromChar Retrieves the coordinates of the upper-left corner of a specified character index.
CharFromPos Retrieves the line and character indices for the character closest to a specified position.
GetLine Retrieves a line of text from an edit control.
GetPasswordChar Retrieves the password character displayed in an edit control when the user enters text.
GetFirstVisibleLine Determines the topmost visible line in an edit control.
CEdit Operations
EmptyUndoBuffer Resets (clears) the undo flag of an edit control.
FmtLines Sets the inclusion of soft line-break characters on or off within a multiple-line edit control.
LimitText Limits the length of the text that the user may enter into an edit control.
LineFromChar Retrieves the line number of the line that contains the specified character index.
LineIndex Retrieves the character index of a line within a multiple-line edit control.
LineLength Retrieves the length of a line in an edit control.
LineScroll Scrolls the text of a multiple-line edit control.
ReplaceSel Replaces the current selection in an edit control with the specified text.
SetPasswordChar Sets or removes a password character displayed in an edit control when the user enters text.
SetRect Sets the formatting rectangle of a multiple-line edit control and updates the control.
SetRectNP Sets the formatting rectangle of a multiple-line edit control without redrawing the control window.
SetSel Selects a range of characters in an edit control.
SetTabStops Sets the tab stops in a multiple-line edit control.
SetReadOnly Sets the read-only state of an edit control.
CEdit Clipboard Operations
Undo Reverses the last edit-control operation.
Clear Deletes (clears) the current selection (if any) in the edit control.
Copy Copies the current selection (if any) in the edit control to the Clipboard in CF_TEXT format.
Cut Deletes (cuts) the current selection (if any) in the edit control and copies the deleted text to the Clipboard in CF_TEXT format.
Paste Inserts the data from the Clipboard into the edit control at the current cursor position. Data is inserted only if the Clipboard contains data in CF_TEXT format.
CEdit Overview | Base Class Members | Hierarchy Chart
Clipboard: Using the Windows Clipboard
Home | Overview | How Do I
This article describes how to use the standard Windows Clipboard API within your MFC application.
Most applications for Windows support cutting or copying data to the Windows Clipboard and pasting data from the Clipboard. The Clipboard data formats vary among applications. The framework supports only a limited number of Clipboard formats for a limited number of classes. You will normally implement the Clipboard-related commands — Cut, Copy, and Paste — on the Edit menu for your view. The class library defines the command IDs for these commands: ID_EDIT_CUT, ID_EDIT_COPY, and ID_EDIT_PASTE. Their message-line prompts are also defined.
Messages and Commands in the Framework, explains how to handle menu commands in your application by mapping the menu command to a handler function. As long as your application does not define handler functions for the Clipboard commands on the Edit menu, they remain disabled. To write handler functions for the Cut and Copy commands, implement selection in your application. To write a handler function for the Paste command, query the Clipboard to see whether it contains data in a format your application can accept. For example, to enable the Copy command, you might write a handler something like the following:
void CMyView::OnEditCopy()
{
if ( !OpenClipboard() )
{
AfxMessageBox( "Cannot open the Clipboard" );
return;
}
// Remove the current Clipboard contents
if( !EmptyClipboard() )
{
AfxMessageBox( "Cannot empty the Clipboard" );
return;
}
// ...
// Get the currently selected data
// ...
// For the appropriate data formats...
if ( ::SetClipboardData( CF_??, hData ) == NULL )
{
AfxMessageBox( "Unable to set Clipboard data" );
CloseClipboard();
return;
}
// ...
CloseClipboard();
}
The Cut, Copy, and Paste commands are only meaningful in certain contexts. The Cut and Copy commands should be enabled only when something is selected, and the Paste command only when something is in the Clipboard. You can provide this behavior by defining update handler functions that enable or disable these commands depending on the context. For more information, see How to Update User-Interface Objects.
The Microsoft Foundation Class Library does provide Clipboard support for text editing with the CEdit and CEditView classes. The OLE classes also simplify implementing Clipboard operations that involve OLE items. For more information on the OLE classes, see Clipboard: Using the OLE Clipboard Mechanism.
Implementing other Edit menu commands, such as Undo (ID_EDIT_UNDO) and Redo (ID_EDIT_REDO), is also left to you. If your application does not support these commands, you can easily delete them from your resource file using the Visual C++ resource editors.
What do you want to know more about?
Copying and pasting data
Using the OLE Clipboard mechanism
//! C/C++语言Windows程序设计 -> 简易文本编辑器 -> 演示
#include <windows.h>
#include <winldap.h>
#include <stdio.h>
#include <stdlib.h>
//! 各控件所使用的ID
#define ID_EDITBOX 1 //< 文本编辑框控件
#define ID_TXTPATH 2 //< 路径编辑框控件
#define ID_SAVEBTN 3 //< 保存文件按钮
#define ID_CLSBTN 4 //< 清空编辑区按钮
#define ID_GROUP 5 //< 组合框
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int CreateChildWindow(HWND, HWND*, LPARAM); //< 创建将使用到的子窗口控件
int SavaInputContent(TCHAR*, TCHAR*); //< 保存输入的文字到文件
char* UnicodeToUtf8(const wchar_t* unicode);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("demo");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.hInstance = hInstance;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = CreateSolidBrush(RGB(236, 233, 216));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Unable to register the window class!"),
TEXT("error"), MB_OK | MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("Simple text editor -> demo"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndChild[5]; //<主窗口的子窗口
HDC hdc;
PAINTSTRUCT ps;
RECT rect ;
static TCHAR *szBuffer = nullptr; //< 写入文件的缓冲区
static TCHAR szPath[256] = {0}; //< 文本路径
static TCHAR szLineNum[32] = {0}; //< 行数与字数个数输出缓冲区
static TCHAR tmp[128] = {0}; //< 保存成功字样的缓冲区
static int iLength = 0;
static int iFlag = 0;
static TCHAR Prompt[32] = {0};
int iLineCount = 0; //< 文本行数
int iCharCount = 0; //< 文本字符个数
switch(message)
{
case WM_CREATE:
CreateChildWindow(hwnd, hwndChild, lParam);
return 0;
case WM_SIZE:
GetClientRect(hwnd, &rect);
//! 对父窗口中的子窗口予以布置
MoveWindow(hwndChild[ID_EDITBOX], 0, 0,
rect.right, rect.bottom-50, TRUE); //< 文本编辑区
MoveWindow(hwndChild[ID_TXTPATH], 60, rect.bottom-31,
200, 20, TRUE); //< 文本路径输入框
MoveWindow(hwndChild[ID_SAVEBTN], 280, rect.bottom-35,
50, 25, TRUE); //<保存按钮
MoveWindow(hwndChild[ID_CLSBTN ], 400, rect.bottom-35,
50, 25, TRUE); //<清空按钮
MoveWindow(hwndChild[ID_GROUP ], 10, rect.bottom-50,
330, 48, TRUE); //<组合框
return 0;
case WM_PAINT:
GetClientRect(hwnd, &rect);
hdc = BeginPaint(hwnd, &ps);
if (1 == iFlag)
{
iFlag = 0;
swprintf_s(tmp, 128, L"Saved!");
}
else
{
swprintf_s(tmp, 128, L"UnSaved!");
}
TextOut(hdc, rect.right - 110,rect.bottom - 30, tmp,lstrlen(tmp));
TextOut(hdc, 20, rect.bottom - 30, TEXT("Path:"), lstrlen(TEXT("Path:")));
TextOut(hdc, 500, rect.bottom - 30, szLineNum, lstrlen(szLineNum));
EndPaint(hwnd, &ps);
return 0;
//! 当用户从菜单中选择命令项、控件向其父窗口发送通知消息或翻译快捷键击键时发送。
case WM_COMMAND:
switch(LOWORD(wParam))
{
//! 文本编辑框
case ID_EDITBOX:
switch(HIWORD(wParam))
{
//! 当编辑控件即将重绘自身时发送。此通知代码在控件格式化文本之后、显示文本之前发送.
//! 此键存在原因:在输入之后就要显示字数和行数,
//! 因此要通过此键来保证字数行数的变换跟上输入的变换
case EN_UPDATE:
iLineCount = SendMessage(hwndChild[ID_EDITBOX], EM_GETLINECOUNT, 0, 0);
iCharCount = GetWindowTextLength(hwndChild[ID_EDITBOX]);
wsprintf(szLineNum, L"LineNum:%d WordsNum:%d", iLineCount, iCharCount);
//! 添加一个矩形到指定窗口的更新区域,表示必须重绘该矩形部分
//! 参数1:该矩形窗口的句柄
//! 2:指向包含要添加到更新区域的矩形的客户端坐标的 RECT结构的指针。
// 如果此参数为NULL,则将整个客户区添加到更新区域。
//! 3:是否要擦除更新区域背景(bool值)
//! 在此处的作用:保持字数和行数变化后会及时显示
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
break;
default:
break;
}
return 0;
//! 保存文件的按钮
case ID_SAVEBTN:
iLength = GetWindowTextLength(hwndChild[ID_EDITBOX]);
if( iLength != 0)
{
szBuffer = (TCHAR*)malloc((iLength+1)*sizeof(TCHAR));
if (szBuffer == nullptr)
{
MessageBox(NULL, TEXT("Allocation Failure"),
TEXT("Prompt"), MB_OK | MB_ICONINFORMATION);
return -1;
}
}
else
{
return -1;
}
GetWindowText(hwndChild[ID_EDITBOX], szBuffer, (iLength+1)*sizeof(TCHAR));
if(GetWindowText(hwndChild[ID_TXTPATH], szPath, 256) < 1)
{
MessageBox(NULL, TEXT("The path cannot be null"),
TEXT("Prompt"), MB_OK | MB_ICONINFORMATION);
return -1;
}
SavaInputContent(szPath, szBuffer);//<保存文件
free(szBuffer);
szBuffer = nullptr;
//!显示保存成功的提示
iFlag = 1;
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
return 0;
case ID_CLSBTN:
SetWindowText(hwndChild[ID_EDITBOX], TEXT(""));
wsprintf(szLineNum, L"LineNum:%d WordsNum:%d", 1, 0);
InvalidateRect(hwnd, NULL, FALSE);
return 0;
default:
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
//! 创建子窗口
int CreateChildWindow(HWND hwnd, HWND *hwndChild, LPARAM lParam)
{
HINSTANCE hInst = ((LPCREATESTRUCT) lParam) -> hInstance;
//! 创建编辑区
hwndChild[ID_EDITBOX] = CreateWindow(TEXT("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 0, 0,
hwnd, (HMENU)ID_EDITBOX, hInst, NULL);
//! 路径输入框
hwndChild[ID_TXTPATH] = CreateWindow(TEXT("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOVSCROLL,
0, 0, 0, 0,
hwnd, (HMENU)ID_TXTPATH, hInst, NULL);
//! 保存按钮
hwndChild[ID_SAVEBTN] = CreateWindow(TEXT("button"), TEXT("Save"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 0, 0,
hwnd, (HMENU)ID_SAVEBTN, hInst, NULL);
//! 清空按钮
hwndChild[ID_CLSBTN] = CreateWindow(TEXT("button"), TEXT("Clear"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 0, 0,
hwnd, (HMENU)ID_CLSBTN, hInst, NULL);
//! 组合框
hwndChild[ID_GROUP] = CreateWindow(TEXT("button"), NULL,
WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 0, 0, 0, 0,
hwnd, (HMENU)ID_GROUP, hInst, NULL);
return 0;
}
//! 将unicode转换为UTF-8
char* UnicodeToUtf8(const wchar_t* unicode)
{
int len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1,
NULL, 0, NULL, NULL) +1;
char* Ptr = (char*)malloc(len); //< 转换之后存放在Ptr缓冲区中
if (Ptr == nullptr)
{
MessageBox(NULL, TEXT("Allocation Failure"),
TEXT("Prompt"), MB_OK | MB_ICONINFORMATION);
return nullptr;
}
WideCharToMultiByte(CP_UTF8, 0, unicode, -1, Ptr, len, NULL, NULL);
return Ptr;
}
//!保存输入内容
#if 1
int SavaInputContent(TCHAR* path, TCHAR* content)
{
FILE *fSave = NULL;
char* contentPath = nullptr;
errno_t err = _wfopen_s(&fSave, path, L"a");//<a:追加写 w:只写
if (0 != err || NULL == fSave)
{
MessageBox(NULL, TEXT("File create failed!"),
TEXT("Prompt"), MB_OK | MB_ICONINFORMATION);
return err;
}
contentPath = UnicodeToUtf8(content);
fwrite(contentPath, 1 ,strlen(contentPath), fSave);
fclose(fSave);
//!写完之后释放资源
free(contentPath);
contentPath = nullptr;
return 0;
}
#else
int SavaInputContent(TCHAR* path, TCHAR* content)
{
FILE *fSave = NULL;
errno_t err = _wfopen_s(&fSave, path, L"a");//<a:追加写 w:只写
if (0 != err || NULL == fSave)
{
MessageBox(NULL, TEXT("File create failed!"),
TEXT("Prompt"), MB_OK | MB_ICONINFORMATION);
return err;
}
const unsigned char uszHead[] = {0xFF, 0xFE};
fwrite(uszHead, 1, sizeof(uszHead), fSave);
fwrite(content, 2, wcslen(content), fSave);
fclose(fSave);
fSave = NULL;
return 0;
}
#endif
1.这个写了如何从剪切板获取、写入数据。https://blog.csdn.net/V1040375575/article/details/105412633
2.你用可视化工具去建好对应的控件,绑定变量
3.直接双击按钮控件会生成点击方法,再通过编辑框绑定的变量去获取值,对值进行写入剪贴板就是复制
4.对值写入剪贴板后清空值就是剪切
5.将剪贴板的值赋给编辑框绑定的变量就是贴贴
6.直接置空编辑框绑定的变量就是删除
这是如何操作编辑框的值https://blog.csdn.net/weixin_45891637/article/details/106967541?ops_request_misc=&request_id=&biz_id=102&utm_term=mfc%E5%85%B3%E8%81%94%E5%8F%98%E9%87%8F%E6%96%B9%E5%BC%8F&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-3-106967541.142^v68^control,201^v4^add_ask,213^v2^t3_esquery_v3&spm=1018.2226.3001.4449
这是如何关联变量https://www.cnblogs.com/douzi2/p/3672784.html
来