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

  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:
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
duilib是一个基于C++的轻量级GUI库,它提供了丰富的控件和界面开发工具,方便开发者快速构建界面。其中,duilib提供了自绘标题栏的功能,使得开发者可以自定义标题栏的样式和交互行为。 要实现duilib的自绘标题栏,首先需要在XML布局文件中定义标题栏控件。通过设置标题栏的位置、大小和属性等,将其嵌入到主窗口的布局中。 然后,需要在代码中继承duilib提供的类,并重写标题栏的绘制函数。在绘制函数中,可以使用GDI+或者其他绘图工具进行自定义的绘制。例如,可以根据设计需求绘制背景图案、按钮、文本等。 同时,还可以为标题栏添加交互行为。通过重写相应的事件处理函数,实现对标题栏的拖拽、最小化、最大化、关闭等操作。可以根据需要添加点击、移动、释放等事件的处理逻辑,实现标题栏的交互功能。 最后,在主窗口的代码中,通过调用duilib提供的API将标题栏控件与相应的绘制类和事件处理类进行关联。这样,当主窗口加载时,duilib会自动调用对应的绘制函数和事件处理函数,完成自绘标题栏的效果。 总之,使用duilib自绘标题栏可以灵活定制应用程序的界面风格,提升用户体验。开发者可以根据需要自定义标题栏的样式和交互行为,以满足不同项目的需求。同时,通过duilib提供的API和工具,实现自绘标题栏也是相对简单和高效的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值