博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#仿QQ设置界面导航
阅读量:6344 次
发布时间:2019-06-22

本文共 7086 字,大约阅读时间需要 23 分钟。

效果预览,选择左边标签,右边内容会自动滚动到适当位置

 

 

public class AnchorPanel    {        List
lst = new List
(); Control MenuPan { get; set; } XtraScrollableControl XSControl; public AnchorPanel(Panel PanMenu, XtraScrollableControl xtraScrollableControl) { MenuPan = PanMenu; XSControl = xtraScrollableControl; XSControl.Scroll += XSControl_Scroll; XSControl.MouseWheel += XSControl_MouseWheel; XSControl.SizeChanged += XSControl_SizeChanged; XSControl.VerticalScroll.LargeChange = 20; } void XSControl_SizeChanged(object sender, EventArgs e) { if (LastAnchor != null && LastAnchorIniHeight < (sender as Control).Height) { LastAnchor.AnchorContainer.Height = (sender as Control).Height; } } #region 容器滚动条移动事件 void XSControl_MouseWheel(object sender, MouseEventArgs e) { XSControl_Scroll(sender, null); } void XSControl_Scroll(object sender, XtraScrollEventArgs e) { CurrentLable = GetMenu((sender as XtraScrollableControl).VerticalScroll.Value); } #endregion #region 添加锚点 PanelMenu LastAnchor; int LastAnchorIniHeight; ///
/// 添加锚点 /// ///
默认为控件的Top,Height,Text属性 ///
是否是最后一一个锚点,为了保证最后一个锚点定位在顶部,需要动态设置最后一个锚点的高度,如果最后一个锚点区域高度小于容器高度,则设置其高度为容器高度 public void AddAnchor(Control col, bool LastControl) { AddAnchor(col, col.Text, LastControl); } ///
/// 添加锚点 /// ///
默认为控件的Top,Height属性 ///
如果Caption为空则取Col的Text属性 ///
是否是最后一一个锚点,为了保证最后一个锚点定位在顶部,需要动态设置最后一个锚点的高度,如果最后一个锚点区域高度小于容器高度,则设置其高度为容器高度 public void AddAnchor(Control col, string Caption, bool LastControl) { Label lbl = new Label() { AutoSize = false, Dock = System.Windows.Forms.DockStyle.Top, Location = new System.Drawing.Point(0, 0), /*lbl.Size = new System.Drawing.Size(219, 37);*/ Height = 37, TabIndex = 0, Text = Caption, TextAlign = System.Drawing.ContentAlignment.MiddleRight, Tag = col.Top.ToString() }; IniEventLable(lbl); if (LastControl) { LastAnchor = new PanelMenu(lbl, col); LastAnchorIniHeight = col.Height; lst.Add(LastAnchor); } else lst.Add(new PanelMenu(lbl, col)); MenuPan.Controls.Add(lbl); MenuPan.Controls.SetChildIndex(lbl, 0); } #endregion ///
/// 根据滚动条位置获得对应的锚点空间 /// ///
滚动条的值 ///
public Label GetMenu(int ScrollValue) { Label lbl = null; foreach (PanelMenu menu in lst) { if (menu.Top <= ScrollValue && menu.Buttom > ScrollValue) lbl = menu.Label; } if (lbl == null) { return null; } return lbl; } ///
/// 初始化锚点的事件 /// ///
void IniEventLable(Label lbl) { lbl.MouseEnter += lbl_MouseEnter; lbl.MouseLeave += lbl_MouseLeave; lbl.MouseClick += lbl_MouseClick; } #region 锚点单击 Label _CurrentLable; public Label CurrentLable { set { if (value == null) return; if (_CurrentLable == value) return; value.BackColor = Color.LightPink; if (_CurrentLable != null) _CurrentLable.BackColor = Color.Transparent; _CurrentLable = value; } get { return _CurrentLable; } //{ return CurrentLable; } } ///
/// 鼠标点击 /// ///
///
void lbl_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { CurrentLable = sender as Label; XSControl.VerticalScroll.Value = int.Parse((sender as Label).Tag.ToString()) - CurrentLable.Top; } } ///
/// 设置鼠标进入时背景色 /// ///
///
private void lbl_MouseEnter(object sender, EventArgs e) { if ((sender as Label) != CurrentLable) (sender as Label).BackColor = Color.FromArgb(0xFF, 0xFF, 0x99); } ///
/// 鼠标移出,还原背景色 /// ///
///
private void lbl_MouseLeave(object sender, EventArgs e) { if ((sender as Label) != CurrentLable) (sender as Label).BackColor = Color.Transparent; } #endregion } public class PanelMenu { public PanelMenu(Label label, Control anchorContainer) { Label = label; AnchorContainer = anchorContainer; Top = anchorContainer.Top; } public PanelMenu(Label label, int top, int height) { Label = label; Top = top; Height = height; } ///
/// 锚点定位的容器对象,通常是Panel /// public Control AnchorContainer { get; set; } ///
/// 锚点,Lable /// public Label Label { get; set; } public int Top { get; set; } private int _height; public int Height { get { if (AnchorContainer != null) return AnchorContainer.Height; else return _height; } set { _height = value; } } public int Buttom { get { return Top + Height; } } }

PS:界面新建一个panel1,用于存放左边的导航列表,右边拖一个dev控件:xtraScrollableControl1

在Load里面新增如下代码
使用:

AnchorPanel APanel;       private void Form3_Load(object sender, EventArgs e)       {           APanel = new AnchorPanel(panel1, xtraScrollableControl1);           labelControl1.Text = groupControl6.Height.ToString();           if (groupControl6.Height < xtraScrollableControl1.Height)               groupControl6.Height = xtraScrollableControl1.Height;           panel1.Controls.Clear();           APanel.AddAnchor(groupControl1, false);           APanel.AddAnchor(groupControl2, false);           APanel.AddAnchor(groupControl3, false);           APanel.AddAnchor(groupControl4, false);           APanel.AddAnchor(groupControl5, false);           APanel.AddAnchor(groupControl6, true);                                                                      APanel.CurrentLable = APanel.GetMenu(0);                                                                   }

Demo下载地址:https://github.com/GarsonZhang/JumpPanel

转载于:https://www.cnblogs.com/GarsonZhang/p/4062625.html

你可能感兴趣的文章
python开发使用sentry捕获未知异常
查看>>
docker-compose使用部署jar项目
查看>>
命令小结
查看>>
常用sql
查看>>
Flash AS3 Base64
查看>>
用Eclipse连接正在运行的Tomcat进行Debug的步骤
查看>>
centos pptpd 核心内容
查看>>
软链接和硬链接详解
查看>>
Iptables下TCPMSS使用
查看>>
李开复给创业者的四点建议
查看>>
shell--5、Shell 基本运算符
查看>>
2018 china footwear grand ceremony summit
查看>>
LVS群集——NAT&&DR模型
查看>>
iptables之recent
查看>>
在iPad上使用Office 365
查看>>
Office 365 On MacOS 系列——安装 O365 其他组件
查看>>
关于APT***及如何构建安全防御体系——文档加密产品
查看>>
Redis 第二种安装部署方法
查看>>
Centos6.4下zabbix的安装配置
查看>>
Android 4.1 Surface系统变化说明
查看>>