Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir, I have a problem in creating some buttons in my main window.
I have no errors and no warnings in creating a button but I cannot see my button on my main window,
C++
//My code in initializing the instance function [ BOOL  InitInstance(HINSTANCE hInstance, int nCmdShow) ]

HWND button1;

button1 = CreateWindow(L"button", L"Run Timer", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | BS_FLAT, 0, 180, 197, 90, button1, (HMENU)IDC_VS_TIME, hInst, NULL);
ShowWindow(button1, nCmdShow);
UpdateWindow(button1);


What I have tried:

Since I am learning I am not sure of spotting the bug,
I see my main window displaying so I tried copying its HWND variable hWnd to my button 1 like this,

C++
button1 = CreateWindow(L"button", L"Run Timer", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | BS_FLAT, 0, 180, 197, 90, hWnd, (HMENU)IDC_VS_TIME, hInst, NULL);
//changed button1 to hWnd

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

I changed button1 to hWnd and my button gets displayed I did also compared both the code but I can't find anything.
Posted
Updated 24-Feb-16 6:52am
v2
Comments
Jochen Arndt 24-Feb-16 10:21am    
And what is the problem now after passing the correct parameters to CreateWindow()?

Read the documentation at https://msdn.microsoft.com/en-us/library/windows/desktop/ms632679(v=vs.85).aspx:

hWndParent
A handle to the parent or owner window of the window being created. To create a child window or an owned window, supply a valid window handle.

A button is a child window (located inside another - the parent - window). So you must pass a handle to that window.
[no name] 24-Feb-16 11:28am    
Sir, From the link you have given to me I find that my try is the correct one because hWnd is the variable which handles the man window.Sir, is this a correct one or not? because I see my buttons when I use hWnd.

Thank you for your kind help.
Richard MacCutchan 24-Feb-16 11:47am    
The HWND variable in the CreateWindow call must be the handle of the parent window where the button is to be displayed. Thus your second version is correct.
[no name] 25-Feb-16 11:46am    
Thank you sir for your kind help

1 solution

The parent in your first call is a bug, because it is the button1 and NOT a parent windows.

C++
HWND button1;
 
button1 = CreateWindow(L"button", L"Run Timer", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | BS_FLAT, 0, 180, 197, 90, button1, (HMENU)IDC_VS_TIME, hInst, NULL);


The answer from Richard is right: You must provide a valid parent window as your second code shows .
 
Share this answer
 
Comments
[no name] 25-Feb-16 11:47am    
Thank you sir for your kind help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900