3
누구나 핫 트랙 효과가있는 소유자의 tabcontrol을 보여주는 C# 예제가 있습니까?누구나 핫 트랙 효과가있는 소유자의 tabcontrol을 그려 보여주는 C# 예제가 있습니까?
저는 C# 및 .NET 2.0을 사용하여 멋진 소유자가 그려진 tabcontrol을 구현했지만 핫 트랙 효과를 구현할 때는 난처한 상황입니다.
어떤 아이디어?
누구나 핫 트랙 효과가있는 소유자의 tabcontrol을 보여주는 C# 예제가 있습니까?누구나 핫 트랙 효과가있는 소유자의 tabcontrol을 그려 보여주는 C# 예제가 있습니까?
저는 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();
}