wxWidgets如何才能正确的调用OnPaint函数进行绘图?

GG_Dolls GG_Dolls 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
界面的结构如下:wxFrame-wxBoxSizer-DrawablePanel。其中,DrawablePanel是我单独分离出来的,继承自wxScrollWindow,主要是想在这个Panel上进行图片的绘制,并且共享给其他类使用。但是现在出了一个问题,OnPaint函数总是无法被调用,从而使得绘图工作无法正常进行。

哪位给看看如何才能实现OnPaint函数周期调用的问题。另外还有一个疑问,wxWindows有没有一个函数,只要子类重写了这个函数,父类就自动调用,而不是添加事件处理(就像Java里的Paint函数一样)?

相关代码如下:

drawablepanel.h

#ifndef __DRAWABLE_PANEL_H__
#define __DRAWABLE_PANEL_H__


#include <wx/scrolwin.h>
#include <wx/bitmap.h>


class DrawablePanel : public wxScrolledWindow
{
public:
DrawablePanel();
~DrawablePanel();

void SetImage(wxBitmap& img);
void SetZoom(int zoom);
int GetZoom();
void SetGridVisible(bool visible);
bool GetGridVisible();

void RefreshNotify();

protected:

void OnPaint(wxPaintEvent& event);

private:

wxBitmap image;
wxBitmap bufferImage;
int zoomLevel ;
bool isGridVisible;


DECLARE_EVENT_TABLE()

};


#endif //__DRAWABLE_PANEL_H__


drawablepanel.cpp

#include "drawablepanel.h"



BEGIN_EVENT_TABLE(DrawablePanel,wxScrolledWindow)
EVT_PAINT(DrawablePanel::OnPaint)
END_EVENT_TABLE()

DrawablePanel::DrawablePanel() : wxScrolledWindow()
{
zoomLevel = 0;
isGridVisible = false;
}

DrawablePanel::~DrawablePanel()
{

}

void DrawablePanel::SetImage(wxBitmap& img)
{
image = img;
RefreshNotify();
}

void DrawablePanel::SetGridVisible(bool visible)
{
if(isGridVisible != visible)
{
isGridVisible = visible;
RefreshNotify();
}
}

bool DrawablePanel::GetGridVisible()
{
return this->isGridVisible;
}

void DrawablePanel::SetZoom(int zoom)
{
int newZoom = zoomLevel + zoom;
if(zoomLevel != newZoom)
{
zoomLevel = newZoom;
RefreshNotify();
}
}

int DrawablePanel::GetZoom()
{
return this->zoomLevel;
}

void DrawablePanel::OnPaint(wxPaintEvent& event)
{
wxMessageBox(wxT("重绘图!"));
wxPaintDC dc(this);
dc.DrawBitmap(image,0,0,true);

}

void DrawablePanel::RefreshNotify()
{
Refresh(true);
Update();
}


...全文
评论 3
收藏 已收藏
分享
返回顶部
GG_Dolls
C++ 语言 65,214 成员 250,514 内容