Hiding/Showing Child Windows of a Tab Control Win32
To be clear, I am coding in win32 and am not using MFC, wxWidgets or .net.
My issue is that I have a tab control with 2 tabs. For debugging purposes, each tab has a single STATIC
window. When initialising, the following code is run:
createTabControl();
CreateStaticViewTab1();
CreateStaticViewTab1();
ShowWindow(Task1Tab, SW_SHOW);
Where
void createTabControl(){
TCITEM tie = { 0 };
hWndInputTab = CreateWindow(WC_TABCONTROL, L"Input", WS_VISIBLE | WS_CHILD , 10, 40, 300, 650, hWnd, (HMENU)TAB_INPUT, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
SetDefaultFont(hWndInputTab);
tie.mask = TCIF_TEXT;
TCHAR pszTab1[] = L"TAB 1";
tie.pszText = pszTab1;
TabCtrl_InsertItem(hWndInputTab, 0, &tie)
TCHAR pszTab2[] = L"TAB 2";
tie.pszText = pszTab2;
TabCtrl_InsertItem(hWndInputTab, 1, &tie)
}
void CreateStaticViewTab1(){
Task1Tab = CreateWindowEx(0,L"STATIC",L"Static Control on Tab1",WS_CHILD | WS_BORDER | SS_CENTER | SS_CENTERIMAGE, 75, 75, 200, 60, hWndInputTab,NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),NULL);
SetDefaultFont(Task1Tab);
}
void CreateStaticViewTab2(){
Task1Tab = CreateWindowEx(0,L"STATIC",L"Static Control on Tab2",WS_CHILD | WS_BORDER | SS_CENTER | SS_CENTERIMAGE, 75, 75, 200, 60, hWndInputTab,NULL, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),NULL);
SetDefaultFont(Task2Tab);
}
In the WndProc
, I am handling the WM_NOTIFY
message. I have checked and can confirm that it executes correctly, calling the appropriate functions ( ShowTab1()
and ShowTab2()
).
These two functions are designed to show and hide the appropriate tabs when the selection changes. They do so by the following:
void ShowTab1(){
ShowWindow(Task2Tab, SW_HIDE);
ShowWindow(Task1Tab, SW_SHOW);
}
void ShowTab2(){
ShowWindow(Task1Tab, SW_HIDE);
ShowWindow(Task2Tab, SW_SHOW);
}
These should - as I understand it - hide and show the correct tabs as appropriate.
My issue is that when the program is loaded, the correct tab (1) is shown. When tab 2 is clicked, the content of tab 1 ( Task1Tab
) is correctly hidden, however, the content of tab 2 ( Task2Tab
) is not correctly shown. When returning to tab 1, Task1Tab
is correctly shown.
I cannot identify why there is a difference between the two pieces of code, as they are virtually identical.
nb. I have tested the code without the ShowWindow(Task1Tab, SW_SHOW)
in the initialisation, this creates the tabs without any content and when tab 2 is clicked, nothing is shown, however, when tab 1 is clicked, Task1Tab
is correctly shown.
You appear to have been caught out by some routine copy/paste errors. You call CreateStaticViewTab1
twice and never call CreateStaticViewTab2
. And both CreateStaticViewTab1
and CreateStaticViewTab2
assign to Task1Tab
. These mistakes would explain the behaviour that you observe.
Were your code to have performed any error checking, then that would have led you to the problem. Checking for errors would have revealed that ShowWindow(Task2Tab, ...)
was failing because of an invalid window handle. So, another important lesson, beyond taking more care with the clipboard, is that you should always check for errors when calling Windows API functions.
明显的问题 - 从未调用过CreateStaticViewTab2()
。
下一篇: 隐藏/显示选项卡控件Win32的子窗口