wxWidgets实现按键重叠和编辑框输入

l360220954 2013-07-02 05:27:49
在下面的demo程序里,我想实现以下两个功能:
1 可以往那个编辑框里输入东西
2 点击到按钮和编辑框的公共区域时,这两个按键都是可以响应的
我这里两个功能都没有实现好,请大家帮我一下啦!

我自己写的demo源码如下,在codeblocks上新建一个空工程,再新建一个文件,然后把下面这段拷到那个新建的文件里去,这段代码是可以直接编辑运行的。程序如下:

// IDs for the controls and the menu commands
enum
{
Minimal_Quit = wxID_EXIT,
Minimal_About = wxID_ABOUT,
Btn_One,
Btn_Two
};

unsigned int g_cmp_handle = 0;
bool g_click_state = 0;

class MyEdit;
class MyButton;

MyEdit* edit = NULL;
MyButton* button = NULL;

class MyApp : public wxApp
{
public:

virtual bool OnInit();
};

class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title);

void OnIdle(wxIdleEvent& event);

private:
wxPanel* panel;
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)
{
}
void OnLeftDown(wxMouseEvent& event);
void OnDoubleClick(wxMouseEvent& WXUNUSED(evt));
void OnLeftUp(wxMouseEvent& event);

private:

DECLARE_EVENT_TABLE()
};

class MyEdit : public wxTextCtrl
{
public:
MyEdit(wxWindow *parent,
wxWindowID id,
const wxString& value = wxEmptyString,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxTextCtrlNameStr)
:wxTextCtrl(parent, id, value, pos, size, style, validator, name)
{
}
void OnTextCtrlEnter( wxCommandEvent& event );
void OnPaint(wxPaintEvent& event);
void OnLeftUp(wxMouseEvent& event);
void OnLeftDown(wxMouseEvent& event);
private:
int _value;
DECLARE_EVENT_TABLE()
};

class MyPanel: public wxPanel
{
public:
MyPanel(wxWindow *parent,
wxWindowID winid = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL | wxNO_BORDER,
const wxString& name = wxPanelNameStr)
:wxPanel(parent, winid, pos, size, style, name)
{
edit = new MyEdit(this, Btn_One, wxT("window"), wxPoint(20, 20), wxSize(90,40));

button = new MyButton(this, Btn_Two, wxT("Button"), wxPoint(30, 20), wxSize(50,80));
}

void OnButtonClick(wxMouseEvent &);


private:
wxPoint m_currentMousePoint;

DECLARE_EVENT_TABLE()
};


// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------

// the event tables connect the wxWidgets events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_IDLE(MyFrame::OnIdle)
END_EVENT_TABLE()

// 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)

// ============================================================================
// implementation
// ============================================================================

// ----------------------------------------------------------------------------
// 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;
}

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

// frame constructor
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{

panel = new MyPanel(this);
}

void MyFrame::OnIdle(wxIdleEvent& event)
{
static int num = 1000000;
wxString wx_string = wxString::Format(wxT("%07i"),num++);
edit->SetValue(wx_string);
}

// event handlers

BEGIN_EVENT_TABLE(MyPanel, wxPanel)
EVT_LEFT_DOWN(MyPanel::OnButtonClick)
END_EVENT_TABLE()

/////////////////////////////////////////////////////////////////////////////
void MyPanel::OnButtonClick(wxMouseEvent &evt)
{
int x, y;
wxString s;
evt.GetPosition(&x,&y);
m_currentMousePoint.x = x;
m_currentMousePoint.y = y;
printf("clicked position(%d, %d)\r\n", x,y);


wxWindowList children = GetChildren();

wxWindowList::iterator node = children.begin();
for (; node != children.end(); ++node )
{
wxWindow* child = (wxWindow*)*node;
wxRect rect = child->GetRect();

if(rect.Contains(x,y))
{
if(g_cmp_handle == (UINT32)child)continue;
wxEvtHandler* evtHandler = child->GetEventHandler();
wxPostEvent(evtHandler, evt);
}

}

}

//==================MyButton==================================
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)
{
g_cmp_handle = (UINT32)this;
g_click_state = 1;
}

void MyButton::OnLeftUp(wxMouseEvent& event)
{
Raise();
event.Skip();
if(g_click_state)
{
MyPanel* parent = (MyPanel*)this->GetParent();
parent->OnButtonClick(event);
g_click_state = 0;
}

wxMessageBox("position", "Button", wxOK | wxICON_INFORMATION, this);
}
void MyButton::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
{
}

//======================wxHmiButton===============================


//======================wxHmiButton===============================
BEGIN_EVENT_TABLE(MyEdit, wxTextCtrl)
/*EVT_TEXT*/EVT_TEXT_ENTER(Btn_One,MyEdit::OnTextCtrlEnter)
EVT_PAINT(MyEdit::OnPaint)
/*EVT_LEFT_DOWN*/EVT_LEFT_UP(MyEdit::OnLeftUp)
EVT_LEFT_DOWN/*EVT_LEFT_UP*/(MyEdit::OnLeftDown)
END_EVENT_TABLE()

void MyEdit::OnLeftDown(wxMouseEvent& event)
{
g_cmp_handle = (UINT32)this;
g_click_state = 1;
}

void MyEdit::OnLeftUp(wxMouseEvent& event)
{
Raise();
event.Skip();
if(g_click_state)
{
MyPanel* parent = (MyPanel*)this->GetParent();
parent->OnButtonClick(event);
g_click_state = 0;
}
wxMessageBox("edit one is clicked",
"BTN2",
wxOK | wxICON_INFORMATION,
this);
}

void MyEdit::OnPaint(wxPaintEvent& event)
{
// CaptureMouse();

if(HasFocus() )
{
wxTextCtrl::OnPaint(event);
}
else
{
wxString wx_string = wxString::Format(wxT("%07i"),_value);
// SetValue(wx_string);
// Refresh();
//WriteText(wx_string); //no
char ansi_string[30];
strcpy(ansi_string,wx_string.mb_str());
printf("wx_string = %s\r\n", ansi_string);


/**/
wxPaintDC dc(this);
//DoPrepareDC(dc);
dc.SetPen(*wxBLACK_PEN);

//dc.SetBrush(*wxWHITE_BRUSH);//
wxColour clr(255, 255, 255);
wxBrush whiteBrush(clr, wxSOLID);
dc.SetBackground (whiteBrush);//SetBrush(*wxWHITE_BRUSH)
dc.Clear();

dc.DrawText(wx_string, 5, 0);

// char ansi_string[30];
// strcpy(ansi_string,wx_string.mb_str());
// printf("wx_string = %s\r\n", ansi_string);
}//end else

}
/**/

void MyEdit::OnTextCtrlEnter( wxCommandEvent& event )
{

// wxHmiPanel* parent = (wxHmiPanel*)this->GetParent();
// parent->OnButtonClick(event);

#if 0
wxMessageBox("Button one is clicked",
"BTN2",
wxOK | wxICON_INFORMATION,
this);
#endif
}
...全文
286 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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