Connecting Tech Pros Worldwide Forums | Help | Site Map

Win32 Button

Newbie
 
Join Date: May 2007
Posts: 25
#1: Sep 14 '07
Why am I unable to produce a button with this Win32 program? All I have been able to produce is the empty window.



Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2.  
  3. // Declare WndProcedure
  4. LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
  5.                WPARAM wParam, LPARAM lParam);
  6.  
  7. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  8.                LPSTR lpCmdLine, int nCmdShow)
  9. {
  10.     HWND       button;
  11.     MSG        Msg;
  12.     HWND       hWnd;
  13.     HRESULT       hRet;
  14.     WNDCLASSEX WndClsEx;
  15.  
  16.     // Populate the WNDCLASSEX structure
  17.     WndClsEx.cbSize        = sizeof(WNDCLASSEX);
  18.     WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
  19.     WndClsEx.lpfnWndProc   = WndProcedure;
  20.     WndClsEx.cbClsExtra    = 0;
  21.     WndClsEx.cbWndExtra    = 0;
  22.     WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  23.     WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
  24.     WndClsEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  25.     WndClsEx.lpszMenuName  = NULL;
  26.     WndClsEx.lpszClassName = "GlowdotWin32TutorialPartI";
  27.     WndClsEx.hInstance     = hInstance;
  28.     WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  29.  
  30.     // Register the class
  31.     RegisterClassEx(&WndClsEx);
  32.  
  33.     // Create the window object
  34.     hWnd = CreateWindow("GlowdotWin32TutorialPartI",
  35.               "Glowdot Win32 Tutorial - Part I",
  36.               WS_OVERLAPPEDWINDOW,
  37.               CW_USEDEFAULT,
  38.               CW_USEDEFAULT,
  39.               CW_USEDEFAULT,
  40.               CW_USEDEFAULT,
  41.               NULL,
  42.               NULL,
  43.               hInstance,
  44.               NULL);
  45.  
  46.     button = CreateWindow( 
  47.     "BUTTON",                                    // predefined class
  48.     "OK",                                        // button text
  49.     WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,    // styles 
  50.                                                  // Size and position values are given 
  51.                                                  // explicitly, because the CW_USEDEFAULT 
  52.                                                  // constant gives zero values for buttons. 
  53.     100,                                          // starting x position
  54.     100,                                          // starting y position 
  55.     200,                                         // button width 
  56.     200,                                         // button height 
  57.     hWnd,                                        // parent window 
  58.     NULL,                                        // No menu 
  59.     hInstance,                                   // Our apps HINSTANCE 
  60.     NULL                                         // pointer not needed 
  61. );
  62.  
  63.     if ( !button)
  64.        return 0;
  65.     // Verify window creation
  66.     if( !hWnd ) // If the window was not created,
  67.         return 0; // stop the application
  68.  
  69.     // Show the window
  70.     ShowWindow(hWnd, SW_SHOWNORMAL);
  71.     ShowWindow(button, SW_SHOWNORMAL);
  72.     UpdateWindow(hWnd);
  73.     UpdateWindow(button);
  74.     // our message pump
  75.     while( (hRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
  76.     { 
  77.         if (hRet == -1)
  78.         {
  79.         // handle the error and possibly exit
  80.         }
  81.         else
  82.         {
  83.             TranslateMessage(&Msg); 
  84.             DispatchMessage(&Msg); 
  85.         }
  86.     }
  87. }
  88.  
  89. //////////////////
  90. // WndProcedure //
  91. //////////////////
  92.  
  93. LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
  94.                WPARAM wParam, LPARAM lParam)
  95. {
  96.     switch(Msg)
  97.     {
  98.     case WM_DESTROY:
  99.         // user wants to exit
  100.         PostQuitMessage(WM_QUIT);
  101.         break;
  102.     default:
  103.         // Hand off unprocessed messages to DefWindowProc
  104.         return DefWindowProc(hWnd, Msg, wParam, lParam);
  105.     }
  106.  
  107.     return 0;
  108. }

Expert
 
Join Date: Sep 2007
Location: VA
Posts: 416
#2: Sep 15 '07

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
#3: Sep 15 '07

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
#4: Sep 15 '07

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
#5: Sep 15 '07

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
#6: Sep 15 '07

re: Win32 Button


Sorry, my compiler is gcc, not Dev silly me.
Member
 
Join Date: Sep 2007
Posts: 90
#7: Sep 25 '07

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.
Reply