2013-01-16 38 views
0

我一直在使用選項卡控件,它只在winMain中發佈時纔可見。當我嘗試在WM_CREATE消息句柄中創建選項卡時,什麼都不顯示。這裏是我的代碼:Winapi win32編程 - 選項卡控件沒有顯示

#define WIN32_LEAN_AND_MEAN 

#define IDC_STATIC      -1 
#define ID_FILE_EXIT     101 
#define ID_HELP_ABOUT     102 
#define ID_B_BLANK      1001 
#define ID_B_HOME      1002 
#define ID_B_SAVE      1003 

#include <strsafe.h> 
#include <windows.h> 
#include <CommCtrl.h> 
#include <iostream> 
#include <ShlObj.h> 
#include <cstring> 

HWND main_hwnd, tab_hwnd, tab0_hwnd, tab1_hwnd, tab2_hwnd, tab3_hwnd, tab4_hwnd; 
HINSTANCE tab_hinst; 
TCHAR HomePath[MAX_PATH*2], SavePath[MAX_PATH*2]; 
WNDCLASSEX wc; 
bool success=FALSE; 
MSG msg; 
HWND button; //button1, button2; 
static HWND text; // text1, text2, text3, text4; 
TCITEM tie; 
int focus = 0, NotifyCase = 0; 

void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code 

    LPVOID lpMsgBuf; 
    LPVOID lpDisplayBuf; 
    DWORD dw = GetLastError(); 

    FormatMessage(
     FORMAT_MESSAGE_ALLOCATE_BUFFER | 
     FORMAT_MESSAGE_FROM_SYSTEM | 
     FORMAT_MESSAGE_IGNORE_INSERTS, 
     NULL, 
     dw, 
     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
     (LPTSTR) &lpMsgBuf, 
     0, NULL); 

    // Display the error message and exit the process 

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
     (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
     LocalSize(lpDisplayBuf)/sizeof(TCHAR), 
     TEXT("%s failed with error %d: %s"), 
     lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf); 
    LocalFree(lpDisplayBuf); 
    ExitProcess(dw); 
} 

