调整窗口大小时,常用控件未正确绘制

时间:2014-11-23 03:55:11

标签: c++ winapi tabs common-controls visual-styles

引言:

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

我通过Visual Styles评论启用了#pragma。我也打电话给InitCommonControlsEx#pragma comment( lib, "comctl32.lib" )

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

enter image description here

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

enter image description here

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

于2014年11月24日编辑:

将控件封入group boxes之后,似乎没有绘画问题。我的显示器是旧的CRT(三星SyncMaster 753s),我的视力不好,但同样,似乎一切都画得很好。窗口在调整大小时仍然可怕地闪烁,但是I have tried everything in my power来修复它。

问题:

我该如何解决这个问题?

我努力解决这个问题:

我还没找到任何东西,但在输入这个问题时我仍然是Goggling ......

相关信息:

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

1。)在C++;

中创建空的Visual Studio项目

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

#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.。)在资源编辑器中创建对话框,添加复选框静态控件。

为对话框设置以下内容:

  • 边境:无
  • 控制:真实
  • 控制父级:true
  • 风格:孩子
  • 系统菜单:false

4.。)以下是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;
}

我正在Visual Studio 2008使用Windows XPC++WinAPI工作。

1 个答案:

答案 0 :(得分:3)

您的选项卡对话框按Z顺序位于选项卡控件下方。这会在第一次调整大小时引起疯狂的透支问题。将选项卡对话框作为子级添加到主窗口后,调用SetWindowPos(tab, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE)将其移至Z顺序的顶部。

有这个确切的问题;花了我几天时间才弄清楚。我在Visual Studio中创建了一个简单的应用程序:

  1. 对ComCtrl v 6的清单参考
  2. InitCommonControlsEx()
  3. 带有标签控件的对话框
  4. 标签内容的子对话框(同级标签)
  5. 在子对话框中,一个单选按钮和一个静态
  6. 没有WM_PRINTCLIENT或类似事物的花哨处理
  7. 恰好在子对话框EnableThemeDialogTexture()中的WM_INITDIALOG

我去XP上进行了检查,……除了闪烁之外,它工作得很好而且看起来很漂亮。

我添加了WS_CLIPCHILDREN,突然之间就可以完美地复制您的屏幕截图。我首先想到的是原因。

我不断推动并添加了WS_COMPOSITED以防止闪烁,然后标签页窗口突然消失了-完全被标签的背景所覆盖。现在我意识到Z顺序有错。

子窗口始终创建在Z顺序的底部,即在标签控件下方。您的代码永远不会使它们向上移动。仅靠运气/透支显示这些选项卡,从而产生您观察到的伪影。固定Z顺序,将不再有伪像。