wxWidget实现连续绘图并能够控制绘图的开始与结束(一)

2014-11-24 11:51:20 · 作者: · 浏览: 5

在用CCD相机拍照的图像进行实时成像的时候遇到这么一个问题:

1: RunCamera();
2: while (1) {
3: GetImg();
4: ShowImg();
5: }
6: CloseCamera();
想要对获取的图像进行实时成像,最先想到的是采用while(1)的方式,但是这样的方式会带来一些问题,除了终止程序,没有办法使得循环停下,这个结果不是我们想要的。如果加上一个i < 100类似的条件,数量太大需要等,数量太小很快就结束了。我们想要设计的是能够随叫随停的,能够控制成像与停止的功能。

第一个想到的是onTimer的方式进行实现,不过水平低低,还是先在网上搜索一翻吧,功夫不负有心人,还是找到了。Making a render loop。

它采用了两种方式,一种为事件处理方式,一种为刚想到的onTimer的方式。下面参考上述链接中的内容,同时考虑自己的功能需求,给出了自己实现的具体代码。

所有的代码都是在codeblock下实现的。

1. 事件处理方式

采用onIdel的方式,一种频率最快的方式之一;但是频率不是固定的,当你进行其它操作时,如打开菜单Idel将会停止处理。

1: // the most improtant code in frame.h and frame.cpp
2: /***************************************************************
3: * Name: tmp1Main.h
4: * Purpose: Defines Application Frame
5: * Author: grass in moon
6: * Created: 2012-07-06
7: * Copyright: grass in moon (http://www.cppblog.com/grass-and-moon/)
8: * License:
9: **************************************************************/
10:
11: #ifndef TMP1MAIN_H
12: #define TMP1MAIN_H
13:
14: #ifndef WX_PRECOMP
15: #include
16: #endif
17:
18: #include "tmp1App.h"
19:
20: class tmp1Frame: public wxFrame
21: {
22: public:
23: tmp1Frame(wxFrame *frame, const wxString& title);
24: ~tmp1Frame();
25:
26: // for painting
27: void PaintNow();
28: void render(wxDC& dc);
29:
30: private:
31: enum
32: {
33: idMenuQuit = 1000,
34: idMenuAbout,
35: idStartLoop,
36: idStopLoop
37: };
38:
39: // mark loop on or off
40: bool render_loop_on;
41:
42: void OnClose(wxCloseEvent& event);
43: void OnQuit(wxCommandEvent& event);
44: void OnAbout(wxCommandEvent& event);
45: void OnStartLoop(wxCommandEvent& event);
46: void OnStopLoop(wxCommandEvent& event);
47:
48: // deel with wxIdleEvent
49: void OnIdle(wxIdleEvent& event);
50:
51: DECLARE_EVENT_TABLE()
52: };
53:
54:
55: #endif // TMP1MAIN_H
56:
57: /***************************************************************
58: * Name: tmp1Main.cpp
59: * Purpose: Code for Application Frame
60: * Author: grass in moon
61: * Created: 2012-07-06
62: * Copyright: grass in moon (http://www.cppblog.com/grass-and-moon/)
63: * License:
64: **************************************************************/
65:
66: #ifdef WX_PRECOMP
67: #include "wx_pch.h"
68: #endif
69:
70: #ifdef __BORLANDC__
71: #pragma hdrstop
72: #endif //__BORLANDC__
73:
74: #include "tmp1Main.h"
75:
76: //helper functions
77: enum wxbuildinfoformat
78: {
79: short_f, long_f
80: };
81:
82: wxString wxbuildinfo(wxbuildinfoformat format)
83: {
84: wxString wxbuild(wxVERSION_STRING);
85:
86: if (format == long_f )
87: {
88: #if defined(__WXMSW__)
89: wxbuild << _T("-Windows");
90: #elif defined(__WXMAC__)
91: wxbuild << _T("-Mac");
92: #elif defined(__UNIX__)
93: wxbuild << _T("-Linux");
94: #endif
95:
96: #if wxUSE_UNICODE
97: wxbuild << _T("-Unicode build");
98: #else
99: wxbuild << _T("-ANSI build");
100: #endif // wxUSE_UNICODE
101: }
102:
103: return wxbuild;
104: }
105:
106: BEGIN_EVENT_TABLE(tmp1Frame, wxFrame)
107: EVT_CLOSE(tmp1Frame::OnClose)
108: EVT_MENU(idMenuQuit, tmp1Frame::OnQuit)
109: EVT_MENU(idMenuAbout, tmp1Frame::OnAbout)
110: EVT_MENU(idStartLoop, tmp1Frame::OnStartLoop)
111: EVT_MENU(idStopLoop, tmp