wxWidgets 不规则透明WinForm窗体(通过透明图片实现)

#include "wx/wxprec.h"


class MyFrame;
class MyApp : public wxApp {
public:
	virtual bool OnInit();
private:
	MyFrame *frame;
};

class MyFrame : public wxFrame {
private:
	wxBitmap *bit;
	wxPoint *point;
public:
	MyFrame();
	void OnPaint(wxPaintEvent& event);
	void OnWindowCreate(wxWindowCreateEvent& event);
	void OnRightClick(wxMouseEvent& event);
	void OnMouseClickDown(wxMouseEvent& event);
	void OnMouseMotion(wxMouseEvent& event);
	void OnMouseClickUp(wxMouseEvent& event);
};

bool MyApp::OnInit(){
	this->frame = new MyFrame();
	this->frame->Show(TRUE);
	this->SetTopWindow(this->frame);
	return TRUE;
};

MyFrame::MyFrame() : wxFrame(NULL,-1,wxT("不规则窗体")){
	this->SetWindowStyle(wxFRAME_SHAPED|wxSIMPLE_BORDER|wxSTAY_ON_TOP);
	::wxInitAllImageHandlers();
	wxImage *img = new wxImage(wxT("d:\\bg.png"),wxBITMAP_TYPE_ANY);
	this->SetSize(img->GetWidth(),img->GetHeight());
	img->SetMask(TRUE);
	img->SetMaskColour(255,255,255);
	bit = new wxBitmap(*img);
	this->Bind(wxEVT_PAINT,&MyFrame::OnPaint,this);
	wxRegion *r = new wxRegion(*bit);
	this->SetShape(*r);
	this->Bind(wxEVT_RIGHT_UP,&MyFrame::OnRightClick,this);

	this->point = new wxPoint(0,0);
	this->Bind(wxEVT_LEFT_DOWN,&MyFrame::OnMouseClickDown,this);
	this->Bind(wxEVT_LEFT_UP,&MyFrame::OnMouseClickUp,this);
	this->Bind(wxEVT_MOTION,&MyFrame::OnMouseMotion,this);
};

void MyFrame::OnPaint(wxPaintEvent& event){
	wxPaintDC *dc = new wxPaintDC(this);
	if(bit->IsOk()){
		dc->DrawBitmap(*bit,0,0,TRUE);
	}
};

void MyFrame::OnWindowCreate(wxWindowCreateEvent& event){
	wxRegion *r = new wxRegion(*bit);
	this->SetShape(*r);
}

void MyFrame::OnRightClick(wxMouseEvent& event){
	if(wxMessageBox(wxT("是否退出程序?"),wxT("退出"),wxYES_NO,this) == 2){
		wxExit();
	}
};

void MyFrame::OnMouseClickDown(wxMouseEvent& event){
	this->CaptureMouse();
	event.GetPosition(&this->point->x,&this->point->y);
};

void MyFrame::OnMouseClickUp(wxMouseEvent& event){
	if(this->HasCapture()){
		this->ReleaseMouse();
	}
};

void MyFrame::OnMouseMotion(wxMouseEvent& event){
	if(event.Dragging() && event.LeftIsDown()){
		int x,y;
		event.GetPosition(&x,&y);
		this->ClientToScreen(&x,&y);
		this->Move(x-this->point->x,y-this->point->y);
	}
};

IMPLEMENT_APP(MyApp);

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值