在窗口中建立TREE窗口,能够正常显示,节点点击也都正常,但就是点击几次根节点后整个树就消失了,需要重新移动整个窗口或resize窗口的时候tree才会重新出来,请问我该怎么办啊?
若是把tree放到dialog窗口里面倒是正常的,谢谢各位请帮帮忙,我已经google了一天了没结果!
case WM_CREATE:
InitCommonControls(); 
hwndTreeContent = CreateWindowEx( 
0,                          // no extended styles 
WC_TREEVIEW,                // window class 
NULL,    // text for window title bar 
WS_CHILD |        // window styles 
WS_VISIBLE|
WS_BORDER |
TVS_HASBUTTONS|
TVS_HASLINES |
TVS_LINESATROOT, 
0,             // default horizontal position 
0,             // default vertical position 
190,             // default width 
300,             // default height 
hWnd,         // no parent for overlapped windows 
(HMENU)4,        // window class menu 
hInst,               // instance owning this window 
(LPVOID) NULL        // pointer not needed 
);  insertTree(hwndTreeContent,TVI_ROOT,"root",TRUE,NULL);  //insertTree是我加节点的一个函数
insertTree(hwndTreeContent,rootitem,"child1",TRUE,NULL);
                insertTree(hwndTreeContent,rootitem,"child2",TRUE,NULL);
break;

解决方案 »

  1.   

    这段代码没问题,只是InitCommonControls最好放在创建窗口之前。还有哪些代码?
      

  2.   

    这是全部的代码,InitCommonControls这个我就是移到最开始也是同样的问题,打开窗口后点击根节点第三次,肯定消失!只有resize后才能出来,请您帮帮我啊!
    #include <windows.h>
    #include <stdio.h>
    #include <commctrl.h>HTREEITEM insertTree(HWND hTree, HTREEITEM hRoot, LPCTSTR name, bool children, LPARAM lParam);
    HWND hwndMain,hwndTreeContent;
    HINSTANCE hInst;LRESULT CALLBACK WndProc(
      HWND hwnd,      // handle to window
      UINT uMsg,      // message identifier
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    );int WINAPI WinMain(
      HINSTANCE hInstance,      // handle to current instance
      HINSTANCE hPrevInstance,  // handle to previous instance
      LPSTR lpCmdLine,          // command line
      int nCmdShow              // show state
    )
    {
    hInst = hInstance; WNDCLASSEX wndex;
    wndex.cbClsExtra = 0;
    wndex.cbSize = sizeof(WNDCLASSEX);
    wndex.cbWndExtra = 0;
    wndex.hbrBackground = (HBRUSH)(LTGRAY_BRUSH);
    wndex.hCursor = LoadCursor(hInstance,IDC_ARROW);
    wndex.hIcon = LoadIcon(NULL,IDI_QUESTION);
    wndex.hIconSm = LoadIcon(NULL,IDI_QUESTION);
    wndex.hInstance = hInstance;
    wndex.lpfnWndProc = WndProc;
    wndex.lpszClassName = "xuaisi";
    wndex.lpszMenuName = NULL;
    wndex.style = CS_HREDRAW | CS_VREDRAW; RegisterClassEx(&wndex);
    hwndMain = CreateWindow("xuaisi","Map Editor",WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN ,0,0,800,600,NULL,NULL,hInstance,NULL); ShowWindow(hwndMain,SW_SHOWNORMAL);
    UpdateWindow(hwndMain); MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return 0;
    }LRESULT CALLBACK WndProc(
      HWND hWnd,      // handle to window
      UINT uMsg,      // message identifier
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    )
    {
    HTREEITEM rootitem,roleItem;
    switch(uMsg)
    {
    case WM_CREATE:
    InitCommonControls(); 
    hwndTreeContent = CreateWindowEx( 
    0,                          // no extended styles 
    WC_TREEVIEW,                // window class 
    NULL,    // text for window title bar 
    WS_CHILD |        // window styles 
    WS_VISIBLE|
    WS_BORDER |
    TVS_HASBUTTONS|
    TVS_HASLINES |
    TVS_LINESATROOT, 
    0,             // default horizontal position 
    0,             // default vertical position 
    190,             // default width 
    300,             // default height 
    hWnd,         // no parent for overlapped windows 
    (HMENU)4,        // window class menu 
    hInst,               // instance owning this window 
    (LPVOID) NULL        // pointer not needed 
    ); 
    rootitem = insertTree(hwndTreeContent,TVI_ROOT,"地形",TRUE,NULL);
    roleItem = insertTree(hwndTreeContent,rootitem,"人物",TRUE,NULL);
    insertTree(hwndTreeContent,rootitem,"道具",TRUE,NULL);
    insertTree(hwndTreeContent,rootitem,"建筑",TRUE,NULL);
    insertTree(hwndTreeContent,roleItem,"主角",TRUE,NULL);
    insertTree(hwndTreeContent,roleItem,"NPC",TRUE,NULL); break;
    case WM_NOTIFY:

    break;
    case WM_LBUTTONDOWN:
    break;
    case WM_PAINT:
    break;
    case WM_CLOSE:
    DestroyWindow(hWnd);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
    }
    return 0;
    }
    HTREEITEM insertTree(HWND hTree, HTREEITEM hRoot, LPCTSTR name, bool children, LPARAM lParam)
    {
    TVINSERTSTRUCT tvis;
    tvis.hParent               = hRoot;
    tvis.hInsertAfter          = TVI_LAST;
    tvis.itemex.mask           = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
    tvis.itemex.hItem          = NULL;
    tvis.itemex.state          = 0;
    tvis.itemex.stateMask      = 0;
    tvis.itemex.pszText        = (LPTSTR)name;
    tvis.itemex.cchTextMax     = sizeof(tvis.itemex.pszText)/sizeof(tvis.itemex.pszText[0]);  tvis.itemex.iImage         = NULL;
    tvis.itemex.iSelectedImage = NULL;
    tvis.itemex.cChildren      = children ? TRUE : FALSE;
    tvis.itemex.lParam         = lParam;
    tvis.itemex.iIntegral      = 1; HTREEITEM hItem = TreeView_InsertItem(hTree, &tvis); return hItem;
    }
      

  3.   

    MSDN里面写的很多都是细节性的问题了,没有提到我这种问题的解决方法,另外我找了很多的code,TREEVIEW全都是建立在dialog中的,在dialog里面就不会出现问题。
      

  4.   

    switch中,把default去掉,把return DefWindowProc(hWnd,uMsg,wParam,lParam)移到外面,return 0去掉。
      

  5.   

    DefWindowProc就是处理各种消息的默认函数,你自己没有处理的消息都需要调用这个函数让系统自己来处理,很多消息即使自己处理了,也要调用这个函数。你现在遇到的问题是因为没有处理WM_PAINT消息而导致的。