wxBitmapButton

From WxWiki
Jump to: navigation, search
Official Classes SmallBlocks.png Archive Containers Controls Data Structures Database Date & Time Debug Device Contexts Dialogs Document & Views Drag & Drop Events Filesystem Frames Graphics Grid Cell Help HTML Logging Miscellaneous Networking Printing Sizers Streams Threading Windows

A bitmap button is a control that contains a bitmap.

Note: Since wxWidgets 2.9.1 bitmap display is supported by the base wxButton class itself and the only tiny advantage of using this class is that it allows to specify the bitmap in its constructor, unlike wxButton. Please see the base class documentation for more information about images support in wxButton.

Sample Code

This short demo program shows how to use wxBitmapButton:

bool HelloWorldApp::OnInit()
{
    wxFrame *frame = new wxFrame((wxFrame*) NULL, -1, _("Hello World"));

    wxPanel *panel = new wxPanel(frame, -1, wxPoint(0, 100), wxSize(320, 100));

    wxBitmap bitmap;
    bitmap.LoadFile("Blue.bmp", wxBITMAP_TYPE_BMP);

    // The last parameter (0) - so we have 2D behaviour
    wxBitmapButton *button1 = new wxBitmapButton(
        panel, -1, bitmap, wxPoint(10, 10), wxSize(50, 50), 0);

    frame->Show(TRUE);
    SetTopWindow(frame);
    return true;
}

wxBASIC Example

frame = new wxFrame( Null, -1, "Graphic demo for wxBasic",
                     wxPoint(20, 20), wxSize(320, 200 + 25))

' Panel covers only bottom half of window.
panel = new wxPanel( frame, -1 ,wxPoint(0, 100), wxSize(320, 100))

' If you create only one panel, it will cover ALL the
' window, so we create an "invisible" second panel.
dummy_panel = new wxPanel(frame, -1, wxPoint(-1, -1), wxSize(0, 0))

' Create an empty bitmap, size does not matter.
bmp = new wxEmptyBitmap(0, 0)
bmp2 = new wxEmptyBitmap(0, 0)
' Allocate memory: this space will be used to store the bitmap.
bmpDC = wxMemoryDC()

' You can load an existing BMP/GIF into your image (wxBITMAP_TYPE_GIF).
' **** Spcecify the right path for your image! ****
bmp.LoadFile("f:/documenti/calcio.bmp", wxBITMAP_TYPE_BMP)
bmp2.LoadFile("f:/documenti/calcio.bmp", wxBITMAP_TYPE_BMP)

' To draw onto your bitmap, you must associate it to a pre-allocated memory space.
bmpDC.SelectObject(bmp)
' Now you can draw your bitmap.
bmpDC.DrawLine(0, 0, 10, 20)

' You can also insert this bitmap inside a button.
button1 = new wxBitmapButton(panel, -1, bmp2, wxPoint(10, 10), wxSize(-1, -1))

' If you specify wxNO_3D, button will be "flat", looking like a normal
' bitmap: so you can draw bitmaps inside a panel (otherwise, you could
' only draw them onto the main frame).
button2 = new wxBitmapButton(panel, -1, bmp2, wxPoint(60, 10), wxSize(50, 50))

Sub onPaint( event )
    dc = wxPaintDC( frame )
    dc.BeginDrawing()
    dc.Blit(0,0,320,200,bmpDC,0,0)
    dc.EndDrawing()
End Sub

Connect(frame, -1, wxEVT_PAINT, "onPaint")
frame.Show(True)

See Also