Win32 Button | Newbie | | Join Date: May 2007
Posts: 25
| |
Why am I unable to produce a button with this Win32 program? All I have been able to produce is the empty window. - #include <windows.h>
-
-
// Declare WndProcedure
-
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
-
WPARAM wParam, LPARAM lParam);
-
-
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
-
LPSTR lpCmdLine, int nCmdShow)
-
{
-
HWND button;
-
MSG Msg;
-
HWND hWnd;
-
HRESULT hRet;
-
WNDCLASSEX WndClsEx;
-
-
// Populate the WNDCLASSEX structure
-
WndClsEx.cbSize = sizeof(WNDCLASSEX);
-
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
-
WndClsEx.lpfnWndProc = WndProcedure;
-
WndClsEx.cbClsExtra = 0;
-
WndClsEx.cbWndExtra = 0;
-
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
-
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
-
WndClsEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
-
WndClsEx.lpszMenuName = NULL;
-
WndClsEx.lpszClassName = "GlowdotWin32TutorialPartI";
-
WndClsEx.hInstance = hInstance;
-
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
-
-
// Register the class
-
RegisterClassEx(&WndClsEx);
-
-
// Create the window object
-
hWnd = CreateWindow("GlowdotWin32TutorialPartI",
-
"Glowdot Win32 Tutorial - Part I",
-
WS_OVERLAPPEDWINDOW,
-
CW_USEDEFAULT,
-
CW_USEDEFAULT,
-
CW_USEDEFAULT,
-
CW_USEDEFAULT,
-
NULL,
-
NULL,
-
hInstance,
-
NULL);
-
-
button = CreateWindow(
-
"BUTTON", // predefined class
-
"OK", // button text
-
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // styles
-
// Size and position values are given
-
// explicitly, because the CW_USEDEFAULT
-
// constant gives zero values for buttons.
-
100, // starting x position
-
100, // starting y position
-
200, // button width
-
200, // button height
-
hWnd, // parent window
-
NULL, // No menu
-
hInstance, // Our apps HINSTANCE
-
NULL // pointer not needed
-
);
-
-
if ( !button)
-
return 0;
-
// Verify window creation
-
if( !hWnd ) // If the window was not created,
-
return 0; // stop the application
-
-
// Show the window
-
ShowWindow(hWnd, SW_SHOWNORMAL);
-
ShowWindow(button, SW_SHOWNORMAL);
-
UpdateWindow(hWnd);
-
UpdateWindow(button);
-
// our message pump
-
while( (hRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
-
{
-
if (hRet == -1)
-
{
-
// handle the error and possibly exit
-
}
-
else
-
{
-
TranslateMessage(&Msg);
-
DispatchMessage(&Msg);
-
}
-
}
-
}
-
-
//////////////////
-
// WndProcedure //
-
//////////////////
-
-
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
-
WPARAM wParam, LPARAM lParam)
-
{
-
switch(Msg)
-
{
-
case WM_DESTROY:
-
// user wants to exit
-
PostQuitMessage(WM_QUIT);
-
break;
-
default:
-
// Hand off unprocessed messages to DefWindowProc
-
return DefWindowProc(hWnd, Msg, wParam, lParam);
-
}
-
-
return 0;
-
}
| | Expert | | Join Date: Sep 2007 Location: VA
Posts: 416
| | | re: Win32 Button
what compiler are you using?
That program fails to create anything on visual studios 2005. I added
if(!hWnd)
{
return 0;
}
and i get a return 0. two tired to try and figure out why.
| | Newbie | | Join Date: May 2007
Posts: 25
| | | re: Win32 Button Quote:
Originally Posted by Studlyami what compiler are you using?
That program fails to create anything on visual studios 2005. I added
if(!hWnd)
{
return 0;
}
and i get a return 0. two tired to try and figure out why. My compiler is gcc
The program creates a window for me, but does not generate or display the button, and the check I added for the button is telling me it is not created.
| | Newbie | | Join Date: May 2007
Posts: 25
| | | re: Win32 Button
Something really, really, really, really weird. On the PC which I do most of my programming on the program fails to run correctly and nothing appears, but on my Vista pc the program runs fine, generates a button and doesn't fail any of the checks.
| | Expert | | Join Date: Sep 2007 Location: VA
Posts: 416
| | | re: Win32 Button
LOL! Alright on my home pc (Vista, Visual Studios 2005 (latest updates)) this program wouldn't even generate the original window. At work (XP, Visual Studios 2003(Uknown updates)) this program loads fine and creates the button. I would say this would of been an issue with Vista, but newguy built it fine on vista. Newguy what program did you use to compile it? maybe we can narrow the problem down that way.
| | Newbie | | Join Date: May 2007
Posts: 25
| | | re: Win32 Button
Sorry, my compiler is gcc, not Dev silly me.
| | Member | | Join Date: Sep 2007
Posts: 90
| | | re: Win32 Button
You can use this function to show what was the last error at any point within your win32 program. You may use it after creating or showing the button to see what the operating system that is not showing the button has to say about it.
void ShowLastError( )
{
TCHAR *buffer;
int errorcode = ::GetLastError();
if ( errorcode )
{
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FOR MAT_MESSAGE_FROM_SYSTEM,
NULL,errorcode,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&buffer,0,NULL);
::MessageBox(HWND_DESKTOP, buffer, 0,0);
::LocalFree( buffer );
}
}
By the way, using UpdateWindow with the button seems a bit awkward to me, the Win32 protocol says you should call UpdateWindow for the parent Window, not for the child controls.
I would also avoid calling ShowWindow for the control too.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|