Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / Problem with mouse functions

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Problem with mouse functions
#3231
Posted by: 2004-01-14 07:24:30
I've been unable to sucessfully like various mouse buttons (ie GetButton() ).  I included the code below, which causes a "[linker error] undefined reference to 'MainFrame::GetButton()'"

//
// The .cpp file
//
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include "main.h"

IMPLEMENT_APP(MainApp) // Initializes the MainApp class and tells our program
// to run it
bool MainApp::OnInit()
{
MainFrame *MainWin = new MainFrame("Hello World!", wxPoint(1,1),
wxSize(300, 200)); // 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)
{
CreateStatusBar(2);
MainMenu = new wxMenuBar();
wxMenu *FileMenu = new wxMenu();

FileMenu->Append(MENU_Quit, "&Quit", "Quit");

MainMenu->Append(FileMenu, "File");
SetMenuBar(MainMenu);

Maximize();
}

void MainFrame::Other()
{
// -------------------

SetStatusText(GetButton(), 0);

// ^^^^^^^^^^^^^^^^^^^

}

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

//
// The .h file
//
#ifndef __BASE_H // Make sure to only declare these classes once
#define __BASE_H
#include <wx/event.h>
#include <wx/menu.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
public:
MainFrame( const wxString &title, const wxPoint &pos, const wxSize &size );

wxMenuBar *MainMenu;
wxString GetButton();

void Other();
void Quit();

DECLARE_EVENT_TABLE()
};

enum
{
TEXT_Main = wxID_HIGHEST + 1, // declares an id which will be used to call our button
FUNC_Other,
MENU_Quit
};

BEGIN_EVENT_TABLE ( MainFrame, wxFrame )
  EVT_MENU(MENU_Quit, MainFrame::Quit)
  EVT_MENU(FUNC_Other, MainFrame::Other)
  EVT_LEFT_DOWN(MainFrame::Other)
  EVT_RIGHT_DOWN(MainFrame::Other)
END_EVENT_TABLE()
#endif
Thanks
Message2. Re: Problem with mouse functions
#3243
Posted by: upCASE 2004-01-15 02:33:52
Hi!
Well... I must admit that I'm not completely sure what you're up to, but I guess that you want to display which mouse button was pressed in the statusbar of the main window.

First of all: The error message is correct, perfectly :)
Proplem is that you defined a function wxString GetButton(); in your MainFrame class, but didn't implement it in the cpp source. So, the linker now looks for a function called GetButton() in the class MainFrame and finds none, because there's none :)
That's what's called an undefined reference, because in your code you reference the function but it can't be found. Implement it and the error will vanish :)

I'm pretty sure that you meant to use wxMouseEvent::GetButton(). There is no event you could use that function on, so it won't work either way as you want it.
I'll write a short example program asap and post it here, based on your code.
Be right back :)

upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message3. Re: Problem with mouse functions
#3244
Posted by: upCASE 2004-01-15 02:56:33
Hi!
Have a look at this.

#ifndef __BASE_H
#define __BASE_H

class MainApp: public wxApp
{
  public:
      virtual bool OnInit();
};

class MainFrame: public wxFrame
{
  public:
      MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size);
      void OnMouse( wxMouseEvent &event);
      void OnQuit(wxCommandEvent &event);
  private:
      DECLARE_EVENT_TABLE()
};

enum
{
   ID_MAINWIN_QUIT = wxID_HIGHEST+1
   
};

#endif


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

#include "base.h"

IMPLEMENT_APP(MainApp)

bool MainApp::OnInit()
{
   MainFrame *win = new MainFrame("Frame", wxPoint (100, 100),
     wxSize(450, 340));
   win->Show(TRUE);
   SetTopWindow(win);

   return TRUE;
}


BEGIN_EVENT_TABLE(MainFrame, wxFrame)
   EVT_MENU(ID_MAINWIN_QUIT, MainFrame::OnQuit)
   EVT_MOUSE_EVENTS(MainFrame::OnMouse) 
END_EVENT_TABLE()

MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
    : wxFrame((wxFrame *) NULL, -1, title, pos, size)
{
    wxMenu *FileMenu = new wxMenu;
    wxMenuBar *MenuBar = new wxMenuBar;

    FileMenu->Append(ID_MAINWIN_QUIT, "&Quit");

    MenuBar->Append(FileMenu, "&File");
    SetMenuBar(MenuBar);

    CreateStatusBar(1);
    SetStatusText("Hello World!");

}

void MainFrame::OnMouse(wxMouseEvent &event)
{
    if( event.IsButton() )
        SetStatusText( wxString::Format("%d. button pressed",event.GetButton()));
    else if(event.Moving())
        SetStatusText("The mouse was moved...");
}

void MainFrame::OnQuit(wxCommandEvent & WXUNUSED(event))
{
    Close(TRUE);
}

For questions: Post here or mail me in private.

upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message4. Re: Problem with mouse functions
#3248
Posted by: 2004-01-16 12:13:23
Thanks a lot.  Thats exactly what I wanted.  Thanks again
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
¥288.00 ·
 
 
¥1,580.00 ·
 
 
¥470.00 ·
 
 
¥317.00 ·
 
 
¥489.00 ·
 
 
¥1,068.00 ·
© Mon 2024-11-11  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0