WinAPI win32 Programming - Button not visible - c

I have used GetLastError() and FormatMessage() to determine that the button is successfully being created. However, it is not visible when I run the program. I originally had buttons created in winMain, but found posts online stating NOT to do this, so I commented it out and added the creations in WM_CREATE. Note, all ID's, etc are defined in a resource.h file, and void ErrorExit() was taken from MSDN to make using GetLastError() and FormatMessage() easy on a relatively new winApi programmer like me. My code is as follows:
#define WIN32_LEAN_AND_MEAN
#define _WIN32_IE 0x0900
#define _WIN32_WINNT 0x0900
#include <strsafe.h>
#include <windows.h>
#include <CommCtrl.h>
#include <iostream>
#include <ShlObj.h>
#include <cstring>
#include "resource.h"
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;
static HWND text;
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:
{
if(tab0_hwnd!=NULL)
button = CreateWindow("Button","Navigate to...", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,350,42,100,25,tab0_hwnd,(HMENU)ID_B_HOME,(HINSTANCE)GetWindowLong(tab0_hwnd, GWL_HINSTANCE),0);
/*if(tab0_hwnd!=NULL)
button = CreateWindow("Button","Save to...", BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,350,92,100,25,tab0_hwnd,(HMENU)ID_B_SAVE,(HINSTANCE)GetWindowLong(tab0_hwnd, GWL_HINSTANCE), NULL);
*/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);
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);
if(main_hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(main_hwnd, nCmdShow);
ShowWindow(tab_hwnd, nCmdShow);
ShowWindow(tab0_hwnd,nCmdShow);
UpdateWindow(main_hwnd);
UpdateWindow(tab_hwnd);
UpdateWindow(tab0_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;
}
Also I'm curious about UpdateWindow(), do I need to call this if I'm not painting?

At WM_CREATE description we read that the message is sent before the function returns.
Hence WM_CREATE for main_hwnd always happens before tab0_hwnd is assigned.
Now let's look at your code handling WM_CREATE:
if(tab0_hwnd!=NULL)
/* you would create the button here, but it does not happen. */
Now, wouldn't it be better to produce an error message if tab0_hwnd is NULL, instead of silently ignoring the error?
It's not necessarily the only problem with the code, but I won't go further without a compilable example.

The call to create the button in the WM_CREATE case of WndProc runs before the the tab0_hwnd window is created. CreateWindow will not return until it calls the WndProc for the window class with WM_CREATE so the order of your code is this:
Create the main window: main_hwnd
Run the code in the WM_CREATE case of WndProc
Create the tab control tab_hwnd
Create the tab control tab0_hwnd
So you need to re-order you code so the button will not be created until after the tab has been created.

Maybe you are missingWS_VISIBLE? It is required or you won't see it.

Related

Change text type of buttons in Win32 API [duplicate]

This question already has answers here:
Normal looking button with c++ / win32
(4 answers)
Programming In C + Win API: How To Get Windows 7 Look For Controls?
(3 answers)
Closed 2 years ago.
I'm working on a program that disables Windows Explorer. I did it before in Python, but when I build it in C it is faster to start the program.
I want the buttons to look elegant, like the title text style and font. How do I make the text of the buttons look nice? I hope you can help me.
My code:
#include <windows.h>
#define ID_BTNHI0 0
#define ID_BTNHI1 1
#define ID_BTNHI2 2
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
WNDCLASSEX class_;
int WINAPI WinMain(HINSTANCE hInstanciaAct, HINSTANCE hInstanciaPrev, LPSTR IpCmdLine, int iCmdShow){
ShowWindow(GetConsoleWindow(), SW_HIDE);
HWND hWnd;
MSG msg;
class_.cbSize = sizeof(WNDCLASSEX);
class_.cbWndExtra = 0;
class_.cbClsExtra = 0;
class_.style = CS_HREDRAW|CS_VREDRAW;
class_.lpfnWndProc = WndProc;
class_.hInstance = hInstanciaAct;
class_.hIcon = LoadImage(NULL, "icoff.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
class_.hIconSm = LoadImage(NULL, "icoff.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
class_.hCursor = LoadCursor(NULL, IDC_ARROW);
class_.lpszClassName = "MICLASE";
class_.lpszMenuName = NULL;
class_.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
if(!RegisterClassEx(&class_)){
MessageBox(NULL, "NON", "ERROR", MB_ICONERROR);
return EXIT_FAILURE;
}
hWnd = CreateWindowEx(0, "MICLASE", "Windows Explorer Toggle", WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 340, 140, HWND_DESKTOP, NULL, hInstanciaAct, NULL);
if(hWnd == NULL){
MessageBox(NULL, "NON2", "ERROR", MB_ICONERROR);
return EXIT_FAILURE;
}
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0) > 0){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
HWND hBtn0;
HWND hBtn1;
HWND hBtn2;
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE:{
hBtn0 = CreateWindow("BUTTON", "Disable Windows Explorer", WS_VISIBLE | WS_CHILD | BS_FLAT, 82.5, 24.5, 175, 22.5, hWnd, (HMENU)ID_BTNHI0, GetModuleHandle(NULL), NULL);
//SetFocus(hBtn0);
hBtn1 = CreateWindow("BUTTON", "Enable Windows Explorer", WS_VISIBLE | WS_CHILD | BS_FLAT, 82.5, 24.5, 175, 22.5, hWnd, (HMENU)ID_BTNHI1, GetModuleHandle(NULL), NULL);
ShowWindow(hBtn1, SW_HIDE);
hBtn2 = CreateWindow("BUTTON", "Open Folder", WS_VISIBLE | WS_CHILD | BS_FLAT, 82.5, 48, 175, 22.5, hWnd, (HMENU)ID_BTNHI2, GetModuleHandle(NULL), NULL);
break;
}
case WM_COMMAND:{
switch(LOWORD(wParam)){
case ID_BTNHI0:{
system("TASKKILL /F /IM explorer.exe");
//clase.hIcon = LoadImage(NULL, "icoff.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
//clase.hIconSm = LoadImage(NULL, "icoff.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
ShowWindow(hBtn0, SW_HIDE);
ShowWindow(hBtn1, SW_SHOW);
//SetFocus(hBtn1);
break;
}
case ID_BTNHI1:{
system("start %SystemRoot%\\explorer.exe");
//clase.hIcon = LoadImage(NULL, "icon.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
//clase.hIconSm = LoadImage(NULL, "icon.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
ShowWindow(hBtn1, SW_HIDE);
ShowWindow(hBtn0, SW_SHOW);
//SetFocus(hBtn0);
break;
}
case ID_BTNHI2:{
system("start .");
//SetFocus(hBtn0);
break;
}
}
break;
}
case WM_DESTROY:{
PostQuitMessage(0);
break;
}
default:{
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
return 0;
}
You need to get rid of the BS_FLAT window style on the buttons, and also enable ComCtrl32 v6.0 Visual Styles for your program, if you haven't already. Then the buttons will have a modern look.

Hiding and showing container windows based on tab

I have created a tab control that contains two tabs. Inside each tab there will be a container window to hold other controls (in the code example, a static control for instance). The idea is that when a new tab is selected, it will hide/show the correct container window that holds a bunch of controls. However I am struggling to get the container windows holding the static controls to show. This is the code so far:
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define ID_TABCTRL 1
#define ID_STATIC0 2
#define ID_STATIC1 3
#define ID_TAB0 4
#define ID_TAB1 5
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hTab, hTab0, hTab1;
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wc = { 0 };
wc.lpszClassName = TEXT("Tab control");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClass(&wc);
CreateWindow(wc.lpszClassName, TEXT("Tab control"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 250, 200, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
TCITEM tie;
INITCOMMONCONTROLSEX icex;
switch (msg)
{
case WM_CREATE:
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_TAB_CLASSES;
InitCommonControlsEx(&icex);
tie.mask = TCIF_TEXT;
///// Create Tab Control /////
hTab = CreateWindow(WC_TABCONTROL, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 200, 150, hwnd, (HMENU)ID_TABCTRL, NULL, NULL);
///// Create Individual Tabs /////
tie.pszText = TEXT("First");
SendMessage(hTab, TCM_INSERTITEM, 0, (LPARAM)(LPTCITEM)&tie);
tie.pszText = TEXT("Second");
SendMessage(hTab, TCM_INSERTITEM, 1, (LPARAM)(LPTCITEM)&tie);
///// Create Container windows for each tab /////
hTab0 = CreateWindow(0, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 200, 150, hTab, (HMENU)ID_TAB0, NULL, NULL);
hTab1 = CreateWindow(0, NULL, WS_CHILD, 0, 0, 200, 150, hTab, (HMENU)ID_TAB1, NULL, NULL);
///// Add example control to one of the tab container windows /////
CreateWindow(TEXT("Static"), TEXT("Yay!"), WS_CHILD | WS_VISIBLE | SS_LEFT, 20, 30, 50, 25, hTab0, (HMENU)ID_STATIC0, NULL, NULL);
CreateWindow(TEXT("Static"), TEXT("It appears to be working"), WS_CHILD | WS_VISIBLE | SS_LEFT, 20, 30, 100, 50, hTab1, (HMENU)ID_STATIC1, NULL, NULL);
break;
case WM_NOTIFY:
switch (((LPNMHDR)lParam)->code)
{
case TCN_SELCHANGE:
switch (TabCtrl_GetCurSel(hTab))
{
///// Show or Hide the appropriate tabs /////
case 0:
ShowWindow(hTab1, SW_HIDE);
ShowWindow(hTab0, SW_SHOW);
case 1:
ShowWindow(hTab0, SW_HIDE);
ShowWindow(hTab1, SW_SHOW);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return(DefWindowProc(hwnd, msg, wParam, lParam));
}
Is it just a case of the container windows hTab0 and hTab1 being stuck behind the tab window (hTab)?
First, you need to change the position of the tab and static form, otherwise it will block the generated content.
Then you can define the generated static text directly through CreateWindow, by using WC_STATIC.
#include <Windows.h>
#include <commctrl.h>
LRESULT CALLBACK WndProc(HWND, UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
{
static HINSTANCE hInstance;
static HWND hwndTab = 0 , hwndStatic1,hwndStatic2;
TCITEM tie;
RECT rcClient;
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_TAB_CLASSES;
TCHAR tabLBL1[256];
GetClientRect(hwnd, &rcClient);
switch (message)
{
case WM_CREATE:
{
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
hwndTab = CreateWindow(WC_TABCONTROL, L"",
WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0, rcClient.right, rcClient.bottom,
hwnd, NULL, hInstance, NULL);
//Add tabs for each day of the week.
tie.mask = TCIF_TEXT | TCIF_IMAGE;
tie.iImage = -1;
wsprintf(tabLBL1, L"tab1");
tie.pszText = tabLBL1;
TabCtrl_InsertItem(hwndTab, 0, &tie);
wsprintf(tabLBL1, L"tab2");
TabCtrl_InsertItem(hwndTab, 1, &tie);
hwndStatic1 = CreateWindow(WC_STATIC, L"123",
WS_CHILD | WS_VISIBLE | WS_BORDER,
200, 200, 100, 100, // Position and dimensions; example only.
hwndTab, NULL, hInstance, // g_hInst is the global instance handle
NULL);
hwndStatic2 = CreateWindow(WC_STATIC, L"456",
WS_CHILD | WS_VISIBLE | WS_BORDER,
400, 200, 100, 100, // Position and dimensions; example only.
hwndTab, NULL, hInstance, // g_hInst is the global instance handle
NULL);
ShowWindow(hwndStatic1, TRUE);
ShowWindow(hwndStatic2, FALSE);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_NOTIFY:
if (((LPNMHDR)lParam)->code == TCN_SELCHANGE)
{
int tabID = TabCtrl_GetCurSel(hwndTab);
switch (tabID)
{
case 0:
ShowWindow(hwndStatic1, TRUE);
ShowWindow(hwndStatic2, FALSE);
break;
case 1:
ShowWindow(hwndStatic1, FALSE);
ShowWindow(hwndStatic2, TRUE);
break;
default:
break;
}
}
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
Zhu Song's answer helped solved the problem (the container windows needing to be a static control, and needing to be positioned inside the tab control's space, below the clickable tabs).
Here is the result, swapping the two container window creation lines:
hTab0 = CreateWindow(TEXT("Static"), NULL, WS_CHILD | WS_VISIBLE, 1, 25, 197, 123, hTab, (HMENU)ID_TAB0, NULL, NULL);
hTab1 = CreateWindow(TEXT("Static"), NULL, WS_CHILD, 1, 25, 197, 123, hTab, (HMENU)ID_TAB1, NULL, NULL);
Note how the container window sizes and position are small enough to just fit inside the main tab control window (you can add WS_BORDER to the style to see where exactly it fits).

C programming Dialog box

When I run my code i go to menu click uyu-> start and it says dialog failed when it needs to show dialog. I have tried perror and GetLastError() to see the error but I couldn't. Is it about the rc file or the main code, I can't tell..
my resource file
#include "mainnnnnnn.h"
IDD_IMUSTARDSOFT_DIALOG DIALOGEX 0, 0, 358, 199
STYLE DS_MODALFRAME | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION |
WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "IMUSTARDSOFT"
FONT 8, "MS Sans Serif", 0, 0, 0x1
BEGIN
DEFPUSHBUTTON "Hide",ID_BUTTON_OK,238,178,50,14,BS_FLAT
PUSHBUTTON "Exit",ID_BUTTON_EXIT,301,178,50,14,BS_FLAT
END
my header
#define utur 7444
#define ErrorDialog 49997
#define IDD_WOW 8888
#define IDD_START 9999
#define IDD_OKK 7777
#define IDD_SAVE_FILE 5655
#define IDD_NEW 4333
#define IDD_IMUSTARDSOFT_DIALOG 2333
#define IDOK 1555
#define IDCANCEL 2222
my main code
#include<windows.h>
#define THEICON "windowIco.ico"
#include "mainnnnnnn.h"
char *errorMsg;
INT_PTR WINAPI ABDialogProc(HWND newWindow, UINT msg, WPARAM wparam, LPARAM lparam){
switch(msg){
case WM_INITDIALOG:
break;
case WM_COMMAND:
break;
}
return FALSE;
}
LRESULT CALLBACK newStartCallback(HWND newWindow, UINT msg, WPARAM wparam, LPARAM lparam){
HMENU hmenu, hsubmenu;
switch(msg){
case WM_CREATE:
hmenu= CreateMenu();
hsubmenu = CreatePopupMenu();
AppendMenu(hsubmenu, MF_STRING|MF_POPUP, IDD_NEW , "&New");
AppendMenu(hsubmenu, MF_STRING|MF_POPUP, IDD_SAVE_FILE, "&Save File");
AppendMenu(hmenu, MF_STRING|MF_POPUP, (UINT_PTR)hsubmenu, "&File");
hsubmenu = CreatePopupMenu();
AppendMenu(hsubmenu, MF_STRING|MF_POPUP, IDD_START, "&Start");
AppendMenu(hsubmenu, MF_STRING|MF_POPUP, IDD_WOW, "&wow"); //IDD_WOW
AppendMenu(hsubmenu, MF_STRING|MF_POPUP, IDD_OKK, "&Okk");
AppendMenu(hmenu, MF_STRING|MF_POPUP, (UINT_PTR)hsubmenu, "&uyu");
SetMenu(newWindow, hmenu); //Forgot to add Set Menu function here
break;
case WM_COMMAND:
switch(LOWORD(wparam)){ //gets low order of wparam.... LOWORD FUNCTION ONE PARAMETER INSIDE GETS ITS LOW ORDER VALUE
case IDD_START: {
printf(" asdaksda");
HINSTANCE newWindowIns;
int returnINTValue = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_IMUSTARDSOFT_DIALOG), newWindow, ABDialogProc);
if(returnINTValue == IDOK){
MessageBox(newWindow, "Dialog exited with IDOK.", "Notice",
MB_OK | MB_ICONINFORMATION);
}
else if(returnINTValue == IDCANCEL){
MessageBox(newWindow, "Dialog exited with IDCANCEL.", "Notice",
MB_OK | MB_ICONINFORMATION);
}
else if(returnINTValue == -1){
MessageBox(newWindow, "Dialog failed!", "Error",
MB_OK | MB_ICONINFORMATION);
GetLastError();
}
}
break;
case IDD_WOW:
MessageBox(newWindow, "THE NAME", "THE TITLE", MB_ICONHAND);
break;
default: {
printf(" asda");
break;
}
}
break;
default:
return DefWindowProc(newWindow, msg, wparam, lparam);
break;
}
}
char classNameofTheNewWnd[] = "StartNewClass";
int WINAPI WinMain(HINSTANCE newWindowIns, HINSTANCE newPREVwindowIns, LPSTR dontknowthis, int nCommandShow){
HWND newWindow;
WNDCLASSEX windowclass;
windowclass.cbSize = sizeof(WNDCLASSEX);
windowclass.style = 0;
windowclass.lpfnWndProc = newStartCallback;
windowclass.hIcon = (HICON)(LoadImage(GetModuleHandle(NULL), THEICON, IMAGE_ICON, LR_DEFAULTSIZE, LR_DEFAULTSIZE, LR_LOADFROMFILE)); //
windowclass.hIconSm = LoadIcon(GetModuleHandle(NULL), THEICON); //turned to GetModuleHandle(NULL) from plain NULL // THESE ARE THE ICONS LETS SEE WHICH ONE WORKS NOW :))))
windowclass.lpszMenuName = NULL;
windowclass.lpszClassName = classNameofTheNewWnd;
windowclass.hbrBackground = (HBRUSH)(COLOR_WINDOW-2);
windowclass.cbWndExtra = 0;
windowclass.cbClsExtra = 0;
windowclass.hInstance = newWindowIns;
windowclass.hCursor = LoadCursor(NULL, IDC_CROSS);
if(!RegisterClassEx(&windowclass)){
printf("no");
}
newWindow = CreateWindowEx(WS_EX_CLIENTEDGE, classNameofTheNewWnd, "Start Something New", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1200, 550, NULL, NULL, newWindowIns, NULL);
ShowWindow(newWindow, nCommandShow);
MSG message;
printf("%d", utur);
while(GetMessage(&message, NULL, 0, 0) > 0){ //don't forget to compare here..... if it is more than 0 then it means there is a new message!!!! :D
DispatchMessage(&message);
}
printf("message.wParam");
return message.wParam;
}

WinApi Win32 Programming - Button click not sending WM_COMMAND message

I have been working on a tab controlled program for awhile now and have only recently been posting on StackOverflow for help. Currently the program runs as follows:
Begin in winMain, Creation of main_hwnd generates WM_CREATE and wndProc is called
tab_hwnd is created as a SysTabControl32
tab0_hwnd, tab1_hwnd, ..., tab4_hwnd are created
In tab0_hwnd, static text and buttons are created
Return to winMain and ShowWindow() is called on main_hwnd, tab_hwnd and tab0_hwnd
Enter a loop until program is closed.
When the program is run, everything is visible; however, the buttons do not function properly. At the same time, when I was searching for errors using GetLastError() and FormatMessage() I found that after creating windows tab#_hwnd (where # = 0,1,2,3 and 4), the following error appears:
Error 6: The handle is Invalid
I am not sure as to why this error appears and why the button does not work. Another user has suggested the following in a previous post:
"WM_COMMAND is sent to immediate parent window, not to grandparents, so you'll either have to subclass your tab controls too, or make your buttons have your main window as parent." - Anton Kovalenko
Edit 1: I am unsure how to subclass my tab controls as suggested by Anton Kovalenko. Any links to tutorials/explanations to comments would be extremely helpful.
Here is the current code:
#define WIN32_LEAN_AND_MEAN
#pragma comment(lib, "comctl32.lib")
#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>
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;
static HWND text;
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);
}
// Call with ErrorExit(TEXT("ANYTHINGHERE"));
LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_CREATE:
{
tab_hwnd = CreateWindowEx(
0,
"SysTabControl32",
"",
WS_CHILD,
CW_USEDEFAULT, CW_USEDEFAULT, 681, 338,
hwnd, NULL, tab_hinst, NULL);
tab0_hwnd = CreateWindowEx(
0,
"SysTabControl32",
"",
WS_CHILD,
1, 22, 679, 315,
tab_hwnd, NULL, tab_hinst, NULL);
//ErrorExit(TEXT("HERE")); //<--- This gives error 6: Handle invalid
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,
1, 22, 679, 315,
tab_hwnd, NULL, tab_hinst, NULL);
tab2_hwnd = CreateWindowEx(
0,
"SysTabControl32",
"",
WS_CHILD,
1, 22, 679, 315,
tab_hwnd, NULL, tab_hinst, NULL);
tab3_hwnd = CreateWindowEx(
0,
"SysTabControl32",
"",
WS_CHILD,
1, 22, 679, 315,
tab_hwnd, NULL, tab_hinst, NULL);
tab4_hwnd = CreateWindowEx(
0,
"SysTabControl32",
"",
WS_CHILD,
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:
//MessageBox(tab0_hwnd,"HERE","OKAY",NULL);
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);
case ID_B_HOME:
if(BN_CLICKED == HIWORD(wParam)){
MessageBox(tab0_hwnd,"HERE","OKAY",NULL);
}
break;
case ID_B_SAVE:
if(BN_CLICKED == HIWORD(wParam)){
}
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)
{
InitCommonControls();
tab_hinst = hInstance;
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);
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);
do{
PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);
TranslateMessage(&msg);
DispatchMessage(&msg);
} while(msg.message!=WM_CLOSE && msg.message!=WM_QUIT);
return msg.wParam;
}

Winapi win32 programming - Tab Control not showing up

I have been working on a Tab Control and it is only visible when posted in winMain. When I try creating the tabs in the WM_CREATE message handle, nothing shows up. Here is my code:
#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;
}
I believe the process that the code should be following is as follows:
1. Create the main window main_hwnd
2. Goes to WM_CREATE and runs this code
3. During this it creates tab_hwnd, tab0_hwnd, ..., tab4_hwnd
4. Then it returns to winMain, and shows the windows. Afterwards it reaches the loop.
Your main_hwnd is assigned after CreateWindowEx returns, hence it's always NULL when WM_CREATE is delivered. You can use the hwnd parameter of your WndProc instead of main_hwnd.
With this modification, it works for me if I also add InitCommonControls() to the beginning of WinMain (linking against comctl32 library).

Resources