processing button clicks

This is a discussion on processing button clicks within the Windows Programming forums, part of the Platform Specific Boards category; I'm making a program that simply adds two numbers, learning. Heres the code: Code: #include <windows.h> static char gszClassName[] = ...

  1. #1
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186

    Question processing button clicks

    I'm making a program that simply adds two numbers, learning.

    Heres the code:

    Code:
    #include <windows.h>
    
    static char gszClassName[]  = "MyWindowClass";
    static HINSTANCE ghInstance = NULL;
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) 
    {
        HWND hBorder,hBox1,hPlus,hBox2,hEqual,hBox3;
    
        char str1[10];
        char str2[10];
        char str3[10];
        int x1,x2;
    
        switch(Message) 
        {   	    	   
    	   case WM_CREATE:				 										   		 
    	   {		    	   
    		  hBorder = CreateWindowEx 
    				(
    				    NULL,
    				    "Static",
    				    "",
    				    WS_CHILD | WS_VISIBLE | WS_BORDER,
    				    4, 2,
    				    196, 46,
    				    hwnd, NULL,
    				    ghInstance,
    				    NULL
    				);	
    
    		  hBox1   = CreateWindowEx
    				(
    				    WS_EX_RIGHT | WS_EX_CLIENTEDGE,
    				    "Edit",
    				    "",
    				    WS_CHILD | WS_VISIBLE,
    				    10, 12,
    				    30, 25,
    				    hwnd, NULL,
    				    ghInstance,
    				    NULL
    				);
    
    		  hPlus   = CreateWindowEx
    				(
    				    NULL,
    				    TEXT("Static"),
    				    TEXT("+"),
    				    WS_CHILD | WS_VISIBLE | SS_SIMPLE,
    				    50, 16,
    				    10, 15,
    				    hwnd, NULL,
    				    ghInstance,
    				    NULL
    				);
    
    
    		  hBox2   = CreateWindowEx
    				(
    				    WS_EX_RIGHT | WS_EX_CLIENTEDGE,
    				    "Edit",
    				    "",
    				    WS_CHILD | WS_VISIBLE,
    				    70, 12,
    				    30, 25,
    				    hwnd, NULL,
    				    ghInstance,
    				    NULL
    				);
    			   
    		  hEqual   = CreateWindowEx
    				(
    				    WS_EX_CLIENTEDGE,
    				    "Button",
    				    "=",
    				    WS_CHILD | WS_VISIBLE,
    				    110, 16,
    				    30, 18,
    				    hwnd, NULL,
    				    ghInstance,
    				    NULL
    				);
    
    		  hBox3   = CreateWindowEx
    				(
    				    WS_EX_RIGHT | WS_EX_CLIENTEDGE,
    				    "Edit",
    				    "",
    				    WS_CHILD | WS_VISIBLE,
    				    148, 12,
    				    45, 25,
    				    hwnd, NULL,
    				    ghInstance,
    				    NULL
    				);
    	   }
    	   break;
    		  
    	   case WM_COMMAND:
    	   {
    		  switch(LOWORD(wParam))
    		  {
    			 case hEqual:
    			 {
    			 
    				GetWindowText(hBox1,str1,10);
    				GetWindowText(hBox2,str2,10);
    			  
    				x1 = atoi(str1);
    				x2 = atoi(str2);
    			    
    				if (x1)				
    				    SetWindowText(hBox3,itoa(x1+x2,str3,10));
    				else
    				    MessageBox(hwnd,"Error",NULL,NULL);	 		  
    			 }
    			 break;
    		  }
    	   }
    	   break;
    
    	   case WM_CLOSE:
    		  DestroyWindow(hwnd);
    	   break;
    
    	   case WM_DESTROY:
    		  PostQuitMessage(0);
    	   break;
    
    	   default:
    		  return DefWindowProc(hwnd, Message, wParam, lParam);
        }
        
        return 0;
    } 
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
    {
        WNDCLASSEX WndC;
        HWND hwnd;
        MSG Msg;
    
        ghInstance = hInstance;
    
        WndC.cbSize        = sizeof(WNDCLASSEX);
        WndC.style         = NULL;
        WndC.lpfnWndProc   = WndProc;
        WndC.cbClsExtra    = 0;
        WndC.cbWndExtra    = 0;
        WndC.hInstance     = ghInstance;
        WndC.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndC.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndC.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);
        WndC.lpszMenuName  = NULL;
        WndC.lpszClassName = gszClassName;
        WndC.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&WndC)) 
        {
    	   MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);
    	   return 0;
        }
    
        hwnd = CreateWindowEx
    	   (
    		  WS_EX_STATICEDGE,
    		  gszClassName,
    		  "math_test",
    		  WS_SYSMENU | WS_MINIMIZEBOX,	//WS_OVERLAPPEDWINDOW 
    		  CW_USEDEFAULT, CW_USEDEFAULT,
    		  210, 75,
    		  NULL, NULL,
    		  ghInstance,
    		  NULL
    	   );
    
        if(hwnd == NULL) 
        {
    	   MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONSTOP | MB_OK);
    	   return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0)) 
        {
    	   TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        
        return Msg.wParam;
    }
    Is there something wrong with the bold faced part of the code, WM_COMMAND? Cause nothing happens when i click the equal button.

    I think the code for getting the text and adding is correct, but im not so sure about this part:
    Code:
    switch(LOWORD(wParam))
    		  {
    			 case hEqual:
    			 {
    the best things in life are simple.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    change this:

    switch(LOWORD(wParam))

    to:

    if((HWND)lParam == hEqual)


    Also, GetMessage can return -1 on error. change to:

    while(GetMessage(&Msg, NULL, 0, 0) > 0)

    BTW: what part of H-town you live in?
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  3. #3
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    That doesnt seem to work.
    I commented out all the adding code, and just put setwindowtext when equal is pressed, and that didn't work, so its not an adding problem.

    I live in the northwest part of Houston. Katy area.
    the best things in life are simple.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    Ok, then try:

    case WM_LBUTTONDOWN:
    case WM_RBUTTONDOWN:
    if(hwnd == hEqual)
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  5. #5
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    that didnt work either.
    the best things in life are simple.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Your variables in the callback are not static so the HWND's will have lost scope.

    If you are creating a child you must cast the HMENU as the int ID number to use in the command case. Pick a number around 40000 to avoid clashes with the ones the resource editor generates.

    case IM_EXIT:
    case IDCANCEL:
    if (BN_CLICKED == HIWORD(wParam))


    GetLastError() is your BEST friend, why have you forsaken him?
    Last edited by novacain; 11-10-2002 at 09:12 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. elliptical button
    By geek@02 in forum Windows Programming
    Replies: 0
    Last Post: 11-21-2006, 01:15 AM
  2. writing text over a deleted button
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-02-2005, 11:32 AM
  3. Window won't display on button command!?
    By psychopath in forum Windows Programming
    Replies: 6
    Last Post: 06-22-2004, 08:12 PM
  4. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21