当前位置:网站首页>C#窗体设计 鼠标任意 拖拽 窗体大小、控件联动
C#窗体设计 鼠标任意 拖拽 窗体大小、控件联动
2022-04-22 06:05:00 【你的青春我的梦】
1.原理
我们常用的windows软件都支持鼠标任意拖拽大小,这样方便我们实际的使用来适应不同的显示器大小和分辨率;另外带有波形显示的软件,我们希望可以缩放波形显示的区域,我们这里就实现这个功能,看下图就是我们要实现的框图(以窗口通信为例)
另外说一下像C++窗体设计时,是有专门的控件来实现窗体拖拽大小时控件的变化,而c#没有这样的控件(笔者没有发现)

图1:窗体结构框图
先简单说一下Form窗体布局的方式:窗体的大小用width、height表示;窗体中的控件大小也是用width,height表示,但是控件多了一个属性就是坐标位置location,其中有x、y;如下图示例

按钮的位置(x,y)=(112,112),那么以哪里定位x/y呢,看图1,控件的左上角定位x、y的坐标起始点,这样在用鼠标拖拽窗体大小的时候,就可以通过改变x、y的值来改变控件的位置。
另外说一下,在拖拽窗体大小时,一般是波形控件的大小随窗体大小比例(绝对值)增大,像按钮、文本输入、下拉列表等只改变其位置,而不改变其大小,美观性
2.代码编写
我们这里实现的效果是图1中的红色框随窗体大小而改变大小;绿色和蓝色框不改变大小,只随窗体大小而改变位置
public partial class Form1 : Form
{
int chart_dif_axis_width, chart_dif_axis_height, box1_dif_axis_x, box1_dif_axis_y, box2_dif_axis_x, box2_dif_axis_y;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int form_width = this.Width;//窗体宽度
int form_height = this.Height;//窗体高度
int chart_width = chart1.Width;//图表宽度
int chart_height = chart1.Height;//图表高度
int groupbox1_x = groupBox1.Location.X;//groupbox1坐标X
int groupbox1_y = groupBox1.Location.Y;//groupbox1坐标Y
int groupbox2_x = groupBox2.Location.X;//groupbox2坐标X
int groupbox2_y = groupBox2.Location.Y;//groupbox2坐标Y
chart_dif_axis_width = form_width - chart_width;//chart和窗体大小的宽度差不变
chart_dif_axis_height = form_height - chart_height;//chart和窗体大小的高度差不变
box1_dif_axis_x= form_width - groupbox1_x;//计算groupbox1到窗体右边沿的距离
box1_dif_axis_y = groupbox1_y;//读取y轴坐标值
box2_dif_axis_x = form_width - groupbox2_x;//计算groupbox2到窗体右边沿的距离
box2_dif_axis_y = groupbox2_y;//读取y轴坐标值
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
int form_width = this.Width;//窗体宽度
int form_height = this.Height;//窗体高度
//chart1.Location = new Point();//位置(x,y)不变
chart1.Width = form_width - chart_dif_axis_width;//修改chart的的大小-宽度
chart1.Height = form_height - chart_dif_axis_height;//修改chart的的大小-高度
groupBox1.Location = new Point(form_width - box1_dif_axis_x, box1_dif_axis_y);
//修改groupBox1的位置(x,y),实际只修改width即可,高度height不变
groupBox2.Location = new Point(form_width - box2_dif_axis_x, box2_dif_axis_y);
//修改groupBox2的位置(x,y),实际只修改width即可,高度height不变
}
}
代码中已有详细的注释,大家可以自行观看,窗体布局如下

3.演示效果

这里我们右下角鼠标拖拽窗体大小,看到其中的控件按照设计要求而变化;
关于最大化和最小化,因为录屏的问题,动画里没有操作,大家可以试一下;
另外说一下,我们在设计代码中可以限制最小窗体大小,这样当窗体缩小时,是有限制的,满足有的需要最小时所有控件要显示不被隐藏;
版权声明
本文为[你的青春我的梦]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yuyan7045/article/details/122164651
边栏推荐
- 有压接器件的高速PCB,为何不建议做喷锡工艺
- Various commonly used tools status bar cache conversion to obtain app information clipboard file operation encryption and decryption operation
- 一文了解肠-器官轴,肠好身体好
- How to use Anaconda to create a new environment for pycharm and use pycharm to create a new project in this environment
- 如果有一种设计不增加成本又能改善信号质量
- No such file or directory include "XXXXXX. H"“
- 根据xml文件的类别信息和坐标信息,将图像对应的类别区域抠出来,并存入文件夹中。
- 计数排序(C语言实现)------学习笔记
- 神经网络激活函数的作用、类型、设计原则
- Display the up and down floating ripple animation according to the size of the sound shell
猜你喜欢

【Latex】分数写法区别

PCB标记的埋雷设计,短路了却找不到一丝踪迹

How to use Anaconda to create a new environment for pycharm and use pycharm to create a new project in this environment

数字图像处理第三版冈萨雷斯笔记第二章

【AI视野·今日NLP 自然语言处理论文速览 第二十八期】Wed, 1 Dec 2021

多标签分类问题中的评价指标:准确率,交叉熵代价函数

Zotero如何在word中引用跳转到参考文献/建立超链接

一文了解肠-器官轴,肠好身体好

肠-肝轴:宿主-微生物群相互作用影响肝癌发生

Glide conflicts with picture selector Matisse after 4.0.0
随机推荐
The last day of 2018, welcome 2019.
抗生素对微生物组和人体健康的影响
使用keras框架编写三层神经网络解决房价预测问题
细菌感染和抗生素使用
jupyter notebook修改配置文件设置启动目录无效
【Latex】分数写法区别
铁与肠道菌群
ArcGIS performs horizon analysis based on tin ground surface data and building data
PCBA上电容开裂短路,怎么又是设计的错?
肠-肝轴:宿主-微生物群相互作用影响肝癌发生
Use of Excel IFS function
Before glide 4.0, glide set the round picture and set the round corner of the picture (four corners can be set together or some corners can be set separately)
数字图像处理第三版冈萨雷斯笔记第二章
Add files to the server
Conflict between glide 4.0 and fillet clipping centercrop
ArcGIS profile analysis
PCBA上電容開裂短路,怎麼又是設計的錯?
ARM上安装PaddlePaddle
【AI视野·今日Sound 声学论文速览 第二期】Fri, 15 Apr 2022
win10没有本地组策略怎么办?