void mTabs_MouseMove(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left && mTabs.SelectedTab != null) { if (mouseListening) { if (e.Y >= 0 && e.Y <= mTabs.ItemSize.Height && e.X >= 0 && e.X <= mTabs.Width) { // Work out tab the mouse is actually pointing at. Graphics g = mTabs.CreateGraphics(); int start = 0; mouseSelected = 0; for (int i = 0; i < mTabs.TabPages.Count; i++) { SizeF sf = g.MeasureString(mTabs.TabPages[i].Text, mTabs.TabPages[i].Font); int width = Math.Max((int)sf.Width + 4, 45); start += width; mouseSelected = i; if (start >= e.X) { break; } } g.Dispose(); } else { // If we leave the form, then pop out the tab into its own window. Point screen = mTabs.PointToScreen(e.Location); Point form = mTabs.TopLevelControl.PointToScreen(e.Location); Point outside = new Point(screen.X - form.X + e.X, screen.Y - form.Y + e.Y); if (outside.X < -5 || outside.Y < -20 || outside.X > mTabs.TopLevelControl.Width || outside.Y > mTabs.TopLevelControl.Height) { mouseListening = false; mTabs.Cursor = Cursors.Default; popOutTab(); } } } } }