Dialog box in resource file - c

I am trying to create a dialog box and it returns an error in the resource file. I have been trying to execute different types of codes and ever found the issue. It says unknown type IDD_TOOL. What is it that's wrong here?
The errors that I get is:
[Error] unknown type name 'IDD_TOOLBAR'
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before numeric constant (line 2 column 22 for resource code.)
This is the resource code.
IDD_ABOUT DIALOG DISCARDABLE 0, 0, 239, 66
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My About Box"
FONT 8, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON "&OK",IDOK,174,18,50,14
PUSHBUTTON "&Cancel",IDCANCEL,174,35,50,14
GROUPBOX "About this program...",IDC_STATIC,7,7,225,52
CTEXT "An example program showing how to use Dialog Boxes\r\n\r\nby theForger",
IDC_STATIC,16,18,144,33
END
This is the main code.
#include<windows.h>
#include "new.rc"
#include "main.h"
#define THEICON "windowIco.ico"
HWND g_hToolbar;
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, 10001 , "&New");
AppendMenu(hsubmenu, MF_STRING|MF_POPUP, 10002, "&Save File");
AppendMenu(hmenu, MF_STRING|MF_POPUP, (UINT)hsubmenu, "&File");
hsubmenu = CreatePopupMenu();
AppendMenu(hsubmenu, MF_STRING|MF_POPUP, 10003, "&Start");
AppendMenu(hsubmenu, MF_STRING|MF_POPUP, 10004, "&dialogbox...");
AppendMenu(hmenu, MF_STRING|MF_POPUP, (UINT)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 10003:
printf(" asdaksda");
break;
case 1004:
// int dialogBoxint = DialogBox(GetModuleHandle(NULL), , newWindow, );
break;
default:
printf(" asda");
break;
}
break;
default:
return DefWindowProc(newWindow, msg, wparam, lparam);
}
}
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 = 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;
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
TranslateMessage(&message);
DispatchMessage(&message);
}
printf("message.wParam");
return message.wParam;
}

Related

How to hide the cursor completely?

When I call ShowCursor(0) (which by the way, does not take an HWND, this is weird), the cursor is hidden, but when I do a special action, for example resizing the window or drag-and-drop a file, the cursor corresponding to this action appears. I don't want to see the cursor whatever action the user is doing. If you wonder why, it's because I'd like to draw it with OpenGL.
I have a popup and layered window:
#define UNICODE
#include <windows.h>
#include <dwmapi.h>
LRESULT CALLBACK wnd_proc(HWND hWnd, UINT uMsg, WPARAM wp, LPARAM lp){
switch(uMsg){
case WM_CREATE: return 0;
case WM_NCHITTEST: return HTBOTTOM;
case WM_DESTROY: PostQuitMessage(0); return 0;
default: return DefWindowProc(hWnd, uMsg, wp, lp);
}
}
HDC hDC; HGLRC hRC;
int main(void){
WNDCLASSEX wcx = {};
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.lpfnWndProc = wnd_proc;
wcx.lpszClassName = L"Win32Class";
wcx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wcx.hbrBackground = CreateSolidBrush(0x00000000);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClassEx(&wcx);
HWND hWnd = CreateWindowEx(WS_EX_ACCEPTFILES | WS_EX_LAYERED, wcx.lpszClassName,
L"Win32Window", WS_POPUP | WS_VISIBLE, 50, 50, 800, 400, NULL, NULL, NULL, NULL);
SetLayeredWindowAttributes(hWnd, RGB(200, 0, 200), 0, LWA_COLORKEY);
ShowCursor(0);
/*DWM_BLURBEHIND blur = {0};
blur.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
blur.fEnable = 1;
blur.hRgnBlur = CreateRectRgn(0, 0, -1, -1);
DwmEnableBlurBehindWindow(hWnd, &blur);*/
MSG msg; while(GetMessage(&msg, NULL, 0, 0)){
DispatchMessage(&msg);
}
}
This is a truncated part of my test file. OpenGL is not included, but I just need to know how to delete the cursor.
If you compile and execute it, you can see that when your cursor is on the window, it is not visible, but when you resize the window the cursor appears, same as when you drag-and-drop a file.

