wxWidgets的两个问题

l360220954 2013-05-06 04:23:23
各位大神好:有一个跨平台类库wxWidgets的两个问题想听取大家的指导!在这里先说下我的需求:在我们的软件中,有可能会处理到按钮重叠或者覆盖的问题,当两个按钮重叠的时候,我们希望在客户点击重叠区域的时候,两个按钮都是可以响应的;当两个按钮相互覆盖的时候,我们需要点击到上面的按钮,同时是在下面的按钮所在的区域的时候,两个按钮也都是可以响应的。
在windows下平台上,这个问题基本上已经解决了,但是在linux平台上,我把windows上的代码放在linux上,结果点击空白区域的时候会死机。最后会附上我写的代码,请各位大神多加指点。

我所做的思路是,用wxPanel派生出一个新的类出来。在这个类里,定义一个函数的映射:EVT_LEFT_DOWN(MyPanel::OnButtonClick),这个函数处理的鼠标按下的消息,在这个函数里,得到鼠标点击的坐标,然后判断该坐标是否在两个按钮的范围之内,在谁的范围内就调用谁的处理函数,当在两个按钮的公共区域内的话,这两个函数的逻辑处理函数就全都得到调用。

用这个思路写成的代码在windows下可以很正常的起作用:当点击在按钮区域的时候,分别会有按钮的处理函数得到调用;当点击空白区域的时候,也会按照函数中所写的那样,弹出点击坐标的对话框。但是在linux平台下的时候,点击空白区域的时候,不会弹出什么对话框,而是会造成到整个程序死机。另外,linux平台和windows下的另外一点不同,就是这两个平台上按钮出现的顺序是相反的。windows上代码先创建的按钮在上面,而linux下正好相反。这又是什么原因,请各位大神帮我分析一下!

请各位大神帮我分析一下这两个问题啊:
1 为什么linux平台和windows平台上按键显示出来的顺序会相反
2 为什么在linux平台上点击空白区域的时候会死机,而不能弹出像预想的那样弹出含有坐标信息的对话框

谢谢啦!

附代码:在两个平台都是可以运行的
#if 0
#include "button.h" //lxx add
#else
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------

// the application icon (under Windows and OS/2 it is in resources and even
// though we could still include the XPM here it would be unused)
#ifndef wxHAS_IMAGES_IN_RESOURCES
#include "../sample.xpm"
#endif

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
// override base class virtuals
// ----------------------------

// this one is called on application startup and is a good place for the app
// initialization (doing it here and not in the ctor allows to have an error
// return: if OnInit() returns false, the application terminates)
virtual bool OnInit();
};

class MyPanel;
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title);

// event handlers (these functions should _not_ be virtual)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnButtonClick(wxMouseEvent &);

private:
MyPanel* panel;
// any class wishing to process wxWidgets events must use this macro
DECLARE_EVENT_TABLE()
};

class MyButton;
class MyPanel: public wxPanel
{
public:
MyPanel(wxWindow *parent);
void OnButtonClick(wxMouseEvent &);

private:
MyButton* m_btn[2];
wxPoint m_currentMousePoint;

DECLARE_EVENT_TABLE()
};

class MyButton : public wxButton
{
public:
MyButton(wxWindow *parent,
wxWindowID id,
const wxString& label = wxEmptyString,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize)
: wxButton(parent, id, label, pos, size)
{
_point = pos;
_size = size;
_CMpoint = wxPoint(0,0);
_clicked = 0;
}
/*
void OnDClick(wxMouseEvent& event)
{
wxLogMessage(wxT("MyButton::OnDClick"));

event.Skip();
}
*/
void OnLeftDown(wxMouseEvent& event);
void OnDoubleClick(wxMouseEvent& WXUNUSED(evt));
void OnLeftUp(wxMouseEvent& event);
wxPoint GetCurrentMousePoint(){return _CMpoint;}
void unsetClicked(){_clicked = 0;}
void setClicked(){_clicked = 1;}
char GetClicked(){return _clicked;}
int PointInThisBtn(const wxPoint &p)
{
return (p.x > _point.x) && (p.x < _point.x + _size.GetWidth()) && (p.y > _point.y) && (p.y < _point.y + _size.GetHeight());
}


private:
wxPoint _CMpoint;
wxSize _size;
wxPoint _point;
char _clicked;
DECLARE_EVENT_TABLE()
};
#endif

// IDs for the controls and the menu commands
// Create a new application object: this macro will allow wxWidgets to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also implements the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
// not wxApp)
IMPLEMENT_APP(MyApp)

// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------
// IDs for the controls and the menu commands
enum
{
// menu items
Minimal_Quit = wxID_EXIT,

// it is important for the id corresponding to the "About" command to have
// this standard value as otherwise it won't be handled properly under Mac
// (where it is special and put into the "Apple" menu)
Minimal_About = wxID_ABOUT,
Btn_One,
Btn_Two
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
EVT_MENU(Minimal_About, MyFrame::OnAbout)
END_EVENT_TABLE()

// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
// call the base class initialization method, currently it only parses a
// few common command-line options but it could be do more in the future
if ( !wxApp::OnInit() )
return false;

// create the main application window
MyFrame *frame = new MyFrame("Minimal wxWidgets App");

// and show it (the frames, unlike simple controls, are not shown when
// created initially)
frame->Show(true);

// success: wxApp::OnRun() will be called which will enter the main message
// loop and the application will run. If we returned false here, the
// application would exit immediately.
return true;
}

BEGIN_EVENT_TABLE(MyPanel, wxPanel)
EVT_LEFT_DOWN(MyPanel::OnButtonClick)
END_EVENT_TABLE()
MyPanel:: MyPanel(wxWindow *parent) : wxPanel(parent)
{
m_btn[1] = new MyButton(this, Btn_Two, wxT("&Two"), wxPoint(130,130), wxSize(50,50));
m_btn[0] = new MyButton(this, Btn_One, wxT("&One"), wxPoint(100,100), wxSize(100,100));
}
void MyPanel::OnButtonClick(wxMouseEvent &evt)
{
int x, y;
wxString s;
evt.GetPosition(&x,&y);
m_currentMousePoint.x = x;
m_currentMousePoint.y = y;

s.Printf(("(%d,%d)"), x, y);
wxMessageBox(s,
"BTN1",
wxOK | wxICON_INFORMATION,
this);

#if 1
for(int i = 0; i < 2; i++)
{
if(m_btn[i]->PointInThisBtn(m_currentMousePoint))
{ wxMessageBox("Button one is clicked",
"BTN2",
wxOK | wxICON_INFORMATION,
this);
}
}
#endif
}


BEGIN_EVENT_TABLE(MyButton, wxButton)
EVT_LEFT_DOWN(MyButton::OnLeftDown)
EVT_LEFT_UP(MyButton::OnLeftUp)
EVT_LEFT_DCLICK(MyButton::OnLeftDown)
END_EVENT_TABLE()
void MyButton::OnLeftDown(wxMouseEvent& event)
{
setClicked();
// event.Skip();
//
}

void MyButton::OnLeftUp(wxMouseEvent& event)
{
#if 1
int x,y;
event.GetPosition(&x,&y);
_CMpoint.x = x + _point.x;
_CMpoint.y = y + _point.y;
event.SetX(_CMpoint.x);
event.SetY(_CMpoint.y);

// printf("(%d,%d), (%d,%d)\r\n", _CMpoint.x, _CMpoint.y, _size.x, _size.y);
#endif
wxMessageBox("position",
"Button",
wxOK | wxICON_INFORMATION,
this);
event.Skip();
MyPanel* parent = (MyPanel*)this->GetParent();
parent->OnButtonClick(event);
}
void MyButton::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
{
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
// set the frame icon
SetIcon(wxICON(sample));

#if wxUSE_MENUS
// create a menu bar
wxMenu *fileMenu = new wxMenu;

// the "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");

fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");

// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, "&File");
menuBar->Append(helpMenu, "&Help");

// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
#endif // wxUSE_MENUS
panel = new MyPanel(this);
#if wxUSE_STATUSBAR
// create a status bar just for fun (by default with 1 pane only)
CreateStatusBar(2);
SetStatusText("Welcome to wxWidgets!");
#endif // wxUSE_STATUSBAR
}


// event handlers

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
// true is to force the frame to close
Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(wxString::Format
(
"Welcome to %s!\n"
"\n"
"This is the minimal wxWidgets sample\n"
"running under %s.",
wxVERSION_STRING,
wxGetOsDescription()
),
"About wxWidgets minimal sample",
wxOK | wxICON_INFORMATION,
this);
}

void MyFrame::OnButtonClick(wxMouseEvent &)
{
wxMessageBox("Button one is clicked", "BTN1", wxOK | wxICON_INFORMATION, this);
}
...全文
214 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
l360220954 2013-05-09
  • 打赏
  • 举报
回复
自己顶,大家帮忙啊
chenzhao064 2013-05-07
  • 打赏
  • 举报
回复
帮顶!
l360220954 2013-05-06
  • 打赏
  • 举报
回复
那个死机的问题不用去解决了,在我们的平台上,是可以直接运行的。那个显示顺序的问题,还要仔细研究!请各位大侠帮忙!

790

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 其他移动相关
社区管理员
  • 移动开发其他问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