自绘标题栏,不规则窗口,背景透明

本文介绍了一个使用C++和GTK+实现的自绘标题栏及不规则窗口的方法,通过覆盖窗口事件,实现了背景透明、自定义形状及窗口操作功能,包括拖动、最大化、最小化和关闭等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 主函数
    int main( int argc, char *argv[] ) 
    { 
    	gtk_init( &argc, &argv ); 
    
    	dsFrame frm;
    	frm.show();
    
    	gtk_main();
    
    	return 0;
    }
  2. base 类定义
    class dsBase 
    { 
    public:
    	dsBase();
    	
    	GtkWidget* win( void )			{ return p_win; }
    	dsShell* shell( void )			{ return p_shell; }
    
    protected:
    	void draw_round_rect( cairo_t *cr, int w, int h, int r = 10, bool bclose = true, int x = 0, int y = 0 );
    	
    	void win( GtkWidget *p )		{ p_win = p; }
    
    private:
    	GtkWidget *p_win;
    }; 
    
  3. base 类实现
    dsBase::dsBase( dsShell *p/* = 0*/ ) 
    		: p_win( 0 ) 
    { 
    }
    
    void dsBase::draw_round_rect( cairo_t *cr, int w, int h, int r /*= 10*/, bool bclose /* = true*/, int x /* = 0*/, int y /* = 0*/ )
    { 
    	cairo_move_to( cr, x, y + r ); 
    	cairo_arc( cr, x + r, y + r, r, 180 * ( M_PI / 180.0 ), 270 * ( M_PI / 180.0 ) ); 
    	cairo_move_to( cr, x, y + r ); 
    	cairo_line_to( cr, x, y + h ); 
    	if ( bclose )
    	{
    		cairo_line_to( cr, x + w, y + h ); 
    	}
    	else
    	{ 
    		cairo_move_to( cr, x + w, y + h ); 
    	}
    	cairo_line_to( cr, x + w, y + r ); 
    	cairo_arc_negative( cr, x + w - r, y + r, r, 0 * ( M_PI / 180.0 ), 270 * ( M_PI / 180.0 ) ); 
    	cairo_line_to( cr, x + r, y ); 
    }


    
    
  4. frame 类定义
    class dsFrame : public dsBase
    {    
    public:
    	dsFrame( int w = 600, int h = 500 );
    	~dsFrame();
    
    	void show( void );
    
    private:
    	static bool on_expose( GtkWidget* widget, GdkEventExpose *event, gpointer data );
    	static bool on_configure( GtkWidget* widget, GdkEventConfigure* event, gpointer data );
    	static bool on_button_press( GtkWidget* widget, GdkEventButton * event, gpointer data );
    	static bool on_move( GtkWidget* widget, GdkEventMotion *event, gpointer data );
    	static bool on_screen_changed( GtkWidget *widget, GdkScreen *old_screen, gpointer userdata );
    
    	GdkRectangle close_rect( GtkWidget *widget );
    	GdkRectangle max_rect( GtkWidget *widget );
    	GdkRectangle min_rect( GtkWidget *widget );
    	bool point_inside( const GdkRectangle &rect, int x, int y );
    
    	bool bottom_right( int x, int y );
    	bool bottom_left( int x, int y );
    	bool top_right( int x, int y );
    	bool top_left( int x, int y );
    	bool top( int x, int y );
    	bool bottom( int x, int y );
    	bool left( int x, int y );
    	bool right( int x, int y );
    	bool title( int x, int y );
    	bool min( int x, int y );
    	bool max( int x, int y );
    	bool close( int x, int y );
    
    	bool update( void ) const	{ return b_update; }
    	bool max( void ) const		{ return b_max; }
    
    	void update( bool v )		{ b_update = v; }
    	void max( bool v )		{ b_max = v; }
    
    private:
    	bool b_update;
    	bool b_max;
    }; 
    
    
    
  5. frmae类实现
    dsFrame:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值