⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mouclickmain.cpp

📁 wxWidgets的程序
💻 CPP
字号:
/*************************************************************** * Name:      MouClickMain.cpp * Purpose:   Code for Application Frame * Author:    lj2505 () * Created:   2008-09-12 * Copyright: lj2505 () * License: **************************************************************/#include "MouClickMain.h"#include <wx/msgdlg.h>//(*InternalHeaders(MouClickFrame)#include <wx/intl.h>#include <wx/string.h>//*)#include <wx/dcclient.h>//helper functionsenum 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(MouClickFrame)const long MouClickFrame::idMenuQuit = wxNewId();const long MouClickFrame::idMenuAbout = wxNewId();const long MouClickFrame::ID_STATUSBAR1 = wxNewId();//*)BEGIN_EVENT_TABLE(MouClickFrame,wxFrame)    //(*EventTable(MouClickFrame)    //*)END_EVENT_TABLE()MouClickFrame::MouClickFrame(wxWindow* parent,wxWindowID id){    m_bmp.LoadFile( _T("ico.bmp"),wxBITMAP_TYPE_BMP); //装如位图    oldpoint.x=70;    oldpoint.y=70;    curpoint.x=70;    curpoint.y=70;    x=0.0;    y=0.0;    //(*Initialize(MouClickFrame)    wxMenuItem* MenuItem2;    wxMenuItem* MenuItem1;    wxMenu* Menu1;    wxMenuBar* MenuBar1;    wxMenu* Menu2;    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));    SetClientSize(wxSize(400,345));    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);    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&MouClickFrame::OnQuit);    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&MouClickFrame::OnAbout);    Connect(wxID_ANY,wxEVT_PAINT,(wxObjectEventFunction)&MouClickFrame::OnPaint);    Connect(wxID_ANY,wxEVT_LEFT_DOWN,(wxObjectEventFunction)&MouClickFrame::OnLeftDClick);    Connect(wxID_ANY,wxEVT_RIGHT_UP,(wxObjectEventFunction)&MouClickFrame::OnLeftDown);    //*)}MouClickFrame::~MouClickFrame(){    //(*Destroy(MouClickFrame)    //*)}void MouClickFrame::OnQuit(wxCommandEvent& event){    Close();}void MouClickFrame::OnAbout(wxCommandEvent& event){    wxString msg = wxbuildinfo(long_f);    wxMessageBox(msg, _("Welcome to..."));}void MouClickFrame::OnLeftDown(wxMouseEvent& event){    wxMessageBox(_T("Hi,你单击了一下窗体!"), _("right click"));}void MouClickFrame::OnPaint(wxPaintEvent& event){    wxPaintDC dc(this); //创建DC    dc.DrawBitmap(m_bmp, curpoint.x, curpoint.y, true); //显示图片}void MouClickFrame::RePaint(float dx,float dy,int flag){//dx和dy分别是x,y轴上的变化量,flag是变化方向标记。//在此处,x轴每次的变化量为1,而y轴为 y轴变化量/x轴变化量。    double fd=fabs(dx); //取绝对值    switch (flag)    {    case 0:        for (int i=0;i<fd;i++)        {            curpoint.x=oldpoint.x-i;            curpoint.y=oldpoint.y+int(dy/fd*i);            /*下面的两个方法要一起用才能显示物体的运动轨迹,清参见:http://www.wxwidgets.org/manuals/2.8.0/wx_wxwindow.html#wxwindowvalidate上关于这两个方法的介绍*/            Refresh();            Update();        }        break;    case 1:        for (int i=0;i<fd;i++)        {            curpoint.x=oldpoint.x-i;            curpoint.y=oldpoint.y+int(dy/fd*i);            Refresh();            Update();        }        break;    case 2:        for (int i=0;i<fd;i++)        {            curpoint.x=oldpoint.x+i;            curpoint.y=oldpoint.y+int(dy/fd*i);            Refresh();            Update();        }        break;    case 3:        for (int i=0;i<fd;i++)        {            curpoint.x=oldpoint.x+i;            curpoint.y=oldpoint.y+int(dy/fd*i);            Refresh();            Update();        }        break;    }    oldpoint=curpoint; //记录下当前坐标}void MouClickFrame::OnLeftDClick(wxMouseEvent& event){    int flag;//获得当前点击的坐标    curpoint.x=event.GetX();    curpoint.y=event.GetY();//计算变化量    x=curpoint.x-oldpoint.x;    y=curpoint.y-oldpoint.y;//定义变化方向标记    if (x<0&&y<0) flag=0;    else if (x<0&&y>0) flag=1;    else if (x>0&&y<0) flag=2;    else flag=3;//调用重绘函数进行重绘    x-=64;    y-=64;    RePaint(x,y,flag);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -