作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个程序创建WC_EDIT
控制在主窗口内。每当调整窗口本身的大小时,主窗口都会调整编辑控件的大小。我正在尝试通过更新 WC_EDIT
的格式化矩形来设置编辑控件内容的边距控制何时调整大小。
主窗口响应WM_SIZE
消息并调整编辑控件的大小,然后编辑控件响应它自己的 WM_SIZE
消息并尝试使用 EM_SETRECT
更新格式化矩形, 但没有任何反应。
#include <windows.h>
#include <commctrl.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK EditWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND hwnd;
HWND editHwnd;
WNDPROC origEditProc;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
{
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = "Window Class";
RegisterClass(&wc);
hwnd = CreateWindow(
wc.lpszClassName,
"",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
editHwnd = CreateWindow(
WC_EDIT,
"Lorem ipsum",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
0,
0,
100,
100,
hwnd,
NULL,
hInstance,
NULL
);
origEditProc = (WNDPROC) GetWindowLongPtr(editHwnd, GWLP_WNDPROC);
SetWindowLongPtr(editHwnd, GWLP_WNDPROC, (LONG_PTR)EditWindowProc);
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_DESTROY:
PostQuitMessage(0);
return 0;
return 0;
case WM_SIZE:
RECT clientRect;
GetClientRect(hwnd, &clientRect);
SetWindowPos(editHwnd, NULL, 0, 0, clientRect.right - clientRect.left, clientRect.bottom - clientRect.top,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK EditWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_SIZE:
RECT rect;
GetClientRect(editHwnd, &rect);
rect.left += 50;
rect.top += 50;
rect.right -= 50;
rect.bottom -= 50;
SendMessage(editHwnd, EM_SETRECT, NULL, (LPARAM)&rect);
}
return CallWindowProc(origEditProc, hwnd, uMsg, wParam, lParam);
}
最佳答案
WC_EDIT
控件的原始窗口过程应该在更新格式化矩形之前被调用:
LRESULT CALLBACK EditWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT result = CallWindowProc(origEditProc, hwnd, uMsg, wParam, lParam);
switch (uMsg)
{
case WM_SIZE:
RECT rect;
GetClientRect(editHwnd, &rect);
rect.left += 50;
rect.top += 50;
rect.right -= 50;
rect.bottom -= 50;
SendMessage(editHwnd, EM_SETRECT, NULL, (LPARAM)&rect);
}
return result;
}
关于c++ - 调整大小时如何更新 WC_EDIT 控件边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48224317/
我是一名优秀的程序员,十分优秀!