1. 程式人生 > >wxWidgets 不規則透明WinForm窗體(通過透明圖片實現)

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);