1

我正在开发一个在主窗口中有一个列表视图的 Win32++ 应用程序。这是我的代码:

HWND CarsListView = NULL;

switch (message)
{
case WM_SHOWWINDOW:
    CarsListView = CreateListView(hWnd);
    ShowWindow(CarsListView, SW_SHOW);
    break;
case WM_SIZING:
    {
        if(!CarsListView)
            MessageBox(hWnd, _T("Null handle."), _T("Error"), MB_ICONERROR | MB_OK);
        RECT WindowRect;
        GetWindowRect( hWnd, &WindowRect);
        SetWindowPos(CarsListView, NULL, 0, 0, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, SWP_SHOWWINDOW); 
    }
    break;
// ...
}

CreateListView定义是这样的:

HWND CreateListView (HWND hwndParent) 
{ 
  INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
  icex.dwICC = ICC_LISTVIEW_CLASSES;
  InitCommonControlsEx(&icex);

  RECT rcClient;                       // The parent window's client area.

  GetClientRect (hwndParent, &rcClient); 

  // Create the list-view window in report view with label editing enabled.
  HWND hWndListView = CreateWindow(WC_LISTVIEW, 
    L"",
    WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
    0, 0,
    rcClient.right - rcClient.left,
    rcClient.bottom - rcClient.top,
    hwndParent,
    /*(HMENU)*/NULL,
    hInst,
    NULL); 
  return (hWndListView);
}

当窗口收到WM_SIZING时,我明白了CarsListView = NULL

我该怎么做才能让那个句柄指向我的列表视图?

4

3 回答 3

1

有两种方法可以做到这一点:“好”的方式和“坏”的方式。

  • “糟糕”的方法是简单地将局部变量声明为static,但这意味着您不能在同一进程中创建两个这种类型的窗口。
  • “好”的方法是将其存储在堆分配结构中,并使用 SetWindowLongPtr() 将指向该结构的指针存储在 Window 信息中。然后,您可以使用 GetWindowLongPtr() 检索此结构。
于 2013-09-05T08:31:40.347 回答
1

我会在 WM_CREATE 而不是在 WM_SHOWWINDOW 中创建列表视图。还要使句柄全局或静态。

或者,您也可以全局创建列表视图并将其隐藏并使其可见并在您希望它执行时设置其位置。

于 2013-09-05T08:41:11.040 回答
1

做这种事情的三种方法。

  1. 丑陋
    的将您的 CarsListView HWND 存储在static. 您不能有 2 个父窗口实例。
  2. 需要时在您的 init 和 GetWindowLongPtr 中
    使用不当。SetWindowsLongPtr(parentHWND,GWLP_USERDATA,CarsListViewHWND)速度很快,您可以拥有任意数量的实例,但如果您需要多个信息,我建议您将 astruct与您的 HWND 一起存储在内部,而不是单个 HWND 以供将来扩展。
  3. The Good ?
    Use SetProp(parentHWND,"Your Unique String",hDataHandle); its by far the more code but with that usage you can use it on every windows without caring if the USERDATA is already used or not. It's the best approach when you need to add personal property to a windows/code you can't be sure how it will be used or change over time
于 2013-09-05T08:47:34.303 回答