简体   繁体   English

如何在C#Winform中的TabPage的选项卡区域检查鼠标移出?

[英]How to check Mouse Out on a Tab area of TabPage in C# Winform?

I want to check Mouse In/Out on Tab Area of TabPage in C# Winform. 我想检查C#Winform中TabPage的Tab区域上的Mouse In / Out。

There are Event MouseLeave, MouseEnter, MouseMove, but there work for whole TabPage. 有事件MouseLeave,MouseEnter,MouseMove,但对于整个TabPage都有效。 I just want for Tab only. 我只想要Tab。

TabControl tabControl = new TabControl();
TabPage tabpage = new TabPage();
tabpage.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);
tabControl.Controls.Add(tabpage);
this.Controls.Add(tabControl);

I'm thinking that If I get to know the Tab area so that I can write Code in MouseMove event for the same, Is there any better way to do the same. 我在想,如果我了解Tab区域,以便可以在MouseMove事件中编写相同的代码,是否有更好的方法来执行此操作?

I want for the area pointed by the arrow in the attached image. 我想要附图中箭头所指的区域。

Tab 标签

The GetTabRect function would help you here: GetTabRect函数将在这里为您提供帮助:

TabPage mouseTab = null;

void tabControl1_MouseMove(object sender, MouseEventArgs e) {
  TabPage checkTab = null;

  for (int i = 0; i < tabControl1.TabPages.Count; ++i) {
    if (tabControl1.GetTabRect(i).Contains(e.Location)) {
      checkTab = tabControl1.TabPages[i];
      break; // To avoid unnecessary loop
    }
  }

  if (checkTab == null && mouseTab != null) {
    mouseTab = null;
  } else if (checkTab != null) {
    if (mouseTab == null || !checkTab.Equals(mouseTab)) {
      mouseTab = checkTab;
      // or do something here...
    }
  }
}

And to handle the mouse leaving the tab header area: 并处理鼠标离开选项卡标题区域:

void tabControl1_MouseLeave(object sender, EventArgs e) {
  if (mouseTab != null) {
    // do something here with mouseTab...

    mouseTab = null;
  }
}

您可以在“选项卡”的整个区域上放置一个面板控件,然后将您提到的事件用于该面板控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM