C#手动改变自制窗体的大小
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 手动改变自制窗体的大小
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int Example_X = 0;
public static int Example_Y = 0;
public static int Example_W = 0;
public static Point CPoint;
#region 利用窗体上的控件移动窗体
/// <summary>
/// 利用控件移动窗体
/// </summary>
/// <param Frm="Form">窗体</param>
/// <param e="MouseEventArgs">控件的移动事件</param>
public void FrmMove(Form Frm, MouseEventArgs e) //Form或MouseEventArgs添加命名空间using System.Windows.Forms;
{
if (e.Button == MouseButtons.Left)
{
Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
Frm.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
}
}
#endregion
#region 获取鼠标的当前位置
/// <summary>
/// 获取鼠标的当前位置
/// </summary>
/// <param Frm="Form">窗体</param>
/// <param e="MouseEventArgs">窗体上有关鼠标的一些信息</param>
public void FrmScreen_SizeInfo(Form Frm, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Example_X = e.X;
Example_Y = e.Y;
Example_W = Frm.Width;
}
}
#endregion
#region 改变窗体的大小(用于鼠标的移动事件)
/// <summary>
/// 改变窗体的大小(用于鼠标的移动事件)
/// </summary>
/// <param Frm="Form">窗体</param>
/// <param Pan="Panel">设置窗体边框的控件</param>
/// <param e="MouseEventArgs">窗体上有关鼠标的一些信息</param>
public void FrmScreen_EnlargeSize(Form Frm, Panel Pan, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
switch (Pan.Name)
{
case "panel_Right": //如果移动的是窗体的右边框
{
if (this.Width <= 70) //如果窗体的宽度小于等于70
{
Frm.Width = 70; //设置窗体的宽度为70
//如果用鼠标向右移动窗体的右边框
if (Cursor.Position.X - Frm.Left + (Pan.Width - Example_X) > Frm.Width)
{
//根据鼠标的移动值,增加窗体的宽度
Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);
}
break;
}
//根据鼠标的移动值,增加窗体的宽度
Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);
break;
}
case "panel_BR": //如果移动的是窗体的右下角
{
//如果窗体的大小不为窗体大小的最小值
if (this.Width > 70 && this.Height > (panel_Title.Height + panel_Bn.Height + 1))
{
//根据鼠标的移动改变窗体的大小
Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);
Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);
}
else
{
if (this.Width <= 70) //如果窗体的宽度小于等于最小值
{
Frm.Width = 70; //设置窗体的宽度为70
if (this.Height <= (panel_Title.Height + panel_Bn.Height + 1))//如果窗体的高小于最小值
{
Frm.Height = panel_Title.Height + panel_Bn.Height + 1;//设置窗体的最小高度
//如果用鼠标向下移动窗体的底边框
if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height)
{
//根据鼠标的移动值,增加窗体的高度
Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);
}
break;
}
//如果用鼠标向右移动窗体
if (Cursor.Position.X - Frm.Left + (Pan.Width - Example_X) > Frm.Width)
{
//增加窗体的宽度
Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);
}
break;
}
if (this.Height <= (panel_Title.Height + panel_Bn.Height + 1))//如果窗体的高度小于等于最小值
{
Frm.Height = panel_Title.Height + panel_Bn.Height + 1;//设置窗体的高度为最小值
Frm.Width = Cursor.Position.X - Frm.Left + (Pan.Width - Example_X);//改变窗体的宽度
//如果用鼠标向下移动窗体的边框
if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height)
{
Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y);//增加窗体的高度
}
break;
}
}
break;
}
case "panel_Bn"://如果移动的是窗体的底边框
{
if (this.Height <= (panel_Title.Height + panel_Bn.Height + 1))//如果窗体的高度小于等于最小值
{
Frm.Height = panel_Title.Height + panel_Bn.Height + 1;//设置窗体的高度为最小值
//如果用鼠标向下移动窗体的下边框
if (Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y) > Frm.Height)
{
Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y); //增加窗体的高度
}
break;
}
Frm.Height = Cursor.Position.Y - Frm.Top + (Pan.Height - Example_Y); //增加窗体的高度
break;
}
}
}
}
#endregion
private void panel_Right_MouseDown(object sender, MouseEventArgs e)
{
FrmScreen_SizeInfo(this, e);//获取鼠标的当前位置
}
private void panel_Right_MouseMove(object sender, MouseEventArgs e)
{
FrmScreen_EnlargeSize(this, (Panel)sender, e);//改变窗体的大小
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Close();
}
private void panel_TitL_MouseDown(object sender, MouseEventArgs e)
{
int Tem_X = -e.X;
if (Convert.ToInt16(((Panel)sender).Tag.ToString()) == 2)//如果移动的是标题栏的中间部分
Tem_X = -e.X - panel_TitL.Width;
if (Convert.ToInt16(((Panel)sender).Tag.ToString()) == 3)//如果移动的是标题栏的尾端
Tem_X = -(this.Width - ((Panel)sender).Width) - e.X;
CPoint = new Point(Tem_X, -e.Y);
}
private void panel_TitL_MouseMove(object sender, MouseEventArgs e)
{
FrmMove(this, e);
}
}
}
Form1.Designer.cs
namespace 手动改变自制窗体的大小
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.panel_BR = new System.Windows.Forms.Panel();
this.panel_All = new System.Windows.Forms.Panel();
this.panel_TitR = new System.Windows.Forms.Panel();
this.panel_BL = new System.Windows.Forms.Panel();
this.panel_TitZ = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel_TitL = new System.Windows.Forms.Panel();
this.panel_Right = new System.Windows.Forms.Panel();
this.panel_Left = new System.Windows.Forms.Panel();
this.panel_Title = new System.Windows.Forms.Panel();
this.panel_Bottom = new System.Windows.Forms.Panel();
this.panel_Bn = new System.Windows.Forms.Panel();
this.panel_TitZ.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel_Title.SuspendLayout();
this.panel_Bottom.SuspendLayout();
this.SuspendLayout();
//
// panel_BR
//
this.panel_BR.BackColor = System.Drawing.Color.Transparent;
this.panel_BR.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_BR.BackgroundImage")));
this.panel_BR.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.panel_BR.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
this.panel_BR.Dock = System.Windows.Forms.DockStyle.Right;
this.panel_BR.Location = new System.Drawing.Point(284, 0);
this.panel_BR.Name = "panel_BR";
this.panel_BR.Size = new System.Drawing.Size(8, 10);
this.panel_BR.TabIndex = 1;
this.panel_BR.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseMove);
this.panel_BR.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseDown);
//
// panel_All
//
this.panel_All.BackColor = System.Drawing.Color.Honeydew;
this.panel_All.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel_All.Location = new System.Drawing.Point(6, 19);
this.panel_All.Name = "panel_All";
this.panel_All.Size = new System.Drawing.Size(280, 96);
this.panel_All.TabIndex = 13;
//
// panel_TitR
//
this.panel_TitR.BackColor = System.Drawing.Color.Transparent;
this.panel_TitR.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_TitR.BackgroundImage")));
this.panel_TitR.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.panel_TitR.Dock = System.Windows.Forms.DockStyle.Right;
this.panel_TitR.Location = new System.Drawing.Point(284, 0);
this.panel_TitR.Name = "panel_TitR";
this.panel_TitR.Size = new System.Drawing.Size(8, 19);
this.panel_TitR.TabIndex = 1;
this.panel_TitR.Tag = "3";
this.panel_TitR.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseMove);
this.panel_TitR.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseDown);
//
// panel_BL
//
this.panel_BL.BackColor = System.Drawing.Color.Transparent;
this.panel_BL.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_BL.BackgroundImage")));
this.panel_BL.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.panel_BL.Cursor = System.Windows.Forms.Cursors.SizeNS;
this.panel_BL.Dock = System.Windows.Forms.DockStyle.Left;
this.panel_BL.Location = new System.Drawing.Point(0, 0);
this.panel_BL.Name = "panel_BL";
this.panel_BL.Size = new System.Drawing.Size(8, 10);
this.panel_BL.TabIndex = 0;
//
// panel_TitZ
//
this.panel_TitZ.BackColor = System.Drawing.Color.Transparent;
this.panel_TitZ.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_TitZ.BackgroundImage")));
this.panel_TitZ.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.panel_TitZ.Controls.Add(this.pictureBox1);
this.panel_TitZ.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel_TitZ.Location = new System.Drawing.Point(12, 0);
this.panel_TitZ.Name = "panel_TitZ";
this.panel_TitZ.Size = new System.Drawing.Size(272, 19);
this.panel_TitZ.TabIndex = 2;
this.panel_TitZ.Tag = "2";
this.panel_TitZ.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseMove);
this.panel_TitZ.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseDown);
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
this.pictureBox1.Cursor = System.Windows.Forms.Cursors.Hand;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(260, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(11, 11);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Tag = "11";
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// panel_TitL
//
this.panel_TitL.BackColor = System.Drawing.Color.Transparent;
this.panel_TitL.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_TitL.BackgroundImage")));
this.panel_TitL.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.panel_TitL.Dock = System.Windows.Forms.DockStyle.Left;
this.panel_TitL.Location = new System.Drawing.Point(0, 0);
this.panel_TitL.Name = "panel_TitL";
this.panel_TitL.Size = new System.Drawing.Size(12, 19);
this.panel_TitL.TabIndex = 0;
this.panel_TitL.Tag = "1";
this.panel_TitL.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseMove);
this.panel_TitL.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_TitL_MouseDown);
//
// panel_Right
//
this.panel_Right.BackColor = System.Drawing.Color.Transparent;
this.panel_Right.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_Right.BackgroundImage")));
this.panel_Right.Cursor = System.Windows.Forms.Cursors.SizeWE;
this.panel_Right.Dock = System.Windows.Forms.DockStyle.Right;
this.panel_Right.Location = new System.Drawing.Point(286, 19);
this.panel_Right.Name = "panel_Right";
this.panel_Right.Size = new System.Drawing.Size(6, 96);
this.panel_Right.TabIndex = 10;
this.panel_Right.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseMove);
this.panel_Right.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseDown);
//
// panel_Left
//
this.panel_Left.BackColor = System.Drawing.Color.Transparent;
this.panel_Left.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_Left.BackgroundImage")));
this.panel_Left.Dock = System.Windows.Forms.DockStyle.Left;
this.panel_Left.Location = new System.Drawing.Point(0, 19);
this.panel_Left.Name = "panel_Left";
this.panel_Left.Size = new System.Drawing.Size(6, 96);
this.panel_Left.TabIndex = 9;
this.panel_Left.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseMove);
this.panel_Left.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseDown);
//
// panel_Title
//
this.panel_Title.BackColor = System.Drawing.Color.Transparent;
this.panel_Title.Controls.Add(this.panel_TitZ);
this.panel_Title.Controls.Add(this.panel_TitR);
this.panel_Title.Controls.Add(this.panel_TitL);
this.panel_Title.Dock = System.Windows.Forms.DockStyle.Top;
this.panel_Title.Location = new System.Drawing.Point(0, 0);
this.panel_Title.Name = "panel_Title";
this.panel_Title.Size = new System.Drawing.Size(292, 19);
this.panel_Title.TabIndex = 7;
//
// panel_Bottom
//
this.panel_Bottom.BackColor = System.Drawing.Color.Transparent;
this.panel_Bottom.Controls.Add(this.panel_Bn);
this.panel_Bottom.Controls.Add(this.panel_BR);
this.panel_Bottom.Controls.Add(this.panel_BL);
this.panel_Bottom.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel_Bottom.Location = new System.Drawing.Point(0, 115);
this.panel_Bottom.Name = "panel_Bottom";
this.panel_Bottom.Size = new System.Drawing.Size(292, 10);
this.panel_Bottom.TabIndex = 8;
//
// panel_Bn
//
this.panel_Bn.BackColor = System.Drawing.Color.Transparent;
this.panel_Bn.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel_Bn.BackgroundImage")));
this.panel_Bn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.panel_Bn.Cursor = System.Windows.Forms.Cursors.SizeNS;
this.panel_Bn.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel_Bn.Location = new System.Drawing.Point(8, 0);
this.panel_Bn.Name = "panel_Bn";
this.panel_Bn.Size = new System.Drawing.Size(276, 10);
this.panel_Bn.TabIndex = 2;
this.panel_Bn.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseMove);
this.panel_Bn.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel_Right_MouseDown);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 125);
this.Controls.Add(this.panel_All);
this.Controls.Add(this.panel_Right);
this.Controls.Add(this.panel_Left);
this.Controls.Add(this.panel_Title);
this.Controls.Add(this.panel_Bottom);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.Text = "Form1";
this.panel_TitZ.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel_Title.ResumeLayout(false);
this.panel_Bottom.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel_BR;
private System.Windows.Forms.Panel panel_All;
private System.Windows.Forms.Panel panel_TitR;
private System.Windows.Forms.Panel panel_BL;
private System.Windows.Forms.Panel panel_TitZ;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Panel panel_TitL;
private System.Windows.Forms.Panel panel_Right;
private System.Windows.Forms.Panel panel_Left;
private System.Windows.Forms.Panel panel_Title;
private System.Windows.Forms.Panel panel_Bottom;
private System.Windows.Forms.Panel panel_Bn;
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace 手动改变自制窗体的大小
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
C#手动改变自制窗体的大小的更多相关文章
- CSS手动改变DIV高宽
本实例代码可以使DIV可以手动改变大小 效果体验:http://hovertree.com/code/css/resize.htm 代码如下: <!DOCTYPE html> <ht ...
- datagridview随窗体的大小而变,表格填满控件
在C#winform布局的时候,我们拖一个datagridview到窗体上面,将datagridview调整为适合窗体的大小,但是我们运行之后,点击最大化按钮的时候,却发现datagridview的大 ...
- 运行时改变控件的大小(点击后立刻ReleaseCapture,然后计算位移,最后发消息改变位置)——最有趣的是TPanel其实也有窗口标题,因此可发HTCAPTION消息
//光标在控件不同位置时的样式 // 由于拐角这点手动精确实在困难 所以用范围 范围+3 这样很容易就找到这一点了 procedure CtrlMouseMove(Ctrl: TWinControl; ...
- MFC中改变控件的大小和位置(zz)
用CWnd类的函数MoveWindow()或SetWindowPos()能够改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...
- netbeans 窗体字体大小设置
当计算机分辨率变大的时候,打开netbeans的时候,字体就会变得越来越小 看起来很不爽,所要就要改变一下,窗体字体大小. 在网上找到了一段修改netbeans窗体字体大小的配置信息,现标记起来,以便 ...
- MFC中改变控件的大小和位置
用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...
- C#-禁止调整窗体的大小
要是想禁止调整窗体的大小,可以查看:FormBorderStyle属性,该属性的设置中有一个"FixedSingle"的选择项,它可以禁止调整窗体的大小.
- 居然还有WM_TIMECHANGE(只在用户手动改变系统时间时才会产生作用)
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- vim改变多窗口的大小
摘自:http://blog.163.com/lgh_2002/blog/static/44017526201031671927647/ vim改变多窗口的大小 VIM改变窗口大小 *window-r ...
随机推荐
- Windwos Live Writer插件指南
Windows Live Writer 即(WLW) 是一个免费的桌面应用程序,可以用于发布博客. 官网下载地址:https://www.microsoft.com/zh-CN/download/de ...
- 20165215 2017-2018-2《Java程序设计》课程总结
20165215 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 预备作业1:我期望的师生关系:令我记忆深刻的老师,期望的师生关系,本学期的学习规划. 预备作业二 ...
- Mysql报错:Packet for query is too large (1121604 > 1048576).You can change this value on the server by setting the max_allowed_packet variable
看错误信息,发现1048576个字节,正好是1*1024*1024byte,也就是1Mb. 这正是mysql默认的max_allowed_packet值. 使用sql语句: show VARIABLE ...
- linux命令之pssh命令
查看一下pssh命令的帮助文档: [root@test2 ~]# pssh --version [root@test2 ~]# pssh --help Usage: pssh [OPTIONS] co ...
- button theme
children:[ButtonTheme.bar( child:ButtonBar( children:[ FlatButton... ], ),), ]
- S-DES算法实现(C++版本)
密码学实验二: /** :;LaEaHKEEGpPXU7;, .:75pKH11252U252XapZgRQgD6XJscLr;,. :LXpRgGaX521JLw1JswJJsJs22XHPPEZE ...
- newcoder 小A的柱状图(单调栈)题解
题目描述 柱状图是有一些宽度相等的矩形下端对齐以后横向排列的图形,但是小A的柱状图却不是一个规范的柱状图,它的每个矩形下端的宽度可以是不相同的一些整数,分别为a[i] 每个矩形的高度是h[i] ,现在 ...
- Python入门 日志打印
logging # logging导入 import logging # 设置打印的最低级别 logging.basicConfig(level = logging.DEBUG) 使用 debug, ...
- Js — CommonUtil
一些js脚本的公用方法: 1:字符串根据给定的每行长度换行 2:比较两个时间的大小3:计算两个日期间相差的天数 1.字符串根据给定的每行长度换行 /** *words:原始字符串 *avg:每行字数 ...
- 微信小程序计算器后后续
改的眼睛都要瞎了,总算是知道问题出哪了 最后一段 在等号里面计算输入的数组,这个判断的主要操作是将输入的数据的数组进行数和符号的拆分然后再计算,把数按字符串输入数组,然后将数和符号进行拆分 ,最后通过 ...