Code: Select all
#include <wx/event.h>
#include <wx/process.h>
#include "MyControl.h"
#include "TestApp.h"
#include <wx/dnd.h>
#include <wx/sizer.h>
#include <wx/app.h>
DECLARE_APP(TestFrmApp);
BEGIN_EVENT_TABLE(MyControl,wxPanel)
EVT_LEFT_DOWN(MyControl::LeftDown)
EVT_LEFT_UP(MyControl::LeftUp)
EVT_MOTION(MyControl::Moving)
EVT_MOUSE_CAPTURE_LOST(MyControl::MouseCaptureLost)
// catch paint events
EVT_PAINT(MyControl::paintEvent)
END_EVENT_TABLE()
MyControl::MyControl(wxWindow * parent,wxBitmap *map, wxString &Label, wxPoint pos,const wxSize& size) : wxPanel(parent,wxID_ANY,pos,size)
{
mymap = *map; // make a local copy
SetForegroundColour(*wxWHITE);
SetSize(mymap.GetWidth()+50,mymap.GetHeight()+50);
paintNow();
Move(pos);
// SetOwnBackgroundColour(*wxLIGHT_GREY);
dragging = false;
LabelText = Label;
}
MyControl::~MyControl()
{
}
void MyControl::paintEvent(wxPaintEvent & evt)
{
wxPaintDC dc(this);
render(dc);
}
void MyControl::paintNow()
{
wxClientDC dc(this);
render(dc);
}
void MyControl::render(wxDC& dc)
{
dc.DrawBitmap( mymap, 0, 0, true );
dc.DrawText(LabelText,0,mymap.GetHeight());
}
void MyControl::LeftDown(wxMouseEvent& event)
{
CaptureMouse();
dragging = true;
Xpos = event.GetX();
Ypos = event.GetY();
}
void MyControl::Moving(wxMouseEvent& event)
{
if (dragging)
{
wxPoint mouseOnScreen = wxGetMousePosition();
int newx = mouseOnScreen.x - Xpos;
int newy = mouseOnScreen.y - Ypos;
this->Move( this->GetParent()->ScreenToClient( wxPoint(newx, newy) ));
}
}
void MyControl::LeftUp(wxMouseEvent& event)
{
// TODO Need code to prevent moving on top of another MyControl
ReleaseMouse();
dragging=false;
Hide();
Show();
}
void MyControl::MouseCaptureLost(wxMouseCaptureLostEvent& event)
{
// don't call event.skip
wxMessageBox("Lost Mouse");
}