当我单击子窗口时,为什么会得到WM_MOUSEACTIVATE?我将焦点更改为父对象,这会导致子对象杀死焦点逻辑

Andlabs

我有一个旨在用作编辑器视图的自定义窗口类,并且由于我直接处理键盘事件,因此我希望在顶部具有一个用于输入文本的标准编辑控件。我的问题很简单:当用户单击自定义窗口类时,我也想抓住焦点,所以我处理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");
}
乔纳森·波特(Jonathan Potter)

从文档中获取WM_MOUSEACTIVATE

仅当子窗口将其传递给DefWindowProc函数时,父窗口才会收到此消息

所以我想说edit控件正在这样做-它传递WM_MOUSEACTIVATEDefWindowProc,然后将消息传递给父窗口。

如果您确实要使用此方法,则需要对edit控件进行子类化,以使其停止将消息传递给DefWindowProc

或者,更改您的方法,而WM_LBUTTONDOWN改为使用

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档