誰もがホットトラッキング効果でタブコントロールを描画した所有者を示すC#の例を持っていますか?
-
03-07-2019 - |
質問
ホットトラッキング効果を使用して所有者が描画したタブコントロールを示すC#の例はありますか?
C#と.NET 2.0を使用して所有者描画タブコントロールを実装しましたが、ホットトラッキングエフェクトの実装に関して困惑しています。
アイデアは?
解決
を使用して、これを行う1つの方法を示します。 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();
}
所属していません StackOverflow