...

View Full Version : simple c program with gui



rock_14
08-05-2007, 01:55 AM
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

ralph l mayo
08-06-2007, 06:08 AM
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.



#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. :/

oracleguy
08-06-2007, 04:21 PM
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.

ralph l mayo
08-06-2007, 04:33 PM
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.

oracleguy
08-06-2007, 05:25 PM
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.


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. :)

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?

rock_14
08-08-2007, 03:56 AM
Thanks, I will take a look at this. I really appreciate the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum