Guidance Forums / wxWidgets (wxWindows) in C++ / Why won't my image appear??

Search 搜索
Home Home

Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Why won't my image appear??
Posted by: 2004-02-10 03:48:24
I've been trying everything I can to load and show an image onto my window, but to no avail.  I've read all of the wxDC documents I could find (including the sample programs), but fail to make much sense of them.  I included my code below; Could someone tell me what I'm doing wrong, or give me some example code.


#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>

#include "base.h"

IMPLEMENT_APP(MainApp) // Initializes the MainApp class and tells our program to run it

bool MainApp::OnInit()
wxImage::AddHandler( new wxGIFHandler );

wxImage Pic_Splash; // Create an image variable

if (Pic_Splash.LoadFile("bitmaps\\splash.gif", wxBITMAP_TYPE_GIF)) // If the bitmap loads, show the splash screen
      new wxSplashScreen(Pic_Splash,
          6000, NULL, -1, wxDefaultPosition, wxDefaultSize,
// Create the window
MainFrame *MainWin = new MainFrame("BlakkCount", wxPoint(1, 1),
wxSize(290, 190)); // Create an instance of our frame, or window
MainWin->Show(TRUE); // show the window
SetTopWindow(MainWin);// and finally, set it as the main window

return TRUE;

MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
: wxFrame((wxFrame *) NULL, -1, title, pos, size)
Center(); // Center the window
SetSizeHints(290, 190, 290, 190, -1, -1); // Set the windows min and max size


//BackgroundBMP = new wxBitmap;
MainMenu = new wxMenuBar(); // Construct a menu bar
wxMenu *FileMenu = new wxMenu(); // Construct a menu
wxMenu *OtherMenu = new wxMenu(); // Construct a menu

// Add items to menu
FileMenu->Append(MENU_Open, "&Open", "Open a file");
FileMenu->Append(MENU_Close, "&Close", "Close currently open file");
FileMenu->Append(MENU_Quit, "&Quit", "Quit the program");
OtherMenu->Append(MENU_About, "&About", "About the program");
// Add items to menu bar
MainMenu->Append(FileMenu, "File");
MainMenu->Append(OtherMenu, "Other");

SetMenuBar(MainMenu); // Show the menu bar

TestIcon.LoadFile("bitmaps\\ball2.ico", wxBITMAP_TYPE_ICO);

// ---------------------------------------
if (TestIcon.Ok()) SetStatusText("LOADED ICON!");
else SetStatusText("FAILED TO LOAD ICON!");

TestIconDC.DrawIcon(TestIcon, 1, 1);
// ---------------------------------------


void MainFrame::OpenFile()
wxTextFile *TxtFile;
wxFileDialog *OpenDialog = new wxFileDialog(this, "Choose a file to open", "", "", "Text files (*.txt)|*.txt|", wxOPEN, wxDefaultPosition);
// Creates a "open file" dialog with 4 file types
if (OpenDialog->ShowModal() == wxID_OK) // if the user click "Open" instead of "cancel"
CurrentDocPath = OpenDialog->GetPath();

delete NoFileLoaded;

SetFont(wxFont(10, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE, wxEmptyString, wxFONTENCODING_DEFAULT));

LinesInFile = new wxStaticText(this,
                 "Lines in File: ",

SetTitle(wxString("BlakKount - ") << OpenDialog->GetFilename()); // Set the Title to reflect the file open

void MainFrame::CloseFile()
LinesInFile->SetLabel("Lines in File: ");

void MainFrame::Maximize()
SetStatusText("Yeah, just try maximizing the window again");

void MainFrame::Quit()
Close(TRUE); // Tells the OS to quit running this process

void MainFrame::About()
wxDialog AboutDlg(this, DLG_Main, "About",
    wxDefaultPosition, wxDefaultSize,

if (AboutDlg.ShowModal() == TRUE) SetStatusText("Oh yeah");


#ifndef __BASE_H // Make sure to only declare these classes once
#define __BASE_H

#include <wx/window.h>
#include <wx/image.h>
#include <wx/splash.h>
#include <wx/bitmap.h>
#include <wx/textctrl.h>
#include <wx/menu.h>
#include <wx/filedlg.h>
#include <wx/msgdlg.h>
#include <wx/event.h>
#include <wx/object.h>
#include <wx/textfile.h>
#include <wx/cmndata.h>
#include <wx/dc.h>
#include <wx/dcmemory.h>
#include <wx/icon.h>

class MainApp: public wxApp // MainApp is the class for our application
{ // MainApp just acts as a container for the window,
public: // or frame in MainFrame
virtual bool OnInit();

class MainFrame: public wxFrame // MainFrame is the class for our window,
{ // It contains the window and all objects in it
MainFrame( const wxString &title, const wxPoint &pos, const wxSize &size );

wxIcon TestIcon;
wxDC TestIconDC;
wxStaticText *NoFileLoaded; // Notification that there is no file loaded
wxStaticText *LinesInFile; // The number of lines in the file that is open
wxString CurrentDocPath; // The Path to the file that is open
wxString CurrentFilename; // The filename of the file that is open
wxTextCtrl *MainTextBox; // Create a text control variable
wxMenuBar *MainMenu; // Create a menu bar variable

/*  Initialize functions  */
void OpenFile();
void CloseFile();
void Quit();
void About();
bool DrawImages(wxDC &dc);

void Maximize();
/*  End initializations  */

DECLARE_EVENT_TABLE() // This is where the stuff happens

TEXT_Main = wxID_HIGHEST + 1, // declares an id which will be used to call our button

BEGIN_EVENT_TABLE ( MainFrame, wxFrame )
  EVT_MAXIMIZE(MainFrame::Maximize) // When maximize event is called
  EVT_MENU(MENU_Open, MainFrame::OpenFile) // When event menu "Open" is called
  EVT_MENU(MENU_Close, MainFrame::CloseFile) // When event menu "Close" is called
  EVT_MENU(MENU_Quit, MainFrame::Quit) // When event menu "Quit" is called
  EVT_MENU(MENU_About, MainFrame::About) // When event menu "About" is called
Message2. Re: Why won't my image appear??
Posted by: upCASE 2004-02-10 06:56:03
The way I see it is that your create a wxDC and try to paint on it. So far so good, but that won't work :)
You'll have to use one of the following classes derived from wxDC:
wxPaintDC, wxClientDC, wxWindowDC or wxScreenDC. Which one you choose depends on where you want to draw (normally a wxClientDC is fine to draw on widgets and panels).
DC means "Device context", so you'll have to define first where to paint on, then paint.

Let's say I have a panel called superPanel somewhere in my code and I want to draw on it.
wxClientDC dc(superPanel);
It's easy :)

If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message3. Thanks, but I have one more problem
Posted by: 2004-02-10 09:42:23
Thanks.  I got my background to appear when I loaded and showed the image in the OnInit() function, but whenever the window was minimized or anything else happened to the window, the image would disappear.
Message4. Re: Why won't my image appear??
Posted by: KaReL 2004-02-10 20:56:27
redraw it in the size event ;)

Message5. Re: Why won't my image appear??
Posted by: upCASE 2004-02-11 04:22:51
Once the window gets refreshed the DC will be cleared. You can either do what Karel said or, if you want to show the image permanent, handle EVT_PAINT for that window and draw the image there using a wxPaintDC.

If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
© Sun 2024-4-14  Guidance Laboratory Inc. Hits:0