自定义wxStaticBitmap的鼠标移动事件(或消息)

先在testwx1Main.cpp中定义一个函数如下,显示坐标:

void testwx1Frame::OnStaticBitmap1Move(wxMouseEvent& event)
{
    int X=event.GetX();
    int Y=event.GetY();
    TextCtrl1->SetLabel(wxString::Format(wxT("X:%i,Y:%i"),X,Y));
}

然后在头文件  testwx1Main.h 中声明这个函数:

void OnStaticBitmap1Move(wxMouseEvent& event);

接着在testwx1Frame类初始化函数里面,关联控件和消息函数:

testwx1Frame::testwx1Frame(wxWindow* parent,wxWindowID id)
{
    //...........................(*Initialize(testwx1Frame)
   
    StaticBitmap1->Connect(ID_STATICBITMAP1,wxEVT_MOTION,(wxObjectEventFunction)&testwx1Frame::OnStaticBitmap1Move,NULL, this);
}


有图有真相:

自定义wxStaticBitmap的鼠标移动事件(或消息)_第1张图片

完整的cpp代码如下:

/***************************************************************
 * Name:      testwx1Main.cpp
 * Purpose:   Code for Application Frame
 * Author:     ()
 * Created:   2012-12-15
 * Copyright:  ()
 * License:
 **************************************************************/

#include "testwx1Main.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(testwx1Frame)
#include <wx/bitmap.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(testwx1Frame)
const long testwx1Frame::ID_BUTTON1 = wxNewId();
const long testwx1Frame::ID_TEXTCTRL1 = wxNewId();
const long testwx1Frame::ID_STATICBITMAP1 = wxNewId();
const long testwx1Frame::idMenuQuit = wxNewId();
const long testwx1Frame::idMenuAbout = wxNewId();
const long testwx1Frame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(testwx1Frame,wxFrame)
    //(*EventTable(testwx1Frame)
    //*)
END_EVENT_TABLE()

testwx1Frame::testwx1Frame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(testwx1Frame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SetClientSize(wxSize(1075,265));
    SetMaxSize(wxSize(600,250));
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    Button1 = new wxButton(this, ID_BUTTON1, _("Label"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer1->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, _("Text"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    BoxSizer1->Add(TextCtrl1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticBitmap1 = new wxStaticBitmap(this, ID_STATICBITMAP1, wxBitmap(wxImage(_T("C:\\Users\\wxp\\Pictures\\wx1.jpg"))), wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER, _T("ID_STATICBITMAP1"));
    StaticBitmap1->SetMinSize(wxSize(500,300));
    BoxSizer1->Add(StaticBitmap1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    SetSizer(BoxSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    SetSizer(BoxSizer1);
    Layout();
    Center();

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&testwx1Frame::OnButton1Click);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&testwx1Frame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&testwx1Frame::OnAbout);
    Connect(wxEVT_MOTION,(wxObjectEventFunction)&testwx1Frame::OnMouseMove);
    //*)

    StaticBitmap1->Connect(ID_STATICBITMAP1,wxEVT_MOTION,(wxObjectEventFunction)&testwx1Frame::OnStaticBitmap1Move,NULL, this);
}

testwx1Frame::~testwx1Frame()
{
    //(*Destroy(testwx1Frame)
    //*)
}

void testwx1Frame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void testwx1Frame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void testwx1Frame::OnButton1Click(wxCommandEvent& event)
{
const char* a = "你好";
wxString b = wxString::FromUTF8(a);
wxMessageBox(b, wxString::FromUTF8("一切中文OK!"));
//比较方便的一种输入wxString类型字符串的方式
wxMessageBox(b, _("这样的输入中文-也行"));
//比较方便的一种输入wxString类型字符串的方式
wxMessageBox(b, wxT("这样的输入中文wxT也行"));
//wxMessageBox(b, "这样的中文不行");
}

void testwx1Frame::OnMouseMove(wxMouseEvent& event)
{
    int X=event.GetX();
    int Y=event.GetY();
    TextCtrl1->SetLabel(wxString::Format(wxT("X:%i,Y:%i"),X,Y));

}
void testwx1Frame::OnStaticBitmap1Move(wxMouseEvent& event)
{
    int X=event.GetX();
    int Y=event.GetY();
    TextCtrl1->SetLabel(wxString::Format(wxT("X:%i,Y:%i"),X,Y));
}


 

完整的头文件如下:

/***************************************************************
 * Name:      testwx1Main.h
 * Purpose:   Defines Application Frame
 * Author:     ()
 * Created:   2012-12-15
 * Copyright:  ()
 * License:
 **************************************************************/

#ifndef TESTWX1MAIN_H
#define TESTWX1MAIN_H

//(*Headers(testwx1Frame)
#include <wx/sizer.h>
#include <wx/menu.h>
#include <wx/textctrl.h>
#include <wx/statbmp.h>
#include <wx/button.h>
#include <wx/frame.h>
#include <wx/statusbr.h>
//*)

class testwx1Frame: public wxFrame
{
    public:

        testwx1Frame(wxWindow* parent,wxWindowID id = -1);
        virtual ~testwx1Frame();

    private:

        //(*Handlers(testwx1Frame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnButton1Click(wxCommandEvent& event);
        void OnMouseMove(wxMouseEvent& event);
        void OnStaticBitmap1Move(wxMouseEvent& event);
        //*)

        //(*Identifiers(testwx1Frame)
        static const long ID_BUTTON1;
        static const long ID_TEXTCTRL1;
        static const long ID_STATICBITMAP1;
        static const long idMenuQuit;
        static const long idMenuAbout;
        static const long ID_STATUSBAR1;
        //*)

        //(*Declarations(testwx1Frame)
        wxButton* Button1;
        wxStaticBitmap* StaticBitmap1;
        wxStatusBar* StatusBar1;
        wxTextCtrl* TextCtrl1;
        //*)

        DECLARE_EVENT_TABLE()
};

#endif // TESTWX1MAIN_H


 

你可能感兴趣的:(自定义wxStaticBitmap的鼠标移动事件(或消息))