Win32 API Child window doesn't appear in parent's area

I am trying to write a simple app in Win32 API. The goal is to create a parent window with blue background and a child window (visible as a small red square) inside it.
I define a parent and child windows:
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GAMEPRACTICE3));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush(RGB(0, 0, 255));
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_GAMEPRACTICE3);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassEx(&wcex);
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE | WS_BORDER,
0,0, 400, 600, HWND_DESKTOP, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Child window
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpszClassName = childClass;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = hInst;
wc.hbrBackground = CreateSolidBrush(RGB(255, 0, 0));
wc.lpfnWndProc = WndProc;
RegisterClassEx(&wc);
HWND hwnd_child = CreateWindow(childClass, NULL,
WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
0, 0, 32, 32,
hWnd, 0, hInst, 0);
ShowWindow(hwnd_child, nCmdShow);
UpdateWindow(hwnd_child);
When I launch the app I get the following output:
It seems that the child window is not displaying at all.
I tried to search similar problems on the Internet but none of the solutions worked for me so far.
Source code:
#include "stdafx.h"
#include "GamePractice3.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst;
WCHAR szTitle[MAX_LOADSTRING];
WCHAR szWindowClass[MAX_LOADSTRING];
WCHAR childClass[MAX_LOADSTRING];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_GAMEPRACTICE3, szWindowClass, MAX_LOADSTRING);
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GAMEPRACTICE3));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = CreateSolidBrush(RGB(0, 0, 255));
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_GAMEPRACTICE3);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassEx(&wcex);
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GAMEPRACTICE3));
MSG msg;
hInst = hInstance;
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE | WS_BORDER,
0,0, 400, 600, HWND_DESKTOP, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpszClassName = childClass;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = hInst;
wc.hbrBackground = CreateSolidBrush(RGB(255, 0, 0));
wc.lpfnWndProc = WndProc;
RegisterClassEx(&wc);
HWND hwnd_child = CreateWindow(childClass, NULL,
WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
0, 0, 32, 32,
hWnd, 0, hInst, 0);
ShowWindow(hwnd_child, nCmdShow);
UpdateWindow(hwnd_child);
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
Any help would be appreciated :).
For Child window doesn't appear in parent's area issue, in addition to #daniel_p's answer:
WS_OVERLAPPEDWINDOW style is not relevant here. This style gives the window a title bar, a window menu, a sizing border, and minimize and maximize buttons.
Error checking is necessary. You will find that register child window class failed and return 0.
Specify a valid null-terminated string for class name. Class name is NULL is one of factors cause RegisterClassEx fail.
Initialize WNDCLASSEX struct like: WNDCLASSEX wc = {0}; Without initialization, some value which you don't set explicitly later will be garbage value. This is another factor cause RegisterClassEx fail. Or you can set every value explicitly.
With the following editions your code will work:
WCHAR childClass[MAX_LOADSTRING] = L"Child Window";
// ...
WNDCLASSEX wc = {0};
Example for error checking of RegisterClassEx function:
DWORD errCode = 0;
ATOM childClassId = RegisterClassEx(&wc);
if (!childClassId)
{
errCode = GetLastError();
MessageBox(NULL, L"RegisterClassEx failed!", L"Error", MB_ICONERROR | MB_OKCANCEL);
return 0;
}
I managed to resolve the issue.
I removed WS_OVERLAPPEDWINDOW from the creation of the child window
I added seperate Window Procedure for the child window with
DefWindowProc
I created a string for childClass and loaded it (LoadStringW)
I initialized all 11 fields of WNDCLASSEXW structure (not sure if it's necessary)
Hope it helps.

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 - Button not visible

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.

Resources