简体   繁体   English

WxWidgets中的缓冲图像

[英]Buffered image in WxWidgets

I am trying to make a Digital IC simulation using wxWidgets. 我正在尝试使用wxWidgets进行数字IC仿真。 I have decent knowledge of C++, but wxwidgets is relatively new. 我对C ++有很好的了解,但wxwidgets相对较新。

As a first step, I need to display a breadboard. 作为第一步,我需要展示面包板。 My solution is 我的解决方案是

  • a class myFrame (extends wxframe ), creates instance of class breadboard (extends wxpanel ). 一个类myFrame (扩展wxframe ),创建类面包板的实例(扩展wxpanel )。

  • breadboard has member wxMemoryDC bbBuffer 面包板有成员wxMemoryDC bbBuffer

  • In constructor of breadboard i draw using following code snippet 面包板的构造函数中,我使用以下代码片段绘制

     wxBitmap bmp(660, 450, -1); bbBuffer.SelectObject(bmp); bbBuffer.SetBackground(*wxWHITE_BRUSH); bbBuffer.Clear(); bbBuffer.SetPen(wxPen(wxColor(80, 80, 80), 1)); bbBuffer.DrawRectangle(10, 10, 640, 430);//more functions like this one follow 

in render function i do this - 在渲染功能中我这样做 -

void BreadBoard::render(wxDC &dc)
{
    dc.Blit(wxPoint(10, 20), bbBuffer.GetSize(), &bbBuffer, wxPoint(0, 0));
}

I also draw wires using mouseEvent on wxClientDC and bbBuffer (this is too simple, later i will write it to XML file, but not now) 我也借鉴wxClientDCbbBuffer使用的MouseEvent(这是太简单了,以后我将其写入XML文件,但不是现在)导线

Is this a good design strategy? 这是一个很好的设计策略吗? Later on it will get complex, so how should i proceed? 稍后它会变得复杂,所以我该怎么办?

Personally, I would not make class breadboard an extension of wxPanel. 就个人而言,我不会将类面包板作为wxPanel的扩展。 It seems cleaner to have breadboard simply contain your code and not be a mixture. 让面包板只包含您的代码而不是混合物似乎更清晰。

You can create a panel as a child of myFrame. 您可以将面板创建为myFrame的子项。

class MyFrame {
    wxPanel * myPanel;
    ...
MyFrame::MyFrame(
    ...
    myPanel = new wxPanel(this,-1,wxPoint(0,0),wxSize(2400,1200)) ;
    ...

Then when you are ready to display the breadboard's bitmap, a method of MyFrame can do a GetDC on the panel, and blit the image from the breadboard's DC to the panel's DC, or pass the panel DC to a method of the breadboard class so that the breadboard can do the blit without exposing its memory DC. 然后,当您准备好显示面包板的位图时,MyFrame的方法可以在面板上执行GetDC,并将图像从面包板的DC blit到面板的DC,或者将面板DC传递给面包板类的方法,以便面包板可以在不暴露其内存DC的情况下进行blit。

void MyFrame::Render()
{
    myBreadboard.Render( wxClientDC( myPanel ) );
}

What are the advantages? 有什么好处?

  • Conceptually clearer, in my mind anyway. 无论如何,在我的脑海里,概念上更加清晰。

  • More obvious how to draw your breadboard to another DC, eg a printer. 更明显的是如何将面包板绘制到另一个DC,例如打印机。 You can simply pass the printer DC to the breadboard blit method. 您只需将打印机DC传递给面包板blit方法即可。

I see that your method render() does the blit. 我看到你的方法render()执行blit。 Some comments on this method: 关于这种方法的一些评论:

  • You are passing in a wxDC parameter. 您正在传入wxDC参数。 What will this be? 这将是什么? I am guessing that this will usually be the wxDC obtained by calling GetDC on this - which is kind of confusing and unnecessary. 我猜这通常是通过调用GetDC获得的wxDC - 这有点令人困惑和不必要。 You could do the call to GetDC inside the render() method, which makes your code cleaner, but now you cannot use render to draw anywhere except to the panel implemented by breadboard which will eventually be a snag. 您可以在render()方法中调用GetDC,这会使您的代码更清晰,但现在除了面板实现的面板之外,您不能使用渲染来绘制任何地方,这最终会成为障碍。

  • You have hard coded the location where the breadboard is drawn. 您已经硬编码了绘制面包板的位置。 Bad idea! 馊主意! Better to pass the location as a parameter, with a sensible default. 最好将位置作为参数传递,具有合理的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM