Winapi ListView can't prevent columns from resizing

做~自己de王妃 提交于 2019-12-06 11:11:06

问题


I'm trying to create ListView with fixed width columns in winapi C++ project.

I try use a trick with handling a HDN_BEGINTRACK notification in Dialog Box Procedure just by returning TRUE in it. As I understood from different articles it might work.

Point is, that I catch it, but returning TRUE does not prevent from resizing.

Also tried returning TRUE just after getting WM_NOTIFY - the same effect.

Help me please. Here are some parts of code:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance;
   ----
   hDlg = CreateDialog(hInst,MAKEINTRESOURCE(IDD_DIALOG),
                       hWnd,(DLGPROC)DlgProc);

   INITCOMMONCONTROLSEX icex; 
   icex.dwICC = ICC_LISTVIEW_CLASSES;
   InitCommonControlsEx(&icex);

   HWND hWndListView =   CreateWindow(
                                    WC_LISTVIEW, 
                                    L"",
                                    WS_CHILD | LVS_REPORT |WS_VISIBLE,
                                    0, 0,
                                    382,
                                    200,
                                    hDlg,
                                    (HMENU)IDC_LIST,
                                    hInst,
                                    NULL);
   ----
   //adding columns and items
   ----
   DWORD exStyle = LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
   ListView_SetExtendedListViewStyle(hWndListView,exStyle);
   SetWindowTheme(hWndListView, L"Explorer", NULL);
   ----
   return TRUE;
}

BOOL CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
   switch (Msg) 
   {
   ----
   case WM_NOTIFY:
       switch(((LPNMHDR)lParam)->code)
       {
       case HDN_BEGINTRACK:
           OutputDebugString(L"HDN_BEGINTRACK\n");
           return TRUE;
       default:
           break;
       }
   ----
   }
   return FALSE;
}

Thanks in advance!


回答1:


Returning TRUE from WM_NOTIFY in a dialog procedure only shows you've processed the message. To actually return a result value, you must also set the dialog's DWL_MSGRESULT:

case HDN_BEGINTRACK:
    OutputDebugString(L"HDN_BEGINTRACK\n");
    SetWindowLong(hDlg, DWL_MSGRESULT, TRUE);  // prevent resizing
    return TRUE;  // message processed; check DWL_MSGRESULT for real result


来源:https://stackoverflow.com/questions/13325887/winapi-listview-cant-prevent-columns-from-resizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!