关闭字体对话框时窗口失去焦点

时间:2015-04-11 23:16:54

标签: c winapi

#include <windows.h>
#include <tchar.h>

LRESULT CALLBACK SecondWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_RBUTTONUP:
        {
            HFONT hFont;
            LOGFONT lf;
            CHOOSEFONT cf = {0};

            hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);

            GetObject(hFont, sizeof(LOGFONT), &lf);
            cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
            cf.hwndOwner = hwnd;
            cf.lpLogFont = &lf;
            cf.lStructSize = sizeof(CHOOSEFONT);

            if(ChooseFont(&cf))
            {
            }
        }
        break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

LRESULT CALLBACK FirstWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
    WNDCLASSEX wcFirst = {0}, wcSecond = {0};
    HWND hwndFirst, hwndSecond;
    MSG msg;

    wcFirst.cbSize = sizeof(WNDCLASSEX);
    wcFirst.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
    wcFirst.hCursor = LoadCursor(0, IDC_ARROW);
    wcFirst.hIcon = LoadIcon(0, IDI_APPLICATION);
    wcFirst.hInstance = hInstance;
    wcFirst.lpfnWndProc = FirstWndProc;
    wcFirst.lpszClassName = TEXT("FirstClass");


    wcSecond.cbSize = sizeof(WNDCLASSEX);
    wcSecond.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
    wcSecond.hCursor = LoadCursor(0, IDC_ARROW);
    wcSecond.hIcon = LoadIcon(0, IDI_APPLICATION);
    wcSecond.hInstance = hInstance;
    wcSecond.lpfnWndProc = SecondWndProc;
    wcSecond.lpszClassName = TEXT("SecondClass");


    if(!RegisterClassEx(&wcFirst))
        return 0;   

    if(!RegisterClassEx(&wcSecond))
        return 0;

    hwndFirst = CreateWindowEx(0, TEXT("FirstClass"), TEXT("First Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        700, 400, 0, 0, hInstance, 0);

    if(!hwndFirst)
        return 0;

    hwndSecond = CreateWindowEx(0, TEXT("SecondClass"), TEXT("Second Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        700, 400, hwndFirst, 0, hInstance, 0);

    if(!hwndSecond)
        return 0;


    ShowWindow(hwndFirst, nShowCmd);
    ShowWindow(hwndSecond, nShowCmd);

    while(GetMessage(&msg, 0, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

关闭字体对话框和第二个窗口后,第一个窗口失去焦点并转到底部。另一个应用程序的窗口显示在顶部。这只发生在我关闭字体对话框,然后关闭第二个窗口时。如果仅显示第一个窗口和第二个窗口,并且我关闭第二个窗口,则第一个窗口不会失去焦点。如果您要对此进行测试,请确保在测试之前,此应用程序下的其他应用程序还有另一个窗口。为什么第一个窗口失去焦点?

1 个答案:

答案 0 :(得分:0)

您可以使用MessageBox重现相同的问题。每当第二个窗口被禁用/启用时(当父窗口全部启用时)并且第二个窗口关闭,第一个窗口可能无法正常恢复。 WS_POPUP将解决此问题。

当第一个窗口保持在最顶层时,第二个窗口最小化时会出现另一个问题。您在第二个窗口中删除WS_MINIMIZEBOX以停止它。您还可以使用SetFocus(GetWindow(hwnd, GW_OWNER))重置焦点

LRESULT CALLBACK SecondWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_RBUTTONUP:
        MessageBox(hwnd, TEXT("Test"), 0, 0);
        break;

    case WM_CLOSE:
        SetFocus(GetWindow(hwnd, GW_OWNER));//force parent window to restore
        DestroyWindow(hwnd);
        break;
    //...

hwndSecond = CreateWindowEx(WS_EX_CONTROLPARENT, TEXT("SecondClass"), TEXT("Second Window"),
    WS_POPUP | WS_SYSMENU | WS_CAPTION | WS_THICKFRAME | WS_MAXIMIZEBOX,
    CW_USEDEFAULT, CW_USEDEFAULT, 700, 400, hwndFirst, 0, hInstance, 0);