112: EVT_IDLE(tmp1Frame::OnIdle)
113: END_EVENT_TABLE()
114:
115: tmp1Frame::tmp1Frame(wxFrame *frame, const wxString& title)
116: : wxFrame(frame, -1, title)
117: {
118: #if wxUSE_MENUS
119: // create a menu bar
120: wxMenuBar* mbar = new wxMenuBar();
121: wxMenu* fileMenu = new wxMenu(_T(""));
122: fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
123: fileMenu->Append(idStartLoop, _("St&artLoop")); // for starting draw text
124: fileMenu->Append(idStopLoop, _("Sto&pLoop")); // for stoping draw text
125: mbar->Append(fileMenu, _("&File"));
126:
127: wxMenu* helpMenu = new wxMenu(_T(""));
128: helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
129: mbar->Append(helpMenu, _("&Help"));
130:
131: SetMenuBar(mbar);
132: #endif // wxUSE_MENUS
133:
134: #if wxUSE_STATUSBAR
135: // create a status bar with some information about the used wxWidgets version
136: CreateStatusBar(2);
137: SetStatusText(_("Hello Code::Blocks user!"),0);
138: SetStatusText(wxbuildinfo(short_f), 1);
139: #endif // wxUSE_STATUSBAR
140:
141: render_loop_on = false;
142: }
143:
144:
145: tmp1Frame::~tmp1Frame()
146: {
147: }
148:
149: void tmp1Frame::OnClose(wxCloseEvent &event)
150: {
151: Destroy();
152: }
153:
154: void tmp1Frame::OnQuit(wxCommandEvent &event)
155: {
156: Destroy();
157: }
158:
159: void tmp1Frame::OnAbout(wxCommandEvent &event)
160: {
161: wxString msg = wxbuildinfo(long_f);
162: wxMessageBox(msg, _("Welcome to..."));
163: }
164:
165: // set render_loop_on true, make OnIdle can draw text
166: void tmp1Frame::OnStartLoop(wxCommandEvent& event)
167: {
168: render_loop_on = true;
169: }
170:
171: // set render_loop_on true, make OnIdle can skip draw text
172: void tmp1Frame::OnStopLoop(wxCommandEvent& event)
173: {
174: render_loop_on = false;
175: }
176:
177: // paint depend on render_loop_on is true or off
178: void tmp1Frame::OnIdle(wxIdleEvent& event)
179: {
180: if (render_loop_on)
181: {
182: PaintNow();
183: event.RequestMore();
184: }
185: }
186:
187: void tmp1Frame::PaintNow()
188: {
189: wxClientDC dc(this);
190: render(dc);
191: }
192:
193: // draw text on screen
194: void tmp1Frame::render(wxDC& dc)
195: {
196: static int y = 0;
197: static int y_speed = 2;
198:
199: y += y_speed;
200: if (y < 0) y_speed = 2;
201: if (y > 200) y_speed = -2;
202:
203: dc.SetBackground(*wxWHITE_BRUSH);
204: dc.Clear();
205: dc.DrawText(wxT("Testing"),40,y);
206: }
207:
2. 时间处理方式
采用onTimer,具有较为固定的处理频率,当处理其他操作如打开菜单式,并不会中止;速度不像事件处理方式那么快,实现的速率与操作系统相关。
目前知道的wxTimer实现的方式有两种,1. 采用EVT_TIMER事件处理方式(该处理方法已经在文简易定时器设计中实现) 2. 创建一个wxTimer的子类,然后对wxTimer中的Notify函数进行重载。
下面采用第二种方式,进行实现。
tmp1Main.h
1: /***************************************************************
2: * Name: tmp1Main.h
3: * Purpose: Defines Application Frame
4: * Author: grass in moon
5: * Created: 2012-07-06
6: * Copyright: grass in moon (http://www.cppblog.com/grass-and-moon/)
7: * License:
8: **************************************************************/
9:
10: #ifndef TMP1MAIN_