任何人都有一个C#示例,显示所有者绘制的带有热跟踪效果的tabcontrol?

我使用C#和.NET 2.0实现了一个很好的所有者绘制tabcontrol但是在实现热跟踪效果时我很难过。

任何想法?

有帮助吗?

解决方案

使用这是一种方法。 MouseEnter MouseMove MouseLeave 触发必要的操作重绘。我使它比最基本的实现复杂一点,以避免闪烁。由您决定如何指示当前的热门选项卡;在示例中,我刚刚更改了选项卡的背景颜色。

在我的情况下,选项卡是Form的成员,我处理了表单中的各种事件,但它可以很容易地适应在自定义派生的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();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top