调整窗口大小时,公共控件未正确绘制

Common controls are not properly painted when I resize window(调整窗口大小时,公共控件未正确绘制)

本文介绍了调整窗口大小时,公共控件未正确绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建带有子对话框作为页面的选项卡控件.

I am creating tab control with child dialog boxes as pages.

我通过 #pragma 注释启用了 Visual Styles.我也调用了 InitCommonControlsEx#pragma comment( lib, "comctl32.lib" ).

I have Visual Styles enabled via #pragma comment. I have also called InitCommonControlsEx and #pragma comment( lib, "comctl32.lib" ) as well.

最初,当窗口加载时,对话框及其常用控件具有适当的背景,请参见下图:

Initially, when window loads, dialog and its common controls have proper background, please see image below:

在调整大小时,事情不是那么一致 -> 背景开始明显不匹配.我将在下面提供截图:

During resizing things are not so consistent -> background starts to mismatches visibly. I will provide screenshot below:

您可以清楚地看到 checkboxstatic control 有不正确的背景,而在我看来 dialog box (创建作为子控件) 具有适当的背景.

You can clearly see that checkbox and static control have improper background, while it seems to me that dialog box ( created to act as a child control ) has proper background.

于 2014 年 11 月 24 日

将控件封装到group box 后,似乎没有绘画问题.我的显示器是旧的 CRT(Samsung SyncMaster 753s),我的视力不好,但似乎一切正常.窗口在调整大小时仍然闪烁可怕,但 我已尽我所能 来修复它.

After enclosing controls into group boxes there seems to be no painting problems. My monitor is old CRT ( Samsung SyncMaster 753s ), and I have bad eyesight, but again, it seems that everything paints properly. The window still flickers horribly on resize, but I have tried everything in my power to fix it.

我该如何解决这个问题?

How can I fix this?

我还没有找到任何东西,但我在输入这个问题时仍然在盯着看......

I haven't found anything yet but I am still Goggling while typing this question...

以下是用于说明问题的演示创建说明:

Here are the instructions for creating demo that illustrates the problem:

1.) 在Visual Studio中创建空的C++项目;

1.) Create empty C++ project in Visual Studio;

2.) 添加头文件,将其命名为 pomocne_funkcije.h 并复制/粘贴以下内容:

2.) add header file, name it pomocne_funkcije.h and copy/paste following:

#include <windows.h>
#include <windowsx.h>
#include <comutil.h>
#include <commctrl.h>
#include <stdio.h>
#include <vector>
#include <ole2.h>
#include <string>
#include <stdlib.h>
#include <locale.h>
#include <Uxtheme.h>

#pragma comment( linker, "/manifestdependency:"type='win32' 
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' 
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' 
language='*'"")

#pragma comment( lib, "comctl32.lib")
#pragma comment( lib,"Msimg32.lib")
#pragma comment( lib, "comsuppw.lib")
#pragma comment( lib, "UxTheme.lib")

3.) 在资源编辑器中创建对话框,添加复选框静态控件.

3.) Create dialog box in resource editor, add checkbox static control.

为对话框设置以下内容:

Set the following for dialog box:

  • 边框:无
  • 控制:真
  • 控制父级:true
  • 风格:儿童
  • 系统菜单:错误

4.) 这是 main.cpp 的代码:

4.) Here is the code for main.cpp :

#include "pomocne_funkcije.h"

static HINSTANCE hInst;

// dialog procedure for firts tab
INT_PTR CALLBACK Ugovori(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        {
            EnableThemeDialogTexture( hDlg, ETDT_ENABLETAB );
        }
        return (INT_PTR)TRUE;
    }
    return (INT_PTR)FALSE;
}

// main window procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HWND hDlgFirstTab;  // handle to the first page dialog box
    switch(msg)
    {
    case WM_CREATE:
        {
            RECT rcClient = {0};
            ::GetClientRect( hwnd, &rcClient );

            HWND hwndTab = CreateWindowEx( 0, WC_TABCONTROL, 
                L"Ugovori", WS_CHILD | WS_VISIBLE, 
                10, 10, rcClient.right - rcClient.left - 20,
                rcClient.bottom - rcClient.top - 63, 
                hwnd, (HMENU)3000, 
                ((LPCREATESTRUCT)lParam)->hInstance, 0 );

            TCITEM tci = {0};

            tci.mask = TCIF_TEXT;
            tci.pszText = L"Основни подаци";
            TabCtrl_InsertItem( hwndTab, 0, &tci );

            // set font so cyrilic symbols can be properly displayed instead of ???
            SendMessage( hwnd, WM_SETFONT, 
                (WPARAM)(HFONT)GetStockObject(DEFAULT_GUI_FONT),
                (LPARAM)TRUE );

            SendMessage( hwndTab, WM_SETFONT, 
                (WPARAM)(HFONT)GetStockObject(DEFAULT_GUI_FONT),
                (LPARAM)TRUE );

            // create page ( dialog box )
            hDlgFirstTab = CreateDialog( ((LPCREATESTRUCT)lParam)->hInstance,
                MAKEINTRESOURCE(IDD_DIALOG1), 
                hwnd, 
                (DLGPROC)Ugovori );  // dialog procedure 

            ShowWindow( hDlgFirstTab, SW_SHOW );

        }
        return 0L;
    case WM_MOVE:
    case WM_MOVING:
    case WM_SIZING:
    case WM_SIZE:
        {
            RECT rcClient = {0};
            GetClientRect( hwnd, &rcClient );

            SetWindowPos( GetDlgItem( hwnd, 3000 ), NULL, 
                rcClient.left + 10, // move it away from window edge by 10 pixels
                rcClient.top + 10,  // move it away from window edge by 10 pixels
                rcClient.right - rcClient.left - 20,  
                // - 63 was the size of the button, 
                // but I have deleted that button here to preserve space
                rcClient.bottom - rcClient.top - 63, 
                SWP_NOZORDER | SWP_NOCOPYBITS );

            // get tab control's client rectangle
            GetClientRect( GetDlgItem( hwnd, 3000 ), &rcTab );

            //============= place dialog box into tab's client area
            MapWindowPoints( GetDlgItem( hwnd, 3000 ), hwnd, 
                (LPPOINT)(&rcTab), 2 );
            // get tab's display area
            TabCtrl_AdjustRect( GetDlgItem( hwnd, 3000 ), 
                FALSE, &rcTab );
            // move dialog box
            SetWindowPos(hDlgFirstTab, NULL, 
                rcTab.left, rcTab.top,
                rcTab.right - rcTab.left, 
                rcTab.bottom - rcTab.top, 
                SWP_NOZORDER);

            //========================= done
            // repaint window
            InvalidateRect( hwnd, NULL, FALSE );
        }
        return 0L;
    case WM_ERASEBKGND:
        return 1L;
    case WM_PAINT:
        {
            PAINTSTRUCT ps = {0};
            HDC hdc = BeginPaint( hwnd, &ps );
            SendMessage( hwnd, WM_PRINTCLIENT, (WPARAM)hdc, 0 );
            EndPaint( hwnd, &ps );
        }
        return 0L;
    case WM_PRINTCLIENT:
        {
            RECT rcClient = {0};
            GetClientRect( hwnd, &rcClient );

            FillRect( (HDC)wParam, &rcClient, 
                (HBRUSH)GetStockObject(WHITE_BRUSH) );
        }
        return 0L;
    case WM_CLOSE:
        ::DestroyWindow(hwnd);
        return 0L;
    case WM_DESTROY:
        ::PostQuitMessage(0);
        return 0L;
    default:
        return ::DefWindowProc( hwnd, msg, wParam, lParam );
    }
    return 0;
}

// WinMain

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
                   int nCmdShow)
{
    // store hInstance in global variable for later use

    hInst = hInstance;

    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    // initialize common controls

    INITCOMMONCONTROLSEX iccex;
    iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccex.dwICC = ICC_LISTVIEW_CLASSES | ICC_UPDOWN_CLASS | 
       ICC_STANDARD_CLASSES | ICC_TAB_CLASSES;
    InitCommonControlsEx(&iccex);

    // register main window class

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInst;
    wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = NULL;
    wc.lpszClassName = L"Main_Window";
    wc.hIconSm = LoadIcon( hInstance, IDI_APPLICATION );

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, 
            L"Window Registration Failed!", L"Error!", 
            MB_ICONEXCLAMATION | MB_OK);

        return 0;
    }

    // create main window

    hwnd = CreateWindowEx( 0, L"Main_Window", 
        L"Contract manager", 
        WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        NULL, NULL, hInstance, 0 );

    if(hwnd == NULL)
    {
        MessageBox(NULL, L"Nemogu da napravim prozor!", L"Greska!",
            MB_ICONEXCLAMATION | MB_OK);

        return 0; 
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

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

    return Msg.wParam;
}

我正在使用 C++WinAPIWindows XP 上的 Visual Studio 2008 中工作.

I am working in Visual Studio 2008 on Windows XP using C++ and WinAPI.

推荐答案

您的选项卡对话框按 Z 顺序位于选项卡控件下方.这会在第一次调整大小时导致疯狂的过度绘制问题.将选项卡对话框作为子窗口添加到主窗口后,调用 SetWindowPos(tab, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE) 将它们移动到 Z 顺序的顶部.

Your tab dialogs are below the tab control in the Z order. This causes crazy overdraw issues on first resizing. After adding the tab dialogs as childs to your main window, call SetWindowPos(tab, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE) to move them to the top of the Z order.

有这个确切的问题;我花了好几天才弄明白.我在 Visual Studio 中创建了一个简单的应用程序:

Had this exact issue; took me days to figure it out. I created a simple application in Visual Studio:

  1. 对 ComCtrl v 6 的清单引用
  2. InitCommonControlsEx()
  3. 带有标签控件的对话框
  4. 选项卡内容的子对话框(与选项卡同级)
  5. 在子对话框中,一个单选按钮和一个静态按钮
  6. 没有花哨的处理 WM_PRINTCLIENT 或类似的东西
  7. 只需EnableThemeDialogTexture()在子对话框的WM_INITDIALOG
  1. manifest reference to ComCtrl v 6
  2. InitCommonControlsEx()
  3. a dialog with a tab control
  4. a sub-dialog for the tab content (sibling to the tab)
  5. in the sub-dialog, a radio button and a static
  6. no fancy handling of WM_PRINTCLIENT or anything alike
  7. just EnableThemeDialogTexture() in WM_INITDIALOG of the sub-dialog

我在 XP 上检查了一下……它运行良好,看起来很漂亮,除了闪烁.

I went to check it on XP and … it worked perfect and looked beautiful, apart from flickering.

我添加了 WS_CLIPCHILDREN 并且突然能够完美地重现您的屏幕截图.我一开始以为是这个原因.

I added WS_CLIPCHILDREN and suddenly was able to reproduce your screenshots perfectly. I was first thinking this was the cause.

我一直推并添加了 WS_COMPOSITED 以防止闪烁,标签窗口突然消失了——完全被标签的背景覆盖了.现在我意识到 Z 顺序有问题.

I kept pushing and added WS_COMPOSITED to prevent flickering, and the tab window suddenly was gone – entirely covered by the tab’s background. Now I realized the Z order was at fault.

子窗口总是在 Z 顺序的底部创建,即在您的选项卡控件下方.您的代码永远不会将它们向上移动.选项卡仅由纯粹的运气/透支显示,产生您观察到的工件.修复 Z 顺序,就不会再有工件了.

Child windows are always created at the bottom of the Z order, i.e. below your tab control. Your code never moves them upwards. The tabs only ever displayed by pure luck/overdraw, producing the artifacts you observe. Fix the Z order and there will be no more artifacts.

这篇关于调整窗口大小时,公共控件未正确绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:调整窗口大小时,公共控件未正确绘制