473,657 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Win32 Button

25 New Member
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. }
Sep 14 '07 #1
7 22361
Studlyami
464 Recognized Expert Contributor
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.
Sep 15 '07 #2
newguy194
25 New Member
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.
Sep 15 '07 #3
newguy194
25 New Member
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.
Sep 15 '07 #4
Studlyami
464 Recognized Expert Contributor
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.
Sep 15 '07 #5
newguy194
25 New Member
Sorry, my compiler is gcc, not Dev silly me.
Sep 15 '07 #6
Cucumber
90 New Member
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_BUFFE R|FORMAT_MESSAG E_FROM_SYSTEM,
NULL,errorcode, MAKELANGID(LANG _NEUTRAL, SUBLANG_DEFAULT ),
(LPTSTR)&buffer ,0,NULL);
::MessageBox(HW ND_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.
Sep 25 '07 #7
Glenn Ward
1 New Member
I know this is an old topic but just in case someone comes upon it by chance I wanted to post a reply so people can see. First of all this isn't good programming practice. Your HWND button shouldn't be in the WinMain function because your Callback can't access it, it must be a global if you wish to use it this way. Also you shouldn't create it in WinMain, you should use the case WM_CREATE to run the function CreateWindow.

Expand|Select|Wrap|Line Numbers
  1. #define ID_EXITBUTTON 69000
  2. #include <Windows.h>
  3.  
  4. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  5.  
  6. HWND Button;
  7. char *window = "Change this text to your window's class name!";
  8.  
  9. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  10. {
  11.     WNDCLASSEX wc;
  12.     HWND hWnd;
  13.     MSG msg;
  14.  
  15.     wc.cbSize        = sizeof(WNDCLASSEX);
  16.     wc.style         = 0;
  17.     wc.lpfnWndProc   = WndProc;
  18.     wc.cbClsExtra    = 0;
  19.     wc.cbWndExtra    = 0;
  20.     wc.hInstance     = hInstance;
  21.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  22.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  23.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  24.     wc.lpszMenuName  = NULL;
  25.     wc.lpszClassName = window;
  26.     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  27.  
  28.     if(!RegisterClassEx(&wc))
  29.     {
  30.         MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  31.         return 0;
  32.     }
  33.  
  34.     // Step 2: Creating the Window
  35.     hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, window, "Character Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  36.  
  37.     if(hWnd == NULL)
  38.     {
  39.         MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  40.         return 0;
  41.     }
  42.  
  43.     ShowWindow(hWnd, nCmdShow);
  44.     UpdateWindow(hWnd);
  45.  
  46.     // Step 3: The Message Loop
  47.     while(GetMessage(&msg, NULL, 0, 0) > 0)
  48.     {
  49.         TranslateMessage(&msg);
  50.         DispatchMessage(&msg);
  51.     }
  52.     return msg.wParam;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  56. {
  57.     switch(msg)
  58.     {
  59.     case WM_CREATE:
  60.         {
  61.             Button = CreateWindow("button", "Exit", BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE, 250, 30, 100, 20, hWnd, (HMENU)ID_EXITBUTTON, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
  62.             break;
  63.         }
  64.     case WM_COMMAND:
  65.         {
  66.             switch(wParam)
  67.             {
  68.             case ID_EXITBUTTON:
  69.                 {
  70.                     PostQuitMessage(0);
  71.                     break;
  72.                 }
  73.             }
  74.             break;
  75.         }
  76.     case WM_DESTROY:
  77.         {
  78.             PostQuitMessage(0);
  79.             break;
  80.         }
  81.     default:
  82.         {
  83.             return DefWindowProc(hWnd, msg, wParam, lParam);
  84.         }
  85.     }
  86.     return 0;
  87. }
  88.  
All this button does is close your window down, you can change the case ID_EXITBUTTON to whatever you choose so that you can manipulate it to do what you please though. Notice I defined ID_EXITBUTTON at the very top, you normally do this in the resource.h file but since I did it here just to give you a working program. The key thing here is that WndProc can now access the button HWND unlike your copy earlier. WndProc must be able to do this because it is a child window, if not you have problems as you can see. Hopefully this explains to future programmers why we code things a certain way like utilize WM_CREATE instead of putting things in WinMain. In fact, WinMain shouldn't really change at all, it should remain the same from program to program. Notice I put the char *window as a global. Normally this would be in WinMain and the only thing you would change but I wanted to show that WinMain should be 100% constant except this variable so that is why I did it. Also you can't compile this in UNICODE because it doesn't convert chat * properly so if you get an error in Visual C++, that is why.
Oct 29 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
9710
by: PK | last post by:
Hello, I am looking for help on the following. I'm trying to create a custom browser toolbar button that will do a few things. One that I'm trying to do at the moment is just simply return the URL of whatever page the user is visiting. I wanted to create a Win32 application using VB.NET. If there is a better way of doing this please let me know. I assume there needs to be some sort of client side code. I was originally leaning...
2
4984
by: RL | last post by:
Hello Perl gurus, 1. I have a web page where I can push a button (dospawn.html). 2. This button calls a CGI script (spawnboss.cgi) 3. spawnboss.cgi calls a forking perl script (forkme.pl) 4. forkme.pl calls the process creation script (createme.pl) 5. createme.pl creates my notepad.exe process, but no window shows up on my PC. The result on my web browser is:
18
3058
by: jrhoads23 | last post by:
Hello, I am trying to find a way to tell if an .NET windows forms Button (System.Windows.Forms.Button) is "depressed" (pushed down). For my application, I can not use a check box control set to button style, I must use a System.Windows.Forms.Button. I can not find a way to tell when it is momentaraly pressed. I tried calling the API SendMessage with the button handle and BM_GETSTATE to get the state of the button. This will only return...
0
1371
by: Tim | last post by:
Hi There I am trying to override the forecolor of a radio button when the flat style is set to system. I am using a manifest file. If you set the name to the same name as your exe with .manifest on the end and set the control to flatstyle it gives you the more modern windows look the green radio button with the gradient. (so Test.exe would have manifest Test.exe.manifest with the contents below in it) But when I use this it does not...
4
2082
by: Dale Levesque | last post by:
I get this message. any ideas?? See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box. ************** Exception Text ************** System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
2
11961
by: Danny | last post by:
Hi I want to be able from my csharp button to start an application ( which I can do) and then somehow send a message to the win32 application that says "press the button" The win32 application ( borland app) has a button on it that I need to click in order for it to start correctly. Is this possible from csharp?? thanks
3
2575
by: Shafiq | last post by:
Hi, I am trying to insert a new toolbar button to the windows explorer menu. I an able to locate the correct ToolbarWindow32, and inserted a button using the code snippet shown below. However the toolbar button text is coming in jung characters. Any help will be greatly appreciated. Int32 iBitmap = 0;
10
2502
by: Mark Rae [MVP] | last post by:
Hi, This is really just a theoretical question for my own interest, and not for any nefarious purpose... :-) Say we have three applications running - Notepad, Wordpad and Excel - and each window is in its "normal" state i.e. not maximised, and not minimised to the taskbar. We click each of the three taskbar buttons in turn and, of course, the three
6
10047
by: Perl Beginner | last post by:
Good morning all, I had to reinstall Perl (ActivePerl 5.10.0 Build 1004) on my computer (Windows XP), the install went fine. however, I'm having issues loading Win32-GuiTest using the repository in PPM. the steps i took are: 1. saved Win32-GuiTest-1.50.5.zip from the search.cpan.org website 2. unzipped in the location c:/Perl/Win32-GuiTest 3. opened the PPM window and went to Edit-Preferences and clicked the repositories tab 4. i put...
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.