用WIN API创建一个带edit文本框的窗口程序,edit中预设字符串"我们的",设置字体大小30,颜色red,输出预设字符串到messageBox
时间: 2024-03-13 21:43:58 浏览: 63
好的,以下是代码实现:
```c++
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const char CLASS_NAME[] = "Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
"Edit Textbox Sample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 350, 250,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
// 创建编辑框
HWND hEdit = CreateWindowEx(
0,
"EDIT",
"我们的",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL | ES_CENTER,
50, 50, 200, 80,
hwnd,
NULL,
GetModuleHandle(NULL),
NULL);
// 设置字体大小和颜色
HFONT hFont = CreateFont(30, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
SendMessage(hEdit, EM_SETBKGNDCOLOR, 0, RGB(255, 255, 255));
SendMessage(hEdit, EM_SETSEL, 0, -1);
break;
}
case WM_COMMAND:
{
if (HIWORD(wParam) == EN_CHANGE)
{
// 获取编辑框中的文本
char buffer[100];
GetWindowText((HWND)lParam, buffer, 100);
// 显示预设字符串到message box
if (strcmp(buffer, "我们的") == 0)
{
MessageBox(hwnd, buffer, "预设字符串", MB_OK);
}
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
```
这段代码创建了一个带有一个edit文本框的窗口程序,edit中预设字符串为"我们的",字体大小为30,颜色为红色。当用户修改文本框中的内容时,程序会检查是否与预设字符串相同,如果相同则弹出一个message box,显示预设字符串。
阅读全文