2013-10-10 47 views
2

我下載了一個非常基本的示例Win32應用程序,並且希望爲其添加樹視圖。這是我的WinMain目前。Win32創建TreeView

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { 
    WNDCLASSEX wc; 
    HWND hwnd; 
    MSG Msg; 

    wc.cbSize  = sizeof(WNDCLASSEX); 
    wc.style   = 0; 
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance  = hInstance; 
    wc.hIcon   = NULL; 
    wc.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = g_szClassName; 
    wc.hIconSm  = NULL; 

    if(!RegisterClassEx(&wc)) { 
     MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    } 

    hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE, 
     g_szClassName, 
     "My Window", 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, 
     NULL, NULL, hInstance, NULL); 

    if(hwnd == NULL) { 
     MessageBox(NULL, "Window Creation Failed!", "Error!", 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; 
} 

它的工作原理與普通窗口類似。

我已經閱讀了很多關於創建樹視圖的頁面,但對於像我這樣的noob而言,這並不明顯。我改變了我的類CreateWindowEx函數是WC_TREEVIEW,所以我的WinMain現在看起來是這樣的:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { 
    WNDCLASSEX wc; 
    HWND hwnd; 
    MSG Msg; 

    wc.cbSize  = sizeof(WNDCLASSEX); 
    wc.style   = 0; 
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance  = hInstance; 
    wc.hIcon   = NULL; 
    wc.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = g_szClassName; 
    wc.hIconSm  = NULL; 

    /*if(!RegisterClassEx(&wc)) { 
     MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    }*/ 

    hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE, 
     /*g_szClassName*/WC_TREEVIEW, 
     "My Window", 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, 
     NULL, NULL, hInstance, NULL); 

    if(hwnd == NULL) { 
     MessageBox(NULL, "Window Creation Failed!", "Error!", 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; 
} 

的問題是,現在,該窗口將不會在我的Windows主題加載,關閉按鈕看起來不一樣。另外,我的菜單不顯示。

下面是創建樹視圖中的代碼(樹視圖顯示不出來):

struct treeView { 
    HWND hwnd; 
    TV_INSERTSTRUCT insert; 
    HTREEITEM parent; 
    HTREEITEM before; 
    HTREEITEM root; 
}; 

case WM_CREATE: 
{ 
    struct treeView resourcesTreeView; 

    resourcesTreeView.hwnd = GetDlgItem(hwnd, ID_RESOURCES_TREE_VIEW); 
    resourcesTreeView.insert.hParent = NULL; 
    resourcesTreeView.insert.hInsertAfter = TVI_ROOT; 
    resourcesTreeView.insert.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE; 
resourcesTreeView.insert.item.pszText = "Parent"; 
    resourcesTreeView.insert.item.iImage = 0; 
    resourcesTreeView.insert.item.iSelectedImage = 1; 
    resourcesTreeView.parent = (HTREEITEM)SendDlgItemMessage(hwnd, ID_RESOURCES_TREE_VIEW, TVM_INSERTITEM, 0, (LPARAM)&resourcesTreeView.insert); 
    resourcesTreeView.root = resourcesTreeView.parent; 
    resourcesTreeView.before = resourcesTreeView.parent; 
    UpdateWindow(hwnd); 
} 

我已經確定要包括:

case WM_INITDIALOG: 
{ 
    /*INITCOMMONCONTROLSEX icc; 
    icc.dwSize = sizeof(icc); 
    icc.dwICC = ICC_WIN95_CLASSES; 
    InitCommonControlsEx(&icc);*/ 
    InitCommonControls(); 
} 

回答

2

WM_INITDIALOG只發送到對話,但你正在手動創建你的窗口。在嘗試創建樹形控件之前,您應該將InitCommonControls()的電話放入您的WinMain()函數中。

像樹一樣的控件不能很好地用作頂級窗口,這正是您要做的。把你的窗口類回是怎麼回事,讓你有一個工作窗口,然後呼叫後添加以下ShowWindow()

RECT rc; 
GetClientRect(hwnd, &rc); 

HWND hwndTree = CreateWindowEx(
    WS_EX_CLIENTEDGE, 
    WC_TREEVIEW, 
    0, 
    WS_CHILD | WS_VISIBLE, 
    0, 0, rc.right, rc.bottom, 
    hwnd, NULL, hInstance, NULL); 

這應該給你一個樹形控件作爲頂部的孩子級窗口。然後,您可以通過發送消息到hwndTree開始隨時添加項目。

+0

非常感謝,愛! – user2868331