ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

c-EnumChildWindows中MoveWindow在对话框内列表视图上方的影响:为什么列表视图标题未正确滚动

2019-10-09 12:21:25  阅读:284  来源: 互联网

标签:win32gui c scroll winapi movewindow


我有一个listview控件(lvc),它在DialogBox(dbx)内,并且dbx也具有垂直滚动条.

每当滚动条滚动时,都会调用EnumChildWindows来枚举dbx的所有子窗口.回调函数包含一个将移动该lvc的MoveWindow函数. lvc可以很好地滚动,但不能滚动其列标题,它们不随列表视图一起移动.

如果我在回调函数中注释掉MoveWindow函数,则没有任何变化. (偏离路线的lvc不会移动!)这意味着EnumChildWindow没问题,但是回调函数内部的MoveWindow引起了问题,我对此很确定,因为从回调函数外部调用MoveWindow函数可以正常工作(因为在此示例中只有一个控件,即lvc,所以我不需要枚举所有子窗口).

这是代码:

main.cpp中

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#define _WIN32_IE 0x0700
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <vector>
#include "res.h"
#define btn 0
#include <iostream>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK diaproc(HWND hwmd, UINT msg, WPARAM wp, LPARAM lp);
BOOL CALLBACK edc(HWND hwmd,LPARAM lp);
HINSTANCE gi;
int iPrevVscroll=0;
/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    gi = wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS ;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Code::Blocks Template Windows App"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
    case WM_CREATE:
        CreateWindow(WC_BUTTON, "CLICK", WS_CHILD | BS_DEFPUSHBUTTON | WS_VISIBLE, 10, 10, 80, 30, hwnd, (HMENU)btn, gi, NULL  );

    break;
    case WM_COMMAND:{
        if( LOWORD(wParam) == btn && HIWORD(wParam) == BN_CLICKED ) DialogBox(gi, MAKEINTRESOURCE(dia), hwnd,(DLGPROC)diaproc);
        DWORD err = GetLastError();
        std::cout<<err<<std::endl<<dia;
    }
    break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

BOOL CALLBACK diaproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lp)
{

    static HWND lv_hwnd;
    static int sci;
    switch(msg)
    {
    case WM_INITDIALOG:
        {

            INITCOMMONCONTROLSEX is;
            is.dwSize = sizeof(INITCOMMONCONTROLSEX);
            is.dwICC = ICC_LISTVIEW_CLASSES;
            InitCommonControlsEx(&is);

            int col_fmt[5] = { LVCFMT_CENTER, LVCFMT_LEFT, LVCFMT_CENTER, LVCFMT_CENTER, LVCFMT_CENTER };
            int col_wid[5] = { 30, 90, 50, 30, 70 };
            std::vector<TCHAR*> col_nam(5);
            col_nam[0] = _T("S.No"); col_nam[1] = _T("Description"); col_nam[2] = _T("HSN"); col_nam[3] = _T("QTY"); col_nam[4] = _T("Rate");

            lv_hwnd = CreateWindow(
                                        WC_LISTVIEW,
                                        _T(""),
                                        WS_CHILD | LVS_REPORT | LVS_EDITLABELS | WS_VISIBLE,
                                        10, 0, 300, 200,
                                        hwnd,
                                        NULL,
                                        (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                        NULL
                                        );
            ListView_SetExtendedListViewStyle(lv_hwnd, LVS_EX_FLATSB | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_LABELTIP );
            LVCOLUMN lvc;
            lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_TEXT;
            for(int i =0; i < 5; i++)
            {
                lvc.fmt = col_fmt[i];
                lvc.cx = col_wid[i];
                lvc.pszText = col_nam[i];
                lvc.iSubItem =  i;
                ListView_InsertColumn(lv_hwnd, i, &lvc);
            }

            SetScrollRange(hwnd, SB_VERT, 0, 225, TRUE);
            SetScrollPos(hwnd, SB_VERT, 0, TRUE);
        } return FALSE;

        case WM_VSCROLL:
        {
            RECT rc; GetWindowRect(lv_hwnd, &rc);
            POINT pt1 = {rc.left, rc.top}; ScreenToClient(hwnd, &pt1);
            POINT pt2 = {rc.right, rc.bottom}; ScreenToClient(hwnd, &pt2);
            std::cout<<"rc.top : "<< rc.top<<"\nrc.bottom: "<< rc.bottom <<"\nrc.right : "<<rc.right<<"\nrc.left : "<<rc.left<<"\n\n";
            std::cout<<"pt1.y : "<< pt1.y<<"\npt2.y: "<< pt2.y<<"\npt2.x : "<<pt2.x<<"\npt1.x : "<<pt1.x<<"\n\n\n";

            switch(LOWORD(wParam))
            {
            case SB_PAGEDOWN:
            case SB_LINEDOWN:
                sci += 10; break;
            case SB_PAGEUP:
            case SB_LINEUP:
                sci -= 10; break;
            case SB_THUMBTRACK:
                sci = HIWORD(wParam); break;
            };
            sci = sci < 0 ? 0 : sci > 225 ? 225 : sci;
            SetScrollPos(hwnd, SB_VERT, sci, FALSE);
            //MoveWindow(lv_hwnd, pt1.x, pt1.y - sci + iPrevVscroll, pt2.x - pt1.x, pt2.y - pt1.y, TRUE);
            EnumChildWindows(hwnd, edc, (LPARAM)sci);
        }; return TRUE;
            case WM_COMMAND:
                if(LOWORD(wParam) == IDCANCEL) EndDialog(hwnd, wParam); return TRUE;

        default: return FALSE;
    }
}

BOOL CALLBACK edc(HWND hwnd, LPARAM lp)
{
    long s = (long) lp;

    RECT rc; GetWindowRect(hwnd, &rc);
    POINT pt1 = {rc.left, rc.top}; ScreenToClient(hwnd, &pt1);
    POINT pt2 = {rc.right, rc.bottom}; ScreenToClient(hwnd, &pt2);
    MoveWindow(hwnd, pt1.x, pt1.y + s - iPrevVscroll, pt2.x - pt1.x, pt2.y - pt1.y, TRUE);

}

res.h

#define lv 1
#define dia 2

res.rc

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "res.h"

dia DIALOGEX 0,0,500,300
CAPTION "New Invoice"
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU | WS_THICKFRAME | WS_VSCROLL

FONT 8, "Ms Shell Dlg"
{

}

在main.cpp中,您可以在底部找到MoveWindow和其他相关功能.

以下图像很有用.

enter image description here
enter image description here

同样,出于说明的目的,两个MoveWindow中的滚动逻辑也不同.

最初,当我遇到此问题时,我正在一个具有许多控件的项目上工作.我分别对此进行了分析,并发现了我上面写的内容.尽管我通过采用不同的方法来向下滚动所有控件(不包括从EnumChildWindows内部调用MoveWindow的控件)来绕过此问题,但是我很好奇知道此问题的原因和解决方案.

感谢您在此篇长文章上的宝贵时间.任何建议或改进也将是惊人的!

解决方法:

EnumChildWindows() reference的备注部分:

If a child window has created child windows of its own,
EnumChildWindows enumerates those windows as well.

因此,您在这里要做的是滚动listview控件,然后分别滚动标题控件.结果是标题控件相对于listview控件移动,如第二个屏幕截图所示.

相反,您应该只移动对话框的直接子级,因为大子级将自动与其父级一起移动.

可能的解决方案:

>在EnumChildWindows()回调中检查父/子关系(例如,通过在子对象上调用GetParent()并将其与对话框句柄进行比较).
>而不是调用EnumChildWindows()和MoveWindow(),而是使用SW_SCROLLCHILDREN调用ScrollWindowEx().这是实现滚动的最简单方法,所以我更喜欢这种解决方案.

标签:win32gui,c,scroll,winapi,movewindow
来源: https://codeday.me/bug/20191009/1879097.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有