A second tutorial: part 2 variables and initialisation

This tutorial makes a  simple wxWidgets program to edit XPM format files

Variables:
We need to have wxBitmap to hold the image, which has to always exist. We also need a filename and a color to edit the bitmap with. It is easiest to create these as part of MyFrame, so right click on the grey bar at the top of the MenuZone, to the right of the menus, and choose Go Frame Declaration. Add code like this within the declaration fo MyFrame:

    wxBitmap myBitmap ;
    wxString myFilename ;
    wxColour myColour ;
 

Initialisation:
The bitmap needs to be setup in the New 16x16 or New 32x32 code, and when the program starts up.

The easist way is to ensure that the code is not repeated unnecessarily is to create a function, but this will not be a direct part of the interface. Right-click in the gray bar at the top of the menu zone and choose Go Non GUI Method. Declare the method, for example, void DoNew (int i)

Now insert the appropriate body code like this:
    myBitmap = wxBitmap ( i, i );

    wxMemoryDC temp_dc;
    temp_dc.SelectObject(myBitmap);
//draw it as a black square in memory
    temp_dc.SetBrush(*wxBLACK_BRUSH);
    temp_dc.SetPen(*wxBLACK_PEN);
    temp_dc.DrawRectangle(0,0,i,i);
    canvas->Refresh();

Now add calls to DoNewfrom the New 32 x 32, New 16 x 16 Methods, and the Frame Constructor. To find these methods, go to the Menu Zone (Ctrl + W) and double click the appropriate Menu replicas and add DoNew (32); or DoNew (16); To find the Frame Constructor, right click in the menuzone grey bar and  choose Go Frame Constructor and add a line DoNew (32); after the line canvas = new MyCanvas(this);

It is easiet to set the inital drawing color to white just before calling DoNew from the frame constructor
       myColour = *wxWHITE ;

These are links to the modified header (.h) and code (.cpp) files
 

Next
Back


wxHatch Home link to wxhatch help index