简体   繁体   中英

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.

There are Event MouseLeave, MouseEnter, MouseMove, but there work for whole TabPage. I just want for Tab only.

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.

I want for the area pointed by the arrow in the attached image.

Tab

The GetTabRect function would help you here:

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;
  }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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