// Step 4: The Window Procedure 
LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch(Msg) 
    { 
     case WM_CREATE: 
      { 
       tab_hwnd = CreateWindowEx(
        0, 
        "SysTabControl32", 
        "", 
        WS_CHILD|WS_CLIPSIBLINGS, 
        CW_USEDEFAULT, CW_USEDEFAULT, 681, 338, 
        main_hwnd, NULL, tab_hinst, NULL); 

       tab0_hwnd = CreateWindowEx(
        0, 
        "SysTabControl32", 
        "", 
        WS_CHILD|WS_CLIPSIBLINGS, 
        1, 22, 679, 315, 
        tab_hwnd, NULL, tab_hinst, NULL); 

       text = CreateWindow("Static","Home Directory",WS_CHILD | WS_VISIBLE,0,50,150,50,tab0_hwnd, 0, tab_hinst,0); 
       text = CreateWindow("Static","C:\\",WS_CHILD | WS_VISIBLE,200,50,150,50,tab0_hwnd, 0, tab_hinst,0); 
       button = CreateWindow("Button","Navigate to...", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,350,42,100,25,tab0_hwnd,(HMENU)ID_B_HOME,tab_hinst,0); 
       text = CreateWindow("Static","Save Directory",WS_CHILD | WS_VISIBLE,0,100,150,50,tab0_hwnd, 0, tab_hinst,0); 
       text = CreateWindow("Static","C:\\",WS_CHILD | WS_VISIBLE,200,100,150,50,tab0_hwnd, 0, tab_hinst,0); 
       button = CreateWindow("Button","Save to...", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,350,92,100,25,tab0_hwnd,(HMENU)ID_B_SAVE,tab_hinst,0); 

       tab1_hwnd = CreateWindowEx(
        0, 
        "SysTabControl32", 
        "", 
        WS_CHILD|WS_CLIPSIBLINGS, 
        1, 22, 679, 315, 
        tab_hwnd, NULL, tab_hinst, NULL); 

       tab2_hwnd = CreateWindowEx(
        0, 
        "SysTabControl32", 
        "", 
        WS_CHILD|WS_CLIPSIBLINGS, 
        1, 22, 679, 315, 
        tab_hwnd, NULL, tab_hinst, NULL); 

       tab3_hwnd = CreateWindowEx(
        0, 
        "SysTabControl32", 
        "", 
        WS_CHILD|WS_CLIPSIBLINGS, 
        1, 22, 679, 315, 
        tab_hwnd, NULL, tab_hinst, NULL); 

       tab4_hwnd = CreateWindowEx(
        0, 
        "SysTabControl32", 
        "", 
        WS_CHILD|WS_CLIPSIBLINGS, 
        1, 22, 679, 315, 
        tab_hwnd, NULL, tab_hinst, NULL); 

       tie.mask = TCIF_TEXT; 
       tie.pszText = "Paths"; 
       TabCtrl_InsertItem(tab_hwnd, 0, &tie); 
       tie.pszText = "Output"; 
       TabCtrl_InsertItem(tab_hwnd, 1, &tie); 
       tie.pszText = "Parameters"; 
       TabCtrl_InsertItem(tab_hwnd, 2, &tie); 
       tie.pszText = "Configurations"; 
       TabCtrl_InsertItem(tab_hwnd, 3, &tie); 
       tie.pszText = "Run"; 
       TabCtrl_InsertItem(tab_hwnd, 4, &tie); 

       HMENU hMenu, hSubMenu; 

       hMenu = CreateMenu(); 

       hSubMenu = CreatePopupMenu(); 
       AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File"); 
       AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "&Exit"); 

       hSubMenu = CreatePopupMenu(); 
       AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help"); 
       AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&About"); 

       SetMenu(hwnd, hMenu); 
      } 
      break; 
     case WM_NOTIFY: 
      switch(((LPNMHDR)lParam) ->code) 
      { 
       case TCN_SELCHANGING: 
        switch(TabCtrl_GetCurFocus(tab_hwnd)) 
        { 
         case 0: 
          ShowWindow(tab0_hwnd,SW_HIDE); 
          break; 
         case 1: 
          ShowWindow(tab1_hwnd,SW_HIDE); 
          break; 
         case 2: 
          ShowWindow(tab2_hwnd,SW_HIDE); 
          break; 
         case 3: 
          ShowWindow(tab3_hwnd,SW_HIDE); 
          break; 
         case 4: 
          ShowWindow(tab4_hwnd,SW_HIDE); 
          break; 
         default: 
          return DefWindowProc(hwnd, Msg, wParam, lParam); 
          break; 
        } 
        break; 
       case TCN_SELCHANGE: 
        switch(TabCtrl_GetCurFocus(tab_hwnd)) 
        { 
         case 0: 
          ShowWindow(tab0_hwnd,SW_SHOW); 
          break; 
         case 1: 
          ShowWindow(tab1_hwnd,SW_SHOW); 
          break; 
         case 2: 
          ShowWindow(tab2_hwnd,SW_SHOW); 
          break; 
         case 3: 
          ShowWindow(tab3_hwnd,SW_SHOW); 
          break; 
         case 4: 
          ShowWindow(tab4_hwnd,SW_SHOW); 
          break; 
         default: 
          return DefWindowProc(hwnd, Msg, wParam, lParam); 
          break; 
        } 
        break; 
       default: 
        return DefWindowProc(hwnd, Msg, wParam, lParam); 
      } 
      break; 
     case WM_COMMAND: 
      switch(LOWORD(wParam)) 
      { 
       case ID_FILE_EXIT: 
        PostMessage(hwnd, WM_CLOSE, 0, 0); 
        break; 
       case ID_HELP_ABOUT: 
        return MessageBox(0, "This program is an alteration of ...", "About", MB_OK); 
        //return DefWindowProc(hwnd2,0,0,0); 
       case ID_B_HOME: 
        MessageBox(tab0_hwnd,"HERE","OKAY",NULL); 
        break; 
       case ID_B_SAVE: 
        break; 
       case ID_B_BLANK: 
        break; 
       default: 
        return DefWindowProc(hwnd, Msg, wParam, lParam); 
      } 
      break; 
     case WM_CLOSE: 
      DestroyWindow(hwnd); 
      break; 
     case WM_DESTROY: 
      PostQuitMessage(0); 
      break; 
     default: 
      return DefWindowProc(hwnd, Msg, wParam, lParam); 
    } 
    return 0; 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow) 
{ 
    tab_hinst = hInstance; 

    //Step 1: Registering the Window Class 
    wc.cbSize  = sizeof(WNDCLASSEX); 
    wc.style   = 0; 
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance  = hInstance; 
    wc.hIcon   = LoadIcon(NULL, IDI_APPLICATION); 
    wc.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = "Home"; 
    wc.hIconSm  = LoadIcon(NULL, IDI_APPLICATION); 

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

    // Step 2: Creating the Window 
    main_hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE, 
     "Home", 
     "The Home page", 
     WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, 
     CW_USEDEFAULT, CW_USEDEFAULT, 700, 400, 
     NULL, NULL, hInstance, NULL); 

    if(main_hwnd == NULL) { 
     MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    } 

    ShowWindow(main_hwnd, nCmdShow); 
      ShowWindow(tab_hwnd, SW_SHOW); 
      ShowWindow(tab0_hwnd, SW_SHOW); 
    UpdateWindow(main_hwnd); 

    // Step 3: The Message Loop 
    do{ 
     PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE); 
     /* Translate virtual-key messages into character messages */ 
     TranslateMessage(&msg); 
     /* Send message to WindowProcedure */ 
     DispatchMessage(&msg); 
    } while(msg.message!=WM_CLOSE && msg.message!=WM_QUIT); 
    return msg.wParam; 
} 

我相信,代碼應該是如下的過程如下: 1.創建主窗口main_hwnd 2.轉到WM_CREATE並運行該代碼 3.在這一點,創建tab_hwnd,tab0_hwnd,...,tab4_hwnd 4.然後它返回到winMain,並顯示窗口。之後它到達循環。

+1

在我進入MFC之前,我想先更好地理解和學習win32 winApi。另外,我的系統還沒有爲MFC設置。 – hherbol

回答

1

你main_hwnd之後CreateWindowEx收益分配,因此它總是NULLWM_CREATE交付。您可以使用WndProchwnd參數而不是main_hwnd

通過此修改,如果我還將InitCommonControls()添加到WinMain(與comctl32庫鏈接),它對我有用。

+0

非常感謝!這是完美的。但現在又出現了一個老問題:按鈕不起作用。在繼續尋求幫助之前,我想知道StackOverflow的正確過程是什麼。我一直在網上搜索4天這個按鈕的事情,並沒有找到任何幫助。我是否應該編輯此線程並在此處尋求幫助或創建另一個線程? – hherbol

+0

儘管此修復程序有效,但在每個「tab#_hwnd」後面都會顯示錯誤,指出該句柄無效。爲什麼會發生? – hherbol

+0

通常,創建另一個線程(並記住你正在與新的觀衆交談,而不是在之前的問題中處於「上下文」中的人)。在這裏,我可以立即說:'WM_COMMAND'被髮送到*父窗口,而不是祖父母,所以你要麼必須繼承你的標籤控件的子類,要麼讓你的按鈕把你的主窗口作爲父窗口。也許有更好的辦法,我不確定。 (順便說一句,你應該總是檢查'BN_CLICKED'通知,過濾掉'BN_SETFOCUS'和朋友) –