代码之家  ›  专栏  ›  技术社区  ›  Gary Willoughby

有没有人有一个C示例显示了一个拥有者绘制的带有热跟踪效果的TabControl?

  •  3
  • Gary Willoughby  · 技术社区  · 16 年前

    有没有人有一个C示例显示了一个拥有者绘制的带有热跟踪效果的TabControl?

    我已经使用C和.NET 2.0实现了一个不错的所有者绘制的TabControl,但在实现热跟踪效果方面,我遇到了困难。

    有什么想法吗?

    1 回复  |  直到 16 年前
        1
  •  5
  •   Charlie    16 年前

    这是一种方法,使用 MouseEnter , MouseMove MouseLeave 触发必要的重画。为了避免闪烁,我使它比最基本的实现稍微复杂一些。如何指示当前的热轨选项卡取决于您;在示例中,我刚刚更改了选项卡的背景色。

    在我的例子中,选项卡是表单的一个成员,我处理了表单中的各种事件,但是它可以很容易地适应在自定义派生的TabControl类中使用虚拟重写。

    // the index of the current hot-tracking tab
    private int hotTrackTab = -1;
    
    // returns the index of the tab under the cursor, or -1 if no tab is under
    private int GetTabUnderCursor()
    {
        Point cursor = this.tabs.PointToClient( Cursor.Position );
        for( int i = 0; i < this.tabs.TabPages.Count; i++ )
        {
            if( this.tabs.GetTabRect( i ).Contains( cursor ) )
                return i;
        }
        return -1;
    }
    
    // updates hot tracking based on the current cursor position
    private void UpdateHotTrack()
    {
        int hot = GetTabUnderCursor();
        if( hot != this.hotTrackTab )
        {
            // invalidate the old hot-track item to remove hot-track effects
            if( this.hotTrackTab != -1 )
                this.tabs.Invalidate( this.tabs.GetTabRect( this.hotTrackTab ) );
    
            this.hotTrackTab = hot;
    
            // invalidate the new hot-track item to add hot-track effects
            if( this.hotTrackTab != -1 )
                this.tabs.Invalidate( this.tabs.GetTabRect( this.hotTrackTab ) );
    
            // force the tab to redraw invalidated regions
            this.tabs.Update();
        }
    }
    
    private void tabs_DrawItem( object sender, DrawItemEventArgs e )
    {
        // draw the background based on hot tracking
        if( e.Index == this.hotTrackTab )
        {
            using( Brush b = new SolidBrush( Color.Yellow ) )
                e.Graphics.FillRectangle( b, e.Bounds );
        }
        else
        {
            e.DrawBackground();
        }
    
        // draw the text label for the item, other effects, etc
    }
    
    
    private void tabs_MouseEnter( object sender, EventArgs e )
    {
        UpdateHotTrack();
    }
    
    private void tabs_MouseLeave( object sender, EventArgs e )
    {
        UpdateHotTrack();
    }
    
    private void tabs_MouseMove( object sender, MouseEventArgs e )
    {
        UpdateHotTrack();
    }