Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple c program with gui

    Hi,

    If I have a simple c program that prints "hello world" and I want to make a graphical page with a button that someone can click and then it will show "hello world" somewhere on the page, how can this be done?

    Any help would be greatly appreciated.

    Thanks

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    This is C++, but maybe it'll still help. NB I don't really know anything about this stuff, I just hacked it together from the official wxWidgets "Hello World" to include a button. You've got to have the wxWidgets headers installed to compile this, but it should work ok on any of the major platforms, providing I didn't do anything really stupid.

    Code:
    #include "wx/wx.h"
    
    class MyApp : public wxApp
    {
    	bool OnInit();
    };
    
    class MyFrame : public wxFrame
    {
    public:
    	MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
    
    	void DoHello(wxCommandEvent& event);
    };
    
    IMPLEMENT_APP(MyApp)
    
    bool MyApp::OnInit()
    {
    	MyFrame* frame = new MyFrame(
    		_T("Press butan"), // Title, _T makes wxStrings out of its arg 
    		wxPoint(50, 50),   // Suggested top left corner
    		wxSize(400, 200)   // width x height in px
    	);
    	frame->Show(TRUE);
    	SetTopWindow(frame);
    	return TRUE;
    }
    
    MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
    : wxFrame((wxFrame*)NULL, -1, title, pos, size)
    {
    	wxButton* button = new wxButton(
    		this,     // Place on this frame 
    		-1,       // This is the ID parameter, I think -1 means "assign anything convenient" but that's just a guess
    		_T("...") // Caption
    		          // optional params for placement follow
    	);
    	// Hook the button to the handler
    	Connect(
    		button->GetId(),                        // Maybe I should have suggested a specific id instead of -1 to avoid this?
    		wxEVT_COMMAND_BUTTON_CLICKED,           // the id of the event clicking raises
    		wxCommandEventHandler(MyFrame::DoHello) // wraps the event interface around a function reference
    	);
    }
    
    void MyFrame::DoHello(wxCommandEvent& WXUNUSED(event))
    {
    	wxMessageBox(
        		_T("Hello world"),                   // Caption
           		_T("Standard nub greeting follows"), // Title 
    		wxOK | wxICON_INFORMATION,           // Display options + extras
    		this                                 // I don't know, the parent window responsible for this?
    	);
    }
    I can't figure out how to attach an exit event without also including more extraneous code for a menubar, so it leaks those pointers. :/
    Last edited by ralph l mayo; 08-06-2007 at 06:11 AM.

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It doesn't leak any memory. Those wx objects that you dynamically created will automatically get cleaned up when the parent deconstructs, that is why you have to pass it the parent.

    And also if you want an exit event on your application you can override the OnExit (I think that is the name) function in your MyApp class.

    Also using the Connect method to link the event handling does work but you can use event tables in wxWidgets which are easier to use. And yes, you'd want to specify a specific ID number to the button. It works since you only have one control though.
    OracleGuy

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Thanks... I assumed I'd have to call the Close(bool) method to trigger garbage collection. I tried either OnExit() or OnQuit(), but it must have been the wrong one because I couldn't establish any evidence that it was ever called.

    I started out with the event table approach from their hello world but it wasn't clicking for me, probably because I didn't understand how ids worked. That approach is a probably better for happening at compile-time, but using those #defines feels a lot more hackish than Connect.

    Either way it seems infinitely preferable to what (admittedly little) I remember about the win32 native API.

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by ralph | mayo
    That approach is a probably better for happening at compile-time, but using those #defines feels a lot more hackish than Connect.
    I know what you mean but once you get used to it, it isn't too bad compared how many ways they could of done it worse.

    Quote Originally Posted by ralph l mayo View Post
    Either way it seems infinitely preferable to what (admittedly little) I remember about the win32 native API.
    Oh yeah... I had do some stuff with the Win32 API a few months ago and have doing lots of wxWidgets before that, it was painful.

    Edit: Oh, but if you can't use C++, you'll probably have to use the native API for whatever platform you are using. Is this going to be a Windows program?
    Last edited by oracleguy; 08-06-2007 at 05:30 PM.
    OracleGuy

  • #6
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I will take a look at this. I really appreciate the help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •