在 Win32 中使用剪贴板
Using the Clipboard in Win32
我使用 Win32API GUI 在 Code::Blocks 中创建了一个小程序,全部用 C++ 编写。
我想添加一个按钮来自动复制编辑控件中的文本,你是怎么做到的?
(请注意,我是 Win32API 的新手)
需要通过OpenClipboard
, and then call SetClipboardData
关联编辑控件才能设置剪贴板
这是一个示例:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
#define IDB_ONE 3001
HWND edit;
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR szCmdLine, _In_ int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
edit = CreateWindowW(L"edit", L"test", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_WANTRETURN | ES_NOHIDESEL, 100, 100, 300, 300, hwnd, NULL, NULL, NULL);
HWND btn = CreateWindowW(L"Button", L"copy", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 500, 150, 50, 50, hwnd, (HMENU)IDB_ONE, NULL, NULL);
return 0;
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDB_ONE:
int len = GetWindowTextLengthW(edit);
TCHAR buf[1024]{};
size_t memsize = sizeof(wchar_t) * (len + 1);
GetWindowTextW(edit, (LPWSTR)buf, len + 1);
HGLOBAL glob = GlobalAlloc(GMEM_MOVEABLE, memsize);
if (glob != NULL)
{
void* mem = GlobalLock(glob);
if (mem != NULL)
{
memcpy(mem, buf, memsize);
GlobalUnlock(glob);
HANDLE handle = NULL;
if (OpenClipboard(edit))
{
EmptyClipboard();
handle = SetClipboardData(CF_UNICODETEXT, glob);
}
}
}
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
对我有用:
我使用 Win32API GUI 在 Code::Blocks 中创建了一个小程序,全部用 C++ 编写。 我想添加一个按钮来自动复制编辑控件中的文本,你是怎么做到的?
(请注意,我是 Win32API 的新手)
需要通过OpenClipboard
, and then call SetClipboardData
关联编辑控件才能设置剪贴板
这是一个示例:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
#define IDB_ONE 3001
HWND edit;
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR szCmdLine, _In_ int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
edit = CreateWindowW(L"edit", L"test", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_WANTRETURN | ES_NOHIDESEL, 100, 100, 300, 300, hwnd, NULL, NULL, NULL);
HWND btn = CreateWindowW(L"Button", L"copy", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 500, 150, 50, 50, hwnd, (HMENU)IDB_ONE, NULL, NULL);
return 0;
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDB_ONE:
int len = GetWindowTextLengthW(edit);
TCHAR buf[1024]{};
size_t memsize = sizeof(wchar_t) * (len + 1);
GetWindowTextW(edit, (LPWSTR)buf, len + 1);
HGLOBAL glob = GlobalAlloc(GMEM_MOVEABLE, memsize);
if (glob != NULL)
{
void* mem = GlobalLock(glob);
if (mem != NULL)
{
memcpy(mem, buf, memsize);
GlobalUnlock(glob);
HANDLE handle = NULL;
if (OpenClipboard(edit))
{
EmptyClipboard();
handle = SetClipboardData(CF_UNICODETEXT, glob);
}
}
}
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
对我有用: