Buttons + Edit Control

This is a discussion on Buttons + Edit Control within the Windows Programming forums, part of the Platform Specific Boards category; Im making a calculator and i want to press a button and display the value in the edit control using ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    15

    Buttons + Edit Control

    Im making a calculator and i want to press a button and display the value in the edit control using SetDlgItemInt().Not sure if im using it correctly but my code isn't compiling correctly the code runs ok if i remove the function.Any help much appreciated

    Code:
    //Includes the WinAPI
    #include <windows.h>
    
    // Variables
    HWND hwndMain, hwndEdit, hwndButton; // Main window HWND and the edit window HWND
    
    COLORREF crEditBg, crEditFg;
    HBRUSH hbrEditBg;
    
    int number = 5;
    
    // Functions
    //Callback function
    LRESULT CALLBACK MainWndProc(HWND hwnd,			// Handle of window which received this message
    							 UINT msg,			// The message
    							 WPARAM wParam,		// Extra Information
    							 LPARAM lParam);	// Extra Information
     
    
    //The entry point of a windows application
    int WINAPI 
    WinMain(HINSTANCE hInstance,			/* Handle to the current instance*/
    		HINSTANCE hPrevInstance,		/* Handle to the previuos instance*/
    		LPSTR lpCmdLine,				/* Pointer to the command line arguements*/ 
    		int nCmdShow)					/* Show state of the window wether the window should be maximised or minimised*/
    {
    	MSG msg;							// MSG structure to store messages
    	WNDCLASSEX wcx;						// WINDOW class iunformation
    
    	// Fill in the WNDCLASS Struct
    	wcx.cbSize = sizeof(WNDCLASSEX);	// Window size. Must always be sizeof(WNDCLASSEX)
    	wcx.style = 0;						// Class Styles
    	wcx.lpfnWndProc = MainWndProc;		// Pointer to the callback procedure******
    	wcx.cbClsExtra = 0;					// Extra byte to allocate following the wndclassex structure
    	wcx.cbWndExtra = 0;					// Extra byte to allocate following an instance of the structure
    	wcx.hInstance = hInstance;			// Instance of the application
    	wcx.hIcon = LoadIcon (NULL, IDI_APPLICATION);  //Class Icon
    	wcx.hCursor = LoadCursor (NULL, IDC_ARROW);  //Class Cursor
    	wcx.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);  //Background brush/Colour
    	wcx.lpszMenuName = NULL;			// Main Resource
    	wcx.lpszClassName = "Draw";			// Name of this class
    	wcx.hIconSm = NULL;					// Small icon for the window
    
    	// Register class with MS-Windows
    	if(!RegisterClassEx(&wcx))
    		return 0;
    	
    	//Create instances of the window now that it has been registered
    	hwndMain = CreateWindowEx(WS_EX_CLIENTEDGE,  //Extended window style
    					"Draw",				// Window class name
    					"Calculator",		// Window Title
    					WS_OVERLAPPEDWINDOW, //Window Style
    					CW_USEDEFAULT,		// Default X-position of window
    					CW_USEDEFAULT,		// Default Y-position of window
    					170,				// Width of the window
    					220,				// Height of the window
    					NULL,				// Handle of parent window
    					NULL,				// Handle to menu
    					hInstance,			// Handle to application instance
    					NULL);				// Pointer to window creation data
    
    	// Initialise colors
    	crEditFg = RGB(0,0,0);				// BLACK ON
    	crEditBg = RGB(255,255,255);		// WHITE
    
    // Create Edit window
    	hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
    							"EDIT",
    							"0",
    							WS_BORDER|WS_VISIBLE|WS_CHILD|ES_NUMBER|ES_RIGHT,
    							0,0,160,30,
    							hwndMain,
    							NULL,
    							hInstance,
    							NULL);
    
    /////////////Create Buttons Here//////////
    	hwndButton = CreateWindowEx(0,"BUTTON","1",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,110,40,40,hwndMain,NULL,hInstance,NULL);
    	
    	hwndButton = CreateWindowEx(0,"BUTTON","2",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,40,110,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","3",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,80,110,40,40,hwndMain,NULL,hInstance,NULL);
    	
    	hwndButton = CreateWindowEx(0,"BUTTON","4",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,70,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","5",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,40,70,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","6",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,80,70,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","7",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,30,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","8",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,40,30,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","9",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,80,30,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","0",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,0,150,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","+",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,120,30,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","-",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,120,70,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","*",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,120,110,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","/",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,120,150,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","=",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,40,150,40,40,hwndMain,NULL,hInstance,NULL);
    
    	hwndButton = CreateWindowEx(0,"BUTTON","C",WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,80,150,40,40,hwndMain,NULL,hInstance,NULL);
    
    	ShowWindow(hwndMain,nCmdShow);
    
    	//^^^^Code above creates the window + edit box + buttons and shows them^^^^ 
    
    	//Process messages coming to this window
    	//this code is polling the system 
    	while(GetMessage(&msg,NULL,0,0))
    	{
    		TranslateMessage(&msg);			// Translates virtual key messages to character mesages
    		DispatchMessage(&msg);		
    	}
    	//Return value to the system
    	return msg.wParam;
    }
    
    // Callback function for the Main Window class
    LRESULT CALLBACK MainWndProc(HWND hwndMain,  // Handle of window which received this message(you may have created more then 1 window)
    							 UINT msg,		 // The message which has been sent
    							 WPARAM wParam,  			
                     LPARAM lParam)  // Extra information
    {
    	switch(msg)
    	{
    	case WM_DESTROY:					// This is the message sent WM_DESTROY
    		//User closed the window
    		PostQuitMessage(0);				// The arguement to PostQuitMessage is the return value of the system
    		break;
    		//Code to show number in Edit Control
    	case WM_COMMAND:
    		if(LOWORD(wParam) == 1){
                    if(HIWORD(wParam) == BN_CLICKED){
    					SetDlgItemInt(HWND,hwndEdit,number,TRUE);
                    }
               }
    		break;
    	default:
    		//Call the default window handler
    		return DefWindowProc(hwndMain,msg,wParam,lParam);
    	}
    	return 0;
    }

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    I got your code to work. First off you need to make sure, where you are creating the button windows that the hwnd and Main don't get separated. That might have been just a typo. Second The SetDlgItemInt function. You have it like this:
    Code:
    SetDlgItemInt(HWND,hwndEdit,number,TRUE);
    It should be like this, I looked up the syntax on msdn:
    Code:
    SetDlgItemInt(hwndEdit,1,number,TRUE);
    .
    I don't know if the 1 is the value you wanted but that parameter needs to be an integer, so I just plugged 1 in. It compiled fine after I changed that. Hope that helps.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    The code compiles fine now thanks but it doesn't display the value in the window any suggestions?

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    You mean the hwndEdit window when you click a button it won't show? I noticed that and was checking it out. It looks like you didn't make a window process for the buttons. So when you click them the messages have nowhere to go so they don't do anything.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    I'm a 100% newbie so can you explain to me what that means or how i make a window process for the buttons.very grateful.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    15
    Well I see now that i have to give the buttons seperate ID's but still its not working

  7. #7
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yes they need separate ID's. That's the easiest way to go. There is another way to do it but you have to write a whole separate LRESULT CALLBACK function, like the one you write for the window. The one that looks like this:
    Code:
    LRESULT CALLBACK WndProc(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
    {
    That was the way I was talking about, it takes a little more work but it is more flexible. For your purposes defining an ID should work. I wrote this simple program that has a button in a window that when clicked a message box pops up. I will just post the program's whole code, since its only function is to have that button and when clicked pop up a message box so it shouldn't be confusing at all.
    Code:
    #include <windows.h>
    #define ID_PUSHBTN 0		/* Defining the button's ID. */
    
    static int x,y;
    HWND hwnd2;
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
    {
    static TCHAR szAppName[]=TEXT("Button test");
    MSG msg;
    HWND hwnd;
    WNDCLASS wc;
    x=GetSystemMetrics(SM_CXSCREEN);
    y=GetSystemMetrics(SM_CYSCREEN);
    
    wc.style=CS_HREDRAW|CS_VREDRAW;
    wc.lpfnWndProc=WndProc;
    wc.cbWndExtra=0;
    wc.cbClsExtra=0;
    wc.hInstance=hInstance;
    wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wc.hCursor=LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName=NULL;
    wc.lpszClassName=szAppName;
    
    if(!RegisterClass(&wc))
    {
    MessageBox(NULL,"This program requires Windows NT!","ERROR",MB_ICONERROR);
    return 0;
    }
    hwnd=CreateWindow(szAppName,szAppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    hwnd2=CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON","Click me!",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,x/2,y/2,75,25,hwnd,ID_PUSHBTN,hInstance,NULL);		/* The button's window being created. */
    
    ShowWindow(hwnd,SW_MAXIMIZE);
    UpdateWindow(hwnd);
    
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }
    LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)		/* Main window's CALLBACK function. */
    {
    PAINTSTRUCT ps;
    HDC hdc;
    switch (message)
    {
    case WM_COMMAND:		/* This is the part you need to look at */
    switch(LOWORD(wParam))		/* This is where the message that the button has been clicked is sent */
    {
    case ID_PUSHBTN:			/* Just like the WM cases but it is case BUTTON_ID
    MessageBox (NULL, "Button has been clicked!" , "Clicked!", MB_ICONQUESTION);		/* The code to be executed when the button is clicked. */
    break;
    }
    return 0;
    
    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
    That code should work, it does for Dev-C++.
    Last edited by jmd15; 03-09-2005 at 05:40 PM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 12:56 PM
  2. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 07:58 AM
  3. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 10:52 PM
  4. endless edit control
    By ZerOrDie in forum Windows Programming
    Replies: 3
    Last Post: 03-21-2003, 02:51 AM
  5. Keeping focus on an edit control ...
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 02-19-2002, 02:12 AM

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