如何构造简单的wxWidgets图像显示

发布于 2024-08-22 16:22:24 字数 530 浏览 21 评论 0原文

我编写了一个 wxPython 程序,并将其转换为 wxWidgets。该程序有一个显示图像的滚动窗口。继 Rappin,wxPython 实际操作(清单 12.1)之后,我在面板中使用了 StaticBitmap。在浏览最新的 wxWidgets 文档时,我发现了一个 可怕的警告,wxStaticBitmap 应该只用于非常小的图像。它说:“...如果您想便携式显示更大的图像,您应该使用自己的控件。”好的。给我看看。我没有“自己的控制权”。

是拉平错了,还是文档已经过时了?

毫无疑问,对于新手来说,问题是在 wxWidgets 中创建一个简单的图像视图窗口的正确方法是什么? wxStaticBitmap 的直接替代品会很好。我查看了 wxWidgets“samples”目录中的“image”程序。它就像战争与和平那么长。当然,一定有一个罐装课程或一个简单的食谱。

I wrote a wxPython program that I am translating to wxWidgets. The program has a scrolled window that displays an image. Following Rappin, wxPython In Action (Listing 12.1), I used a StaticBitmap within a panel. While surfing the latest wxWidgets documentation, I found a dire warning that wxStaticBitmap should only be used for very small images. It says, "... you should use your own control if you want to display larger images portably." Okay. Show me. I don't have my "own control."

Was Rappin wrong, or is the documentation out of date?

The question - a newbie one, no doubt - is what is the right way to do a simple image-view window in wxWidgets? A drop-in replacement for wxStaticBitmap would be nice. I looked into the "image" program in the wxWidgets "samples" directory. It's as long a War and Peace. Surely there must be a canned class or a simple recipe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡忘如思 2024-08-29 16:22:24

不要让“图像”样本的大小欺骗了您,只需几行代码即可完成您想要的操作。

image.cpp文件中搜索MyImageFrame类,它无非是一个带有私有位图字段的类,一个用于设置位图和窗口客户端的自定义构造函数大小,以及 EVT_PAINT 的事件处理程序:

void OnPaint(wxPaintEvent& WXUNUSED(event))
{
    wxPaintDC dc( this );
    dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ );
}

由于您不需要框架类,因此这里是您的方法:您创建一个 wxWindow 的简单后代,它具有类似的构造函数、paint处理程序并复制您在代码中使用的 wxStaticBitmap 方法。也许只是一种设置新位图并将控件大小调整为新位图尺寸的方法。

Don't let the size of the "image" sample fool you, only a few lines of code are necessary to do what you want.

Search for the MyImageFrame class in the image.cpp file, it is nothing more than a class with a private bitmap field, a custom constructor to set the bitmap and the window client size, and an event handler for EVT_PAINT:

void OnPaint(wxPaintEvent& WXUNUSED(event))
{
    wxPaintDC dc( this );
    dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ );
}

Since you don't want a frame class here's your recipe: You create a simple descendant of wxWindow that has a similar constructor, paint handler and duplicates the methods of wxStaticBitmap that you use in your code. Maybe simply one method to set a new bitmap and resize the control to the new bitmap dimensions.

盗梦空间 2024-08-29 16:22:24
// A scrolled window for showing an image.
class PictureFrame: public wxScrolledWindow
{   
public:
    PictureFrame()
        : wxScrolledWindow()
        , bitmap(0,0)
    {;}

    void Create(wxWindow *parent, wxWindowID id = -1)
    {
        wxScrolledWindow::Create(parent, id);
    }

    void LoadImage(wxImage &image) {
        bitmap = wxBitmap(image);
        SetVirtualSize(bitmap.GetWidth(), bitmap.GetHeight());
        wxClientDC dc(this);
        PrepareDC(dc);
        dc.DrawBitmap(bitmap, 0, 0);
    }

protected:
    wxBitmap bitmap;

    void OnMouse(wxMouseEvent &event) {
        int xx,yy;
        CalcUnscrolledPosition(event.GetX(), event.GetY(), &xx, &yy);
        event.m_x = xx; event.m_y = yy;
        event.ResumePropagation(1); // Pass along mouse events (e.g. to parent)
        event.Skip();
    }

    void OnPaint(wxPaintEvent &event) {
        wxPaintDC dc(this);
        PrepareDC(dc);
        dc.DrawBitmap(bitmap, 0,0, true);
    }
private:
    DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(PictureFrame,wxScrolledWindow)
    EVT_PAINT(PictureFrame::OnPaint)
    EVT_MOUSE_EVENTS(PictureFrame::OnMouse)
END_EVENT_TABLE()
// A scrolled window for showing an image.
class PictureFrame: public wxScrolledWindow
{   
public:
    PictureFrame()
        : wxScrolledWindow()
        , bitmap(0,0)
    {;}

    void Create(wxWindow *parent, wxWindowID id = -1)
    {
        wxScrolledWindow::Create(parent, id);
    }

    void LoadImage(wxImage &image) {
        bitmap = wxBitmap(image);
        SetVirtualSize(bitmap.GetWidth(), bitmap.GetHeight());
        wxClientDC dc(this);
        PrepareDC(dc);
        dc.DrawBitmap(bitmap, 0, 0);
    }

protected:
    wxBitmap bitmap;

    void OnMouse(wxMouseEvent &event) {
        int xx,yy;
        CalcUnscrolledPosition(event.GetX(), event.GetY(), &xx, &yy);
        event.m_x = xx; event.m_y = yy;
        event.ResumePropagation(1); // Pass along mouse events (e.g. to parent)
        event.Skip();
    }

    void OnPaint(wxPaintEvent &event) {
        wxPaintDC dc(this);
        PrepareDC(dc);
        dc.DrawBitmap(bitmap, 0,0, true);
    }
private:
    DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(PictureFrame,wxScrolledWindow)
    EVT_PAINT(PictureFrame::OnPaint)
    EVT_MOUSE_EVENTS(PictureFrame::OnMouse)
END_EVENT_TABLE()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文