当前位置:网站首页>WinForm scroll bar beautification
WinForm scroll bar beautification
2022-04-23 07:22:00 【qq_ fifteen million one hundred and ninety-seven thousand four 】
Do it for the first time winfrom application , It took a lot of effort to beautify the scroll bar , Novices in online materials say they either can't understand or can't use . Here I write down my implementation process , Try to write in detail , I hope to help other friends who are struggling with this problem like me .
First, redraw the control and copy it directly Sunday Like snow Written :
https://blog.csdn.net/qq_33212020/article/details/53447135
Create a new user control in the project , Copy it all and you can use it directly , Again Sunday Like snow Thank you . Here, write the usage of the control you have found out to your friends who need it .
The original effect :
Personal effects :
principle : Put two in the design interface panel(pn_info,pn_scroll.AutoScroll Property is set to false, Better name it the same as me , Easy to use code ),pn_info Put it in pn_scroll Inside ,pn_info Release information ,pn_scroll Put the scroll bar .pn_info Add wheel Events , Drive the scroll bar to move . Scroll bar drag event pn_info Rolling (pn_info By changing top Value to simulate scrolling ).pn_info Of location Set to (0,0). I'm here pn_info Add data for dynamic .
Source code :
SundayRXScrollBar sb = new SundayRXScrollBar();// Scroll bar instance
// Window initialization
public void init()
{
history his = new history();
List<history> hisList = his.read();// Get history list
for(var i=0;i<hisList.Count;i++)
{
int timestamp = hisList[i].timestamp;
historyItem item = new historyItem();
item.setDate(toDate(timestamp));
item.chName(timestamp.ToString());// Assign a value to the check box ( Judge the choice of )
item.Location = new System.Drawing.Point(0, i * 115); // Record location
item.setAvgForce(hisList[i].avg_force.ToString("0.#"));
item.setMaxForce(hisList[i].max_force.ToString());
item.setMaxFrequence(hisList[i].max_frequence.ToString());
item.setPunchCount(hisList[i].punch_count.ToString());
item.setTrainTime(hisList[i].train_time.ToString());
item.setRemark(hisList[i].remark.ToString());
pn_info.Controls.Add(item);
}
// The above part is my pn_info add to item, You can't copy , Please change your adding method
// The following contents can be copied completely
// The height of the inner container shall be set to the actual height , Here I each item Is the height of 115
pn_info.Height = hisList.Count * 115;
pn_info.Focus(); // Get focus
sb_init(); // Scroll bar initialization
}
private void sb_init()
{
if (pn_info.Height <= pn_scroll.Height)
{
// When the height of the inner container is less than that of the scroll bar container, the scroll bar will not be produced
pn_info.Width = pn_scroll.Width;
return;
}
sb.Width = 10; // Width of scroll bar (px)
pn_info.Width = pn_scroll.Width-10;
sb.Height = pn_scroll.Height;
// Slider height = Visual height / Actual height * Scroll bar height (pn_scroll.Height)
float length = (float)pn_scroll.Height / (float)pn_info.Height * (float)pn_scroll.Height;
sb.SliderHeight = (int)length;
//sb.SliderColor = Color.FromArgb(2,153,255);// Slider color
//sb.MouseDownSliderColor After clicking the mouse, the slider color
//sb.MouseOverSliderColor The color of the slider when the mouse touches
//sb.BottomColor Scroll bar color
sb.SliderWidthPercent = 1; // The ratio of slider width to scroll bar width ,1 It means full
sb.Dock = DockStyle.Right; // Scroll bar position
sb.ValueChanged += sb_ValueChanged; // Scroll bar drag event
sb.GotFocus += sb_GotFocus; // Scroll bar to get focus events
pn_scroll.Controls.Add(sb); //pn_scroll Add scroll bar
pn_info.MouseWheel += pn_info_MouseWheel; //pn_info Add wheel Events
}
bool isMouseWheel = false; // Prevent the scroll bar driven by the roller from triggering again sb_ValueChanged
//pn_info Wheel event
private void pn_info_MouseWheel(object sender, MouseEventArgs e)
{
isMouseWheel = true;
int scrollRang = 40; // Each roll height (px)
int sbRang = calSbRang(scrollRang); // The actual scrolling range of the scroll bar
if (e.Delta > 0) // Wheel Up
{
if (sb.Value - sbRang<= 0)
{
// Scroll to the top , Set directly to the default value , Prevent errors caused by rolling
sb.Value = 0;
pn_info.Top = 0;
}
else
{
sb.Value -= sbRang;
pn_info.Top += scrollRang;
}
}
else// The roller is down
{
if (sb.Value + sbRang>= 100)
{
// Scroll to the end , Set directly to the default value , Prevent errors caused by rolling
sb.Value = 100;
pn_info.Top = -(pn_info.Height-pn_scroll.Height);
}
else
{
sb.Value += sbRang;
pn_info.Top -= scrollRang;
}
}
}
// Because the scroll bar scrolling range may be decimal , turn int Accuracy will be lost after , Cause a certain error when rolling to the bottom or to the bottom
float residue = 0; //float turn int The decimal part after rounding is accumulated 1
private int calSbRang(int scrollRang)
{
float temp = ((float)scrollRang) / (float)(pn_info.Height - pn_scroll.Height) * 100;// Scroll bar scrolling scale
int sbRang = Convert.ToInt32(temp);// Keep the integer part
residue += temp - (float)sbRang; // Accumulate the rounded decimal part
if (residue >= 1)
{
residue -= 1;
sbRang += 1;
}
return sbRang;
}
// Scroll bar drag
private void sb_ValueChanged(object sender, EventArgs e)
{
if (isMouseWheel) return;
pn_info.Top =-Convert.ToInt32((float)sb.Value / 100 * (float)(pn_info.Height-pn_scroll.Height));
}
// When the scroll bar gets the focus, give the focus to pn_info, Prevent wheel events that trigger the scroll bar ( The sliding speed of the roller of the scroll bar is inconsistent with that in the container )
private void sb_GotFocus(object sender, EventArgs e)
{
isMouseWheel = false;
pn_info.Focus();
}
After several tests , When there is less content , The scroll bar slider extends out of the container .
terms of settlement : Redraw control code SliderHeight Comment this sentence in the method ( The original meaning should be when the scroll bar height - Slider height <100 If so, set the scroll bar height +100, I don't know why the author should deal with it like this )
版权声明
本文为[qq_ fifteen million one hundred and ninety-seven thousand four ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230609265052.html
边栏推荐
- Chapter 2 pytoch foundation 2
- cmder中文乱码问题
- 1.2 preliminary pytorch neural network
- [2021 book recommendation] practical node red programming
- MySQL notes 1_ database
- [point cloud series] a rotation invariant framework for deep point cloud analysis
- 【点云系列】Fully-Convolutional geometric features
- Chapter 2 pytoch foundation 1
- 【点云系列】 场景识别类导读
- 【点云系列】SO-Net:Self-Organizing Network for Point Cloud Analysis
猜你喜欢
C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
MySQL的安装与配置——详细教程
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
winform滚动条美化
GEE配置本地开发环境
[recommendation for new books in 2021] professional azure SQL managed database administration
【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
[2021 book recommendation] effortless app development with Oracle visual builder
Record WebView shows another empty pit
随机推荐
Chapter 8 generative deep learning
torch. mm() torch. sparse. mm() torch. bmm() torch. Mul () torch The difference between matmul()
Pymysql connection database
第2章 Pytorch基础2
Visual studio 2019 installation and use
Use originpro express for free
MySQL notes 4_ Primary key auto_increment
[2021 book recommendation] artistic intelligence for IOT Cookbook
WebView displays a blank due to a certificate problem
Data class of kotlin journey
PyTorch 22. PyTorch常用代码段合集
[2021 book recommendation] effortless app development with Oracle visual builder
【2021年新书推荐】Learn WinUI 3.0
【点云系列】FoldingNet:Point Cloud Auto encoder via Deep Grid Deformation
Cancel remote dependency and use local dependency
Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
1.2 初试PyTorch神经网络
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
MySQL installation and configuration - detailed tutorial
Machine learning III: classification prediction based on logistic regression