有人有一个 C# 示例,显示所有者绘制的具有热跟踪效果的选项卡控件吗?

发布于 2024-07-06 18:10:11 字数 128 浏览 11 评论 0原文

有人有一个 C# 示例,显示所有者绘制的具有热跟踪效果的选项卡控件吗?

我已经使用 C# 和 .NET 2.0 实现了一个很好的所有者绘制的选项卡控件,但在实现热跟踪效果时我感到很困惑。

有任何想法吗?

Anyone have a C# example showing an owner drawn tabcontrol with hot-tracking effects?

I've implemented a nice owner drawn tabcontrol using C# and .NET 2.0 but i'm stumped when it comes to implementing hot-tracking effects.

Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落□心尘 2024-07-13 18:10:11

这是一种方法,使用 MouseEnter、MouseMove 和 < a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseleave.aspx" rel="noreferrer">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();
}

Here's one way to do it, using MouseEnter, MouseMove, and MouseLeave to trigger the necessary redraws. I made it a little more complicated than the most basic implementation, to avoid flicker. It's up to you how to indicate the current hot-track tab; in the example I just changed the background color of the tab.

In my case the tab was a member of a Form and I handled the various events in the form, but it could easily be adapted to use virtual overrides in a custom-derived TabControl class.

// 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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文