开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
UITabControl.cs 28.71 KB
一键复制 编辑 原始数据 按行查看 历史
电脑程序爱好者 提交于 2021年01月25日 15:25 +08:00 . init created
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HDC = System.IntPtr;
using HWND = System.IntPtr;
namespace NavTabControl
{
public sealed class UITabControl : TabControl, IStyleInterface
{
public const int VK_RBUTTON = 0x2;
public const int VK_LBUTTON = 0x1;
public const int WM_PAINT = 0xF;
[StructLayout(LayoutKind.Sequential)]
public struct PAINTSTRUCT
{
public IntPtr hdc;
public bool fErase;
public RECT rcPaint;
public bool fRestore;
public bool fIncUpdate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public byte[] rgbReserved;
}
private readonly UITabControlHelper Helper;
private int DrawedIndex = -1;
private readonly Timer timer = new Timer();
[DllImport("user32")]
public static extern int GetCursorPos(out POINT lpPoint);
[DllImport("user32")] public static extern int FindWindowEx(HWND hwnd1, HWND hwnd2, string lpsz1, string lpsz2);
[DllImport("user32")] public static extern short GetKeyState(int nVirtKey);
[DllImport("user32")] public static extern int GetWindowRect(HWND hwnd, ref RECT lpRect);
[DllImport("user32")] public static extern int BeginPaint(HWND hwnd, ref PAINTSTRUCT lpPaint);
[DllImport("user32")] public static extern int EndPaint(HWND hwnd, ref PAINTSTRUCT lpPaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PtInRect([In] ref RECT lprc, Point pt);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint = true);
public UITabControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
ItemSize = new Size(150, 40);
DrawMode = TabDrawMode.OwnerDrawFixed;
Font = UIFontColor.Font;
AfterSetFillColor(FillColor);
Size = new Size(450, 270);
Helper = new UITabControlHelper(this);
timer.Interval = 500;
timer.Tick += Timer_Tick;
}
~UITabControl()
{
timer.Stop();
timer.Dispose();
}
private string mainPage = "";
[DefaultValue(true)]
[Description("主页名称,此页面不显示关闭按钮")]
public string MainPage
{
get => mainPage;
set
{
mainPage = value;
Invalidate();
}
}
private void Timer_Tick(object sender, EventArgs e)
{
timer.Stop();
DrawedIndex = SelectedIndex;
}
protected override void OnSelected(TabControlEventArgs e)
{
base.OnSelected(e);
if (ShowActiveCloseButton && !ShowCloseButton)
{
timer.Start();
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (ForbidCtrlTab)
{
switch (keyData)
{
case (Keys.Tab | Keys.Control):
//组合键在调试时,不容易捕获;可以先按住Ctrl键(此时在断点处已经捕获),然后按下Tab键,都释放后,再进入断点处单步向下执行;
//此时会分两次进入断点,第一次(好像)是处理Ctrl键,第二次处理组合键
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
[DefaultValue(true)]
[Description("是否禁用Ctrl+Tab")]
public bool ForbidCtrlTab { get; set; } = true;
public void SelectPage(int pageIndex) => Helper.SelectPage(pageIndex);
public void SelectPage(Guid pageGuid) => Helper.SelectPage(pageGuid);
public void AddPage(UIPage page) => Helper.AddPage(page);
public void AddPage(Guid guid, UIPage page) => Helper.AddPage(guid, page);
public void AddPage(int index, UIPage page) => Helper.AddPage(index, page);
public void AddPages(params UIPage[] pages)
{
foreach (var page in pages) AddPage(page);
}
public void AddPage(int pageIndex, UITabControl page) => Helper.AddPage(pageIndex, page);
public void AddPage(Guid guid, UITabControl page) => Helper.AddPage(guid, page);
private Color _fillColor = UIColor.LightBlue;
private Color tabBackColor = Color.FromArgb(56, 56, 56);
/// <summary>
/// Tag字符串
/// </summary>
[DefaultValue(null)]
[Description("获取或设置包含有关控件的数据的对象字符串")]
public string TagString { get; set; }
/// <summary>
/// 自定义主题风格
/// </summary>
[DefaultValue(false)]
[Description("获取或设置可以自定义主题风格")]
public bool StyleCustomMode { get; set; }
private HorizontalAlignment textAlignment = HorizontalAlignment.Center;
[DefaultValue(HorizontalAlignment.Center)]
[Description("文字显示方向")]
public HorizontalAlignment TextAlignment
{
get => textAlignment;
set
{
textAlignment = value;
Invalidate();
}
}
private bool tabVisible = true;
[DefaultValue(true)]
[Description("标签页是否显示")]
public bool TabVisible
{
get => tabVisible;
set
{
tabVisible = value;
if (!tabVisible)
{
ItemSize = new Size(0, 1);
}
else
{
if (ItemSize == new Size(0, 1))
{
ItemSize = new Size(150, 40);
}
}
Invalidate();
}
}
/// <summary>
/// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("当使用边框时填充颜色,当值为背景色或透明色或空值则不填充")]
[DefaultValue(typeof(Color), "235, 243, 255")]
public Color FillColor
{
get => _fillColor;
set
{
_fillColor = value;
AfterSetFillColor(value);
_style = UIStyle.Custom;
Invalidate();
}
}
/// <summary>
/// 边框颜色
/// </summary>
[Description("边框颜色")]
[DefaultValue(typeof(Color), "56, 56, 56")]
public Color TabBackColor
{
get => tabBackColor;
set
{
tabBackColor = value;
_menuStyle = UIMenuStyle.Custom;
Invalidate();
}
}
private Color tabSelectedColor = Color.FromArgb(36, 36, 36);
/// <summary>
/// 边框颜色
/// </summary>
[Description("选中Tab页背景色")]
[DefaultValue(typeof(Color), "36, 36, 36")]
public Color TabSelectedColor
{
get => tabSelectedColor;
set
{
tabSelectedColor = value;
_menuStyle = UIMenuStyle.Custom;
Invalidate();
}
}
private Color tabSelectedForeColor = UIColor.Blue;
/// <summary>
/// 边框颜色
/// </summary>
[Description("选中Tab页字体色")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color TabSelectedForeColor
{
get => tabSelectedForeColor;
set
{
tabSelectedForeColor = value;
_style = UIStyle.Custom;
Invalidate();
}
}
private Color tabUnSelectedForeColor = Color.FromArgb(240, 240, 240);
/// <summary>
/// 边框颜色
/// </summary>
[Description("未选中Tab页字体色")]
[DefaultValue(typeof(Color), "240, 240, 240")]
public Color TabUnSelectedForeColor
{
get => tabUnSelectedForeColor;
set
{
tabUnSelectedForeColor = value;
_menuStyle = UIMenuStyle.Custom;
Invalidate();
}
}
private Color tabSelectedHighColor = UIColor.Blue;
/// <summary>
/// 边框颜色
/// </summary>
[Description("选中Tab页高亮")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color TabSelectedHighColor
{
get => tabSelectedHighColor;
set
{
tabSelectedHighColor = value;
_style = UIStyle.Custom;
Invalidate();
}
}
private UIStyle _style = UIStyle.Blue;
/// <summary>
/// 主题样式
/// </summary>
[DefaultValue(UIStyle.Blue), Description("主题样式")]
public UIStyle Style
{
get => _style;
set => SetStyle(value);
}
[Browsable(false)]
public override Rectangle DisplayRectangle
{
get
{
Rectangle rect = base.DisplayRectangle;
if (tabVisible)
{
return new Rectangle(rect.Left - 4, rect.Top - 4, rect.Width + 8, rect.Height + 8);
}
else
{
return new Rectangle(rect.Left - 4, rect.Top - 5, rect.Width + 8, rect.Height + 9);
}
}
}
private void AfterSetFillColor(Color color)
{
foreach (TabPage page in TabPages)
{
page.BackColor = color;
}
}
public void SetStyle(UIStyle style)
{
SetStyleColor(UIStyles.GetStyleColor(style));
_style = style;
}
public void SetStyleColor(UIBaseStyle uiColor)
{
if (uiColor.IsCustom()) return;
tabSelectedForeColor = tabSelectedHighColor = uiColor.MenuSelectedColor;
_fillColor = uiColor.PlainColor;
Invalidate();
}
private UIMenuStyle _menuStyle = UIMenuStyle.Black;
[DefaultValue(UIMenuStyle.Black)]
[Description("主题风格")]
public UIMenuStyle MenuStyle
{
get => _menuStyle;
set
{
if (value != UIMenuStyle.Custom)
{
SetMenuStyle(UIStyles.MenuColors[value]);
}
_menuStyle = value;
}
}
private void SetMenuStyle(UIMenuColor uiColor)
{
tabBackColor = uiColor.BackColor;
tabSelectedColor = uiColor.SelectedColor;
tabUnSelectedForeColor = uiColor.UnSelectedForeColor;
Invalidate();
}
protected override void CreateHandle()
{
base.CreateHandle();
DoubleBuffered = true;
SizeMode = TabSizeMode.Fixed;
Appearance = TabAppearance.Normal;
Alignment = TabAlignment.Top;
}
private bool showCloseButton;
[DefaultValue(false), Description("所有Tab页面标题显示关闭按钮")]
public bool ShowCloseButton
{
get => showCloseButton;
set
{
showCloseButton = value;
if (showActiveCloseButton) showActiveCloseButton = false;
Invalidate();
}
}
private bool showActiveCloseButton;
[DefaultValue(false), Description("当前激活的Tab页面标题显示关闭按钮")]
public bool ShowActiveCloseButton
{
get => showActiveCloseButton;
set
{
showActiveCloseButton = value;
if (showCloseButton) showCloseButton = false;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 绘制背景色
e.Graphics.Clear(TabBackColor);
if (!TabVisible)
{
return;
}
for (int index = 0; index <= TabCount - 1; index++)
{
Rectangle TabRect = new Rectangle(GetTabRect(index).Location.X - 2, GetTabRect(index).Location.Y - 2, ItemSize.Width, ItemSize.Height);
Bitmap bmp = new Bitmap(TabRect.Width, TabRect.Height);
Graphics g = Graphics.FromImage(bmp);
SizeF sf = e.Graphics.MeasureString(TabPages[index].Text, Font);
int textLeft = ImageList?.ImageSize.Width ?? 0;
if (ImageList != null) textLeft += 4 + 4 + 6;
if (TextAlignment == HorizontalAlignment.Right)
textLeft = (int)(TabRect.Width - 4 - sf.Width);
if (TextAlignment == HorizontalAlignment.Center)
textLeft = textLeft + (int)((TabRect.Width - textLeft - sf.Width) / 2.0f);
// 绘制标题
g.Clear(TabBackColor);
if (index == SelectedIndex)
{
g.Clear(TabSelectedColor);
g.FillRectangle(TabSelectedHighColor, 0, bmp.Height - 4, bmp.Width, 4);
}
g.DrawString(TabPages[index].Text, Font, index == SelectedIndex ? tabSelectedForeColor : TabUnSelectedForeColor, textLeft, TabRect.Top + 2 + (TabRect.Height - sf.Height - 4) / 2.0f);
var menuItem = Helper[index];
bool show1 = TabPages[index].Text != MainPage;
bool show2 = menuItem == null || !menuItem.AlwaysOpen;
bool showButton = show1 && show2;
if (showButton)
{
if (ShowCloseButton || (ShowActiveCloseButton && index == SelectedIndex))
{
g.DrawFontImage(77, 28, index == SelectedIndex ? tabSelectedForeColor : TabUnSelectedForeColor, new Rectangle(TabRect.Width - 28, TabRect.Top, 24, TabRect.Height));
}
}
// 绘制图标
if (ImageList != null)
{
int imageIndex = TabPages[index].ImageIndex;
if (imageIndex >= 0 && imageIndex < ImageList.Images.Count)
{
g.DrawImage(ImageList.Images[imageIndex], 4 + 6, TabRect.Y + (TabRect.Height - ImageList.ImageSize.Height) / 2.0f, ImageList.ImageSize.Width, ImageList.ImageSize.Height);
}
}
if (RightToLeftLayout && RightToLeft == RightToLeft.Yes)
{
bmp = bmp.HorizontalFlip();
}
e.Graphics.DrawImage(bmp, TabRect.Left, TabRect.Top);
bmp.Dispose();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
int removeIndex = -1;
for (int index = 0; index <= TabCount - 1; index++)
{
Rectangle TabRect = new Rectangle(GetTabRect(index).Location.X - 2, GetTabRect(index).Location.Y - 2, ItemSize.Width, ItemSize.Height);
Rectangle rect = new Rectangle(TabRect.Right - 28, TabRect.Top, 24, TabRect.Height);
if (e.Location.InRect(rect))
{
removeIndex = index;
break;
}
}
if (removeIndex < 0 || removeIndex >= TabCount)
{
return;
}
var menuItem = Helper[removeIndex];
bool show1 = TabPages[removeIndex].Text != MainPage;
bool show2 = menuItem == null || !menuItem.AlwaysOpen;
bool showButton = show1 && show2;
if (showButton)
{
if (ShowCloseButton)
{
if (BeforeRemoveTabPage == null || BeforeRemoveTabPage.Invoke(this, removeIndex))
{
RemoveTabPage(removeIndex);
}
}
else if (ShowActiveCloseButton && removeIndex == SelectedIndex)
{
if (DrawedIndex == removeIndex)
{
if (BeforeRemoveTabPage == null || BeforeRemoveTabPage.Invoke(this, removeIndex))
{
RemoveTabPage(removeIndex);
}
}
}
}
}
public delegate bool OnBeforeRemoveTabPage(object sender, int index);
public delegate void OnAfterRemoveTabPage(object sender, int index);
public event OnBeforeRemoveTabPage BeforeRemoveTabPage;
public event OnAfterRemoveTabPage AfterRemoveTabPage;
[DefaultValue(false)]
[Description("多页面框架时,包含UIPage,在点击Tab页关闭时关闭UIPage")]
public bool AutoClosePage { get; set; }
internal void RemoveTabPage(int index)
{
if (index < 0 || index >= TabCount)
{
return;
}
TabPage tabPage = TabPages[index];
if (AutoClosePage)
{
var pages = tabPage.GetControls<UIPage>();
for (int i = 0; i < pages.Count; i++)
{
pages[i].Final();
pages[i].Dispose();
pages[i] = null;
}
}
TabPages.Remove(TabPages[index]);
AfterRemoveTabPage?.Invoke(this, index);
if (TabCount == 0) return;
if (index == 0) SelectedIndex = 0;
if (index > 0) SelectedIndex = index - 1;
}
public enum UITabPosition
{
Left,
Right
}
[DefaultValue(UITabPosition.Left)]
[Description("标签页显示位置")]
public UITabPosition TabPosition
{
get => (RightToLeftLayout && RightToLeft == RightToLeft.Yes)
? UITabPosition.Right
: UITabPosition.Left;
set
{
RightToLeftLayout = value == UITabPosition.Right;
RightToLeft = (value == UITabPosition.Right) ? RightToLeft.Yes : RightToLeft.No;
}
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
Init(SelectedIndex);
}
public void Init(int index = 0)
{
if (index < 0 || index >= TabPages.Count)
{
return;
}
if (SelectedIndex != index)
SelectedIndex = index;
List<UIPage> pages = TabPages[SelectedIndex].GetControls<UIPage>();
foreach (var page in pages)
{
page.Init();
}
//List<UITabControlMenu> leftTabControls = TabPages[SelectedIndex].GetControls<UITabControlMenu>();
//foreach (var tabControl in leftTabControls)
//{
// tabControl.Init();
//}
List<UITabControl> topTabControls = TabPages[SelectedIndex].GetControls<UITabControl>();
foreach (var tabControl in topTabControls)
{
tabControl.Init();
}
}
internal IntPtr UpDownButtonHandle => FindUpDownButton();
private IntPtr FindUpDownButton()
{
return (HDC)FindWindowEx(Handle, IntPtr.Zero, UpDownButtonClassName, null);
}
public void OnPaintUpDownButton(UpDownButtonPaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = e.ClipRectangle;
Color upButtonArrowColor = tabUnSelectedForeColor;
Color downButtonArrowColor = tabUnSelectedForeColor;
Rectangle upButtonRect = rect;
upButtonRect.X = 0;
upButtonRect.Y = 0;
upButtonRect.Width = rect.Width / 2 - 1;
upButtonRect.Height -= 1;
Rectangle downButtonRect = rect;
downButtonRect.X = upButtonRect.Right + 1;
downButtonRect.Y = 0;
downButtonRect.Width = rect.Width / 2 - 1;
downButtonRect.Height -= 1;
g.Clear(tabBackColor);
if (Enabled)
{
if (e.MouseOver)
{
if (e.MousePress)
{
//鼠标按下
if (e.MouseInUpButton)
upButtonArrowColor = Color.FromArgb(200, TabSelectedHighColor);
else
downButtonArrowColor = Color.FromArgb(200, TabSelectedHighColor);
}
else
{
//鼠标移动
if (e.MouseInUpButton)
upButtonArrowColor = TabSelectedHighColor;
else
downButtonArrowColor = TabSelectedHighColor;
}
}
}
else
{
upButtonArrowColor = SystemColors.ControlDark;
downButtonArrowColor = SystemColors.ControlDark;
}
g.SmoothingMode = SmoothingMode.AntiAlias;
RenderButton(g, upButtonRect, upButtonArrowColor, ArrowDirection.Left);
RenderButton(g, downButtonRect, downButtonArrowColor, ArrowDirection.Right);
UpDownButtonPaintEventHandler handler = Events[EventPaintUpDownButton] as UpDownButtonPaintEventHandler;
handler?.Invoke(this, e);
}
internal void RenderButton(Graphics g, Rectangle rect, Color arrowColor, ArrowDirection direction)
{
switch (direction)
{
case ArrowDirection.Left:
g.DrawFontImage(61700, 24, arrowColor, rect);
break;
case ArrowDirection.Right:
g.DrawFontImage(61701, 24, arrowColor, rect, 1);
break;
}
}
private static readonly object EventPaintUpDownButton = new object();
private const string UpDownButtonClassName = "msctls_updown32";
private UpDownButtonNativeWindow _upDownButtonNativeWindow;
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (UpDownButtonHandle != IntPtr.Zero)
{
if (_upDownButtonNativeWindow == null)
{
_upDownButtonNativeWindow = new UpDownButtonNativeWindow(this);
}
}
}
protected override void OnCreateControl()
{
base.OnCreateControl();
if (UpDownButtonHandle != IntPtr.Zero)
{
if (_upDownButtonNativeWindow == null)
{
_upDownButtonNativeWindow = new UpDownButtonNativeWindow(this);
}
}
}
protected override void OnHandleDestroyed(EventArgs e)
{
base.OnHandleDestroyed(e);
if (_upDownButtonNativeWindow != null)
{
_upDownButtonNativeWindow.Dispose();
_upDownButtonNativeWindow = null;
}
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
if (UpDownButtonHandle != IntPtr.Zero)
{
if (_upDownButtonNativeWindow == null)
{
_upDownButtonNativeWindow = new UpDownButtonNativeWindow(this);
}
}
if (e.Control is TabPage)
{
e.Control.Padding = new Padding(0);
}
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (UpDownButtonHandle != IntPtr.Zero)
{
if (_upDownButtonNativeWindow == null)
{
_upDownButtonNativeWindow = new UpDownButtonNativeWindow(this);
}
}
}
private class UpDownButtonNativeWindow : NativeWindow, IDisposable
{
private UITabControl _owner;
private bool _bPainting;
private Rectangle clipRect;
public UpDownButtonNativeWindow(UITabControl owner)
{
_owner = owner;
AssignHandle(owner.UpDownButtonHandle);
}
private bool LeftKeyPressed()
{
if (SystemInformation.MouseButtonsSwapped)
{
return (GetKeyState(VK_RBUTTON) < 0);
}
return (GetKeyState(VK_LBUTTON) < 0);
}
/// <summary>
/// 获取鼠标位置
/// </summary>
/// <returns></returns>
private Point GetCursorPos()
{
UITabControl.GetCursorPos(out POINT pos);
return new Point(pos.X, pos.Y);
}
private void DrawUpDownButton()
{
RECT rect = new RECT();
bool mousePress = LeftKeyPressed();
Point cursorPoint = SystemEx.GetCursorPos();
GetWindowRect(Handle, ref rect);
var mouseOver = PtInRect(ref rect, cursorPoint);
cursorPoint.X -= rect.Left;
cursorPoint.Y -= rect.Top;
var mouseInUpButton = cursorPoint.X < clipRect.Width / 2;
using (Graphics g = Graphics.FromHwnd(Handle))
{
UpDownButtonPaintEventArgs e = new UpDownButtonPaintEventArgs(g, clipRect, mouseOver, mousePress, mouseInUpButton);
_owner.OnPaintUpDownButton(e);
}
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_PAINT:
if (!_bPainting)
{
Point UpDownButtonLocation = new Point(_owner.Size.Width - 52, 0);
Size UpDownButtonSize = new Size(52, _owner.ItemSize.Height);
clipRect = new Rectangle(UpDownButtonLocation, UpDownButtonSize);
MoveWindow(Handle, UpDownButtonLocation.X, UpDownButtonLocation.Y, clipRect.Width, clipRect.Height);
PAINTSTRUCT ps = new PAINTSTRUCT();
_bPainting = true;
BeginPaint(m.HWnd, ref ps);
DrawUpDownButton();
EndPaint(m.HWnd, ref ps);
_bPainting = false;
m.Result = new IntPtr(1);
}
else
{
base.WndProc(ref m);
}
break;
default:
base.WndProc(ref m);
break;
}
}
#region IDisposable 成员
public void Dispose()
{
_owner = null;
base.ReleaseHandle();
}
#endregion IDisposable 成员
}
public delegate void UpDownButtonPaintEventHandler(object sender, UpDownButtonPaintEventArgs e);
public class UpDownButtonPaintEventArgs : PaintEventArgs
{
public UpDownButtonPaintEventArgs(
Graphics graphics,
Rectangle clipRect,
bool mouseOver,
bool mousePress,
bool mouseInUpButton)
: base(graphics, clipRect)
{
MouseOver = mouseOver;
MousePress = mousePress;
MouseInUpButton = mouseInUpButton;
}
public bool MouseOver { get; }
public bool MousePress { get; }
public bool MouseInUpButton { get; }
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

基于导航菜单的winForm 基础框架项目(.NET Framework实现)。区别与传统的WinForm采用的MDI实现,框架使用简单,开箱即用,可直接关注业务部分的实现,是开发Window工具类项目的绝好选择。
暂无标签
LGPL-3.0
使用 LGPL-3.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/david10000/WinFormSample.NET4.git
git@gitee.com:david10000/WinFormSample.NET4.git
david10000
WinFormSample.NET4
基于导航菜单的 WinForm 项目的基础框架
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /