gpt4 book ai didi

c - 当我点击我的子窗口时,为什么我得到 WM_MOUSEACTIVATE?我让它把焦点转移到 parent 身上,这会搞砸 child 杀死焦点逻辑

转载 作者:太空宇宙 更新时间:2023-11-04 00:05:39 25 4
gpt4 key购买 nike

我有一个用作编辑器 View 的自定义窗口类,并且由于我直接处理键盘事件,所以我希望在顶部有一个标准的编辑控件来输入文本。我的问题很简单:我也想在用户点击自定义窗口类时获取焦点,所以我处理 WM_MOUSEACTIVATE:

    case WM_MOUSEACTIVATE:
SetFocus(hwnd);
return MA_ACTIVATE;

但我也希望编辑控件在失去焦点时消失:

    case WM_COMMAND:
if (HIWORD(wparam) == EN_KILLFOCUS) {
MessageBeep(-1);
ShowWindow(edit, SW_HIDE);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);

除了出于某种原因,WM_MOUSEACTIVATE 处理程序在我单击(可见的)编辑控件时触发;这会导致它失去焦点并重新获得焦点,但在我隐藏编辑控件之前不会!

完整的测试程序如下;如果您单击窗口客户区的任意位置,您将看到编辑控件。单击一次以使其成为焦点,然后再次单击它。您应该会听到一声嘟嘟声,然后编辑控件消失。需要通用控件 6。

如果我将编辑控件子类化并在那里处理 WM_KILLFOCUS,也会发生这种情况。 (这是我最初拥有的。)

这是怎么回事?谢谢。

// 22-23 august 2014
// edited from wineditoverlaytest 22 august 2014
// scratch Windows program by pietro gagliardi 17 april 2014
// fixed typos and added toWideString() 1 may 2014
// borrows code from the scratch GTK+ program (16-17 april 2014) and from code written 31 march 2014 and 11-12 april 2014
#define _UNICODE
#define UNICODE
#define STRICT
#define _GNU_SOURCE // needed to declare asprintf()/vasprintf()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <windows.h>
#include <commctrl.h> // needed for InitCommonControlsEx() (thanks Xeek in irc.freenode.net/#winapi for confirming)
#include <windowsx.h>

#ifdef _MSC_VER
#error sorry! the scratch windows program relies on mingw-only functionality! (specifically: asprintf())
#endif

HMODULE hInstance;
HICON hDefaultIcon;
HCURSOR hDefaultCursor;
HFONT controlfont;

void panic(char *fmt, ...);

HWND area;
HWND edit;

LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
RECT r;

switch (msg) {
case WM_SIZE:
GetClientRect(hwnd, &r);
MoveWindow(area, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE);
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
panic("oops: message %ud does not return anything; bug in wndproc()", msg);
}

LRESULT CALLBACK areawndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg) {
case WM_MOUSEACTIVATE:
SetFocus(hwnd);
return MA_ACTIVATE;
case WM_COMMAND:
if (HIWORD(wparam) == EN_KILLFOCUS) {
MessageBeep(-1);
ShowWindow(edit, SW_HIDE);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
case WM_LBUTTONUP:
MoveWindow(edit, GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam), 100, 20, TRUE);
ShowWindow(edit, SW_SHOW);
return 0;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
panic("oops: message %ud does not return anything; bug in wndproc()", msg);
}

HWND makeMainWindow(void)
{
WNDCLASS cls;
HWND hwnd;

ZeroMemory(&cls, sizeof (WNDCLASS));
cls.lpszClassName = L"mainwin";
cls.lpfnWndProc = wndproc;
cls.hInstance = hInstance;
cls.hIcon = hDefaultIcon;
cls.hCursor = hDefaultCursor;
cls.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
if (RegisterClass(&cls) == 0)
panic("error registering window class");
hwnd = CreateWindowEx(0,
L"mainwin", L"Main Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
panic("opening main window failed");
return hwnd;
}

void buildUI(HWND mainwin)
{
#define CSTYLE (WS_CHILD | WS_VISIBLE)
#define CXSTYLE (0)
#define SETFONT(hwnd) SendMessage(hwnd, WM_SETFONT, (WPARAM) controlfont, (LPARAM) TRUE);
// build GUI here; use CSTYLE and CXSTYLE in CreateWindowEx() and call SETFONT() on each new widget

WNDCLASS cls;

ZeroMemory(&cls, sizeof (WNDCLASS));
cls.lpszClassName = L"area";
cls.lpfnWndProc = areawndproc;
cls.hInstance = hInstance;
cls.hIcon = hDefaultIcon;
cls.hCursor = hDefaultCursor;
cls.hbrBackground = (HBRUSH) (COLOR_GRADIENTACTIVECAPTION + 1);
if (RegisterClass(&cls) == 0)
panic("error registering area window class");
area = CreateWindowEx(0,
L"area", L"",
WS_CHILD | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
mainwin, NULL, hInstance, NULL);
if (area == NULL)
panic("opening main window failed");

edit = CreateWindowEx(WS_EX_CLIENTEDGE,
L"edit", L"",
WS_CHILD | ES_AUTOHSCROLL | ES_LEFT | ES_NOHIDESEL | WS_TABSTOP,
0, 0, 0, 0,
area, NULL, hInstance, NULL);
if (edit == NULL)
panic("edit creation failed");
SETFONT(edit);
}

void firstShowWindow(HWND hwnd);
void initwin(void);

int main(int argc, char *argv[])
{
HWND mainwin;
MSG msg;

initwin();

mainwin = makeMainWindow();
buildUI(mainwin);
firstShowWindow(mainwin);

for (;;) {
BOOL gmret;

gmret = GetMessage(&msg, NULL, 0, 0);
if (gmret == -1)
panic("error getting message");
if (gmret == 0)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

DWORD iccFlags =
// ICC_ANIMATE_CLASS | // animation control
// ICC_BAR_CLASSES | // toolbar, statusbar, trackbar, tooltip
// ICC_COOL_CLASSES | // rebar
// ICC_DATE_CLASSES | // date and time picker
// ICC_HOTKEY_CLASS | // hot key
// ICC_INTERNET_CLASSES | // IP address entry field
// ICC_LINK_CLASS | // hyperlink
// ICC_LISTVIEW_CLASSES | // list-view, header
// ICC_NATIVEFNTCTL_CLASS | // native font
// ICC_PAGESCROLLER_CLASS | // pager
// ICC_PROGRESS_CLASS | // progress bar
ICC_STANDARD_CLASSES | // "one of the intrinsic User32 control classes"
// ICC_TAB_CLASSES | // tab, tooltip
// ICC_TREEVIEW_CLASSES | // tree-view, tooltip
// ICC_UPDOWN_CLASS | // up-down
// ICC_USEREX_CLASSES | // ComboBoxEx
// ICC_WIN95_CLASSES | // some of the above
0;

void initwin(void)
{
INITCOMMONCONTROLSEX icc;
NONCLIENTMETRICS ncm;

hInstance = GetModuleHandle(NULL);
if (hInstance == NULL)
panic("error getting hInstance");
hDefaultIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
if (hDefaultIcon == NULL)
panic("error getting default window class icon");
hDefaultCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
if (hDefaultCursor == NULL)
panic("error getting default window cursor");
icc.dwSize = sizeof (INITCOMMONCONTROLSEX);
icc.dwICC = iccFlags;
if (InitCommonControlsEx(&icc) == FALSE)
panic("error initializing Common Controls");
ncm.cbSize = sizeof (NONCLIENTMETRICS);
if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
sizeof (NONCLIENTMETRICS), &ncm, 0) == 0)
panic("error getting non-client metrics for getting control font");
controlfont = CreateFontIndirect(&ncm.lfMessageFont);
if (controlfont == NULL)
panic("error getting control font");
}

void panic(char *fmt, ...)
{
char *msg;
TCHAR *lerrmsg;
char *fullmsg;
va_list arg;
DWORD lasterr;
DWORD lerrsuccess;

lasterr = GetLastError();
va_start(arg, fmt);
if (vasprintf(&msg, fmt, arg) == -1) {
fprintf(stderr, "critical error: vasprintf() failed in panic() preparing panic message; fmt = \"%s\"\n", fmt);
abort();
}
// according to http://msdn.microsoft.com/en-us/library/windows/desktop/ms680582%28v=vs.85%29.aspx
lerrsuccess = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, lasterr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lerrmsg, 0, NULL);
if (lerrsuccess == 0) {
fprintf(stderr, "critical error: FormatMessage() failed in panic() preparing GetLastError() string; panic message = \"%s\", last error in panic(): %ld, last error from FormatMessage(): %ld\n", msg, lasterr, GetLastError());
abort();
}
// note to self: use %ws instead of %S (thanks jon_y in irc.oftc.net/#mingw-w64)
if (asprintf(&fullmsg, "panic: %s\nlast error: %ws\n", msg, lerrmsg) == -1) {
fprintf(stderr, "critical error: asprintf() failed in panic() preparing full report; panic message = \"%s\", last error message: \"%ws\"\n", msg, lerrmsg);
abort();
}
fprintf(stderr, "%s\n", fullmsg);
va_end(arg);
exit(1);
}

void firstShowWindow(HWND hwnd)
{
// we need to get nCmdShow
int nCmdShow;
STARTUPINFO si;

nCmdShow = SW_SHOWDEFAULT;
GetStartupInfo(&si);
if ((si.dwFlags & STARTF_USESHOWWINDOW) != 0)
nCmdShow = si.wShowWindow;
ShowWindow(hwnd, nCmdShow);
if (UpdateWindow(hwnd) == 0)
panic("UpdateWindow(hwnd) failed in first show");
}

最佳答案

来自 WM_MOUSEACTIVATE 的文档:

The parent window receives this message only if the child window passes it to the DefWindowProc function

所以我会说编辑控件正在执行此操作 - 它将 WM_MOUSEACTIVATE 传递给 DefWindowProc,然后后者将消息传递给父窗口。

如果您真的想使用此方法,则需要子类化编辑控件以阻止它将消息传递给 DefWindowProc

或者,改变您的方法,改用 WM_LBUTTONDOWN

关于c - 当我点击我的子窗口时,为什么我得到 WM_MOUSEACTIVATE?我让它把焦点转移到 parent 身上,这会搞砸 child 杀死焦点逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25462989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com