您的位置:首页 > 其它

[wxWidgets]_[中级]_[自定义wxStaticText控件支持图片背景(透明背景)]

2013-12-11 11:06 901 查看
场景:

1.在贴图的情况中,wxStaticText并不能支持有背景的父控件,这样文字背景就有一个灰黄色的默认背景。即使写子类继承wxStaticText加入EVT_ERASE_BACKGROUND也不能让背景色消失。这时候可以参考mfc的实现,返回一个透明BRUSH给wxStaticText做绘制默认背景色用.不过这里就需要重载DoMSWControlColor方法,这个是基于本地的实现。没办法了,如果用基于本地的实现就能很容易实现一些特效,但是在wxWidgets下如果开发跨平台控件就得多写很多代码。可以仿照wx的源代码写两个平台的版本.这里实现了msw和mac平台下的wxStaticText子类,mac平台下很容易,只需要重载
HasTransparentBackground 返回true就行了,看wx的chm文档有写。

文件1: 首先是基类

dh_static_text.h

/*
* DhStaticTextBase.h
*
*  Created on: 2013-7-7
*  Author: Sai
*/

#ifndef DHSTATICTEXT_H_BASE_
#define DHSTATICTEXT_H_BASE_

#include "wx/wx.h"

class DhStaticTextBase :public wxStaticText
{
public:
DhStaticTextBase();
virtual ~DhStaticTextBase(){}

virtual bool HasTransparentBackground();
void SetBg(wxBitmap* bg);

protected:
wxBitmap *bg_;
};

#ifdef __QXWIN32__
#include "window/base/msw/dh_static_text.h"
#else
#include "window/base/osx/dh_static_text.h"
#endif

#endif /* DHSTATICTEXT_H_BASE_ */


文件2:基类的实现dh_static_text_base.cpp

/*
* dh_static_text.cpp
*
*  Created on: 2013-7-26
*  Author: Sai
*/

#include "window/base/dh_static_text.h"

DhStaticTextBase::DhStaticTextBase()
{
bg_ = NULL;
}

bool DhStaticTextBase::HasTransparentBackground()
{
return true;
}

void DhStaticTextBase::SetBg(wxBitmap* bg)
{
bg_ = bg;
}


文件3: msw的子类声明: dh_static_text.h

/*
* DhStaticText.h
*
*  Created on: 2013-7-7
*  Author: Sai
*/

#ifndef DHSTATICTEXT_H_
#define DHSTATICTEXT_H_

#include "window/base/dh_static_text.h"
#include "wx/msw/private.h"

class DhStaticText :public DhStaticTextBase
{
DECLARE_EVENT_TABLE()
public:
DhStaticText();
virtual ~DhStaticText(){}

protected:
void OnEraseBackground(wxEraseEvent& event);
virtual WXHBRUSH DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd);

private:
wxBrush *brush_;

wxRect last_rect_;
wxBitmap bg_cache_;
};

#endif /* DHSTATICTEXT_H_ */


文件4: msw的实现dh_static_text.cpp

/*
* DhStaticText.cpp
*
*  Created on: 2013-7-7
*  Author: Sai
*/

#include "window/base/msw/dh_static_text.h"

BEGIN_EVENT_TABLE(DhStaticText, DhStaticTextBase)
EVT_ERASE_BACKGROUND(DhStaticText::OnEraseBackground)
END_EVENT_TABLE()

DhStaticText::DhStaticText()
{
SetBackgroundStyle(wxBG_STYLE_ERASE);
SetForegroundColour(*wxWHITE);
brush_ = new wxBrush(wxNullColour,wxBRUSHSTYLE_TRANSPARENT);
}

void DhStaticText::OnEraseBackground(wxEraseEvent& event)
{
wxRect rect = GetRect();
if (last_rect_ != rect)
{
last_rect_ = rect;
int height = bg_->GetHeight();
int width = bg_->GetWidth();
rect.height = (height < (rect.y+rect.height))?(height - rect.y):rect.height;
rect.width = (width < (rect.x+rect.width))?(width - rect.x):rect.width;

assert(bg_->GetHeight() >= (rect.y+rect.height));
assert(bg_->GetWidth() >= (rect.x+rect.width));
bg_cache_ = bg_->GetSubBitmap(
wxRect(rect.x, rect.y, rect.width, rect.height));
}
event.GetDC()->DrawBitmap(bg_cache_,0,0);
}

WXHBRUSH DhStaticText::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd)
{
HDC hdc = (HDC)pDC;
::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
return (WXHBRUSH)brush_->GetResourceHandle();
}


文件5: mac os x的dh_static_text.h

/*
* DhStaticText.h
*
*  Created on: 2013-7-7
*  Author: Sai
*/

#ifndef DHSTATICTEXT_H_
#define DHSTATICTEXT_H_

#include "window/base/dh_static_text.h"

class DhStaticText :public DhStaticTextBase
{
public:
DhStaticText(){}
virtual ~DhStaticText(){}
};

#endif /* DHSTATICTEXT_H_ */


用法:

description_text_ = new DhStaticText();
description_text_->Create(this,wxID_ANY,wxEmptyString);
description_text_->SetBg(bg_);


效果图:其中文字后边的背景是带图片的父控件.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