Winform设置相关

>>  Winform查找根目录

  1) AppDomain.CurrentDomain.BaseDirectory 地址为: d:MyProjectBin

    Application.StartupPath的地址为:d:MyProjectBin

  2)Application.StartupPath只可以用于Winform窗体中 

   AppDomain.CurrentDomain.BaseDirectory可用于Winform窗体中和Dll文件中

>> 如何使Winform实现全屏显示

  1)设置窗体的WindowState=Maxinized

  2)获取窗体中个控件的状态、窗体的初始尺寸、窗体中各控件的初始尺寸

    #region 窗体大小控制
        private ArrayList InitialCrl = new ArrayList();//用以存储窗体中所有的控件名称        
        private ArrayList CrlLocationX = new ArrayList();//用以存储窗体中所有的控件原始位置        
        private ArrayList CrlLocationY = new ArrayList();//用以存储窗体中所有的控件原始位置        
        private ArrayList CrlSizeWidth = new ArrayList();//用以存储窗体中所有的控件原始的水平尺寸
        private ArrayList CrlSizeHeight = new ArrayList();//用以存储窗体中所有的控件原始的垂直尺寸
        private int FormSizeWidth;//用以存储窗体原始的水平尺寸        
        private int FormSizeHeight;//用以存储窗体原始的垂直尺寸                     
        private double FormSizeChangedX;//用以存储相关父窗体/容器的水平变化量        
        private double FormSizeChangedY;//用以存储相关父窗体/容器的垂直变化量         
        private int Wcounter = 0;//为防止递归遍历控件时产生混乱,故专门设定一个全局计数器     

        public void ResetAllCrlState(Control CrlContainer)
        //重新设定窗体中各控件的状态(在与原状态的对比中计算而来)        
        {
            FormSizeChangedX = (double)this.Size.Width / (double)FormSizeWidth;
            FormSizeChangedY = (double)this.Size.Height / (double)FormSizeHeight;
            foreach (Control kCrl in CrlContainer.Controls)
            {
                if (kCrl.Controls.Count > 0)
                {
                    ResetAllCrlState(kCrl);
                }
                Point point = new Point();
                point.X = (int)((int)CrlLocationX[Wcounter] * FormSizeChangedX);
                point.Y = (int)((int)CrlLocationY[Wcounter] * FormSizeChangedY);
                kCrl.Width = (int)((int)CrlSizeWidth[Wcounter] * FormSizeChangedX);
                kCrl.Height = (int)((int)CrlSizeHeight[Wcounter] * FormSizeChangedY);
                kCrl.Bounds = new Rectangle(point, kCrl.Size);
                Wcounter++;
            }
        }
        public void GetInitialFormSize()
        //获得并存储窗体的初始尺寸        
        {
            FormSizeWidth = this.Size.Width;
            FormSizeHeight = this.Size.Height;
        }
        public void GetAllCrlLocation(Control CrlContainer)
        //获得并存储窗体中各控件的初始位置 
        {
            foreach (Control iCrl in CrlContainer.Controls)
            {
                if (iCrl.Controls.Count > 0)
                    GetAllCrlLocation(iCrl);
                InitialCrl.Add(iCrl);
                CrlLocationX.Add(iCrl.Location.X);
                CrlLocationY.Add(iCrl.Location.Y);
            }
        }
        public void GetAllCrlSize(Control CrlContainer)
        //获得并存储窗体中各控件的初始尺寸        
        {
            foreach (Control iCrl in CrlContainer.Controls)
            {
                if (iCrl.Controls.Count > 0)
                    GetAllCrlSize(iCrl);
                CrlSizeWidth.Add(iCrl.Width);
                CrlSizeHeight.Add(iCrl.Height);
            }
        }
      
 
        #endregion
View Code

  3) 在窗体的SizeChanged事件中存储窗体中各控件的初始尺寸

 private void WinLoading_SizeChanged(object sender, EventArgs e)
        {
            Wcounter = 0;
            int counter = 0;
            if (this.Size.Width < FormSizeWidth || this.Size.Height < FormSizeHeight)
            //如果窗体的大小在改变过程中小于窗体尺寸的初始值,
            //则窗体中的各个控件自动重置为初始尺寸,且窗体自动添加滚动条            
            {
                foreach (Control iniCrl in InitialCrl)
                {
                    iniCrl.Width = (int)CrlSizeWidth[counter];
                    iniCrl.Height = (int)CrlSizeHeight[counter];
                    Point point = new Point();
                    point.X = (int)CrlLocationX[counter];
                    point.Y = (int)CrlLocationY[counter];
                    iniCrl.Bounds = new Rectangle(point, iniCrl.Size);
                    counter++;
                }
                this.AutoScroll = true;
            }
            else
            //否则,重新设定窗体中所有控件的大小(窗体内所有控件的大小随窗体大小的变化而变化)            
            {
                this.AutoScroll = false;
                ResetAllCrlState(this);
            }
        }
View Code

  4) 在窗体的构造函数显示空间大小和位置的重绘

 public WinLoading()
        {
            InitializeComponent();
            GetInitialFormSize();
            GetAllCrlLocation(this);
            GetAllCrlSize(this);
        }
View Code

>> 实现Winform窗体颜色渐变,在窗体的Paint事件中重绘窗体

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            System.Drawing.Drawing2D.LinearGradientBrush b = new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), Color.Blue, Color.Green);
            this.ResizeRedraw = true;//在窗体调整大小时候重绘自己
            e.Graphics.FillRectangle(b, new Rectangle(0, 0, this.Width, this.Height));
        }
View Code

>> Winform 实现按钮长按效果

    利用定时器或线程实现

    按钮的MouseDown事件 设备定时器启用

    按钮的MouseUp事件 设备定时器禁用

    具体操作在定时器的Click事件中实现

>> DataGridView奇奇怪怪属性

    DataGridView 本来WInform在现在已经是很过时的东西了,在弄这个DataGridView,各处不明觉厉的设置,真心烂到家的一个控件,在WInform里又想不到其他的东东好呈现数据,还选了这个,记录下各种他的各种怪脾气吧!

    1).DataGridIView 去除第一列的空白列,设置RowHeadsVisible=false

    2).去除无意义的最后一行 设置AllowUserToAddRow=false

    3). DataGridView 控制每一列的显示顺序 设置DisplayIndex

       memberdataDisp.Columns["col0"].DisplayIndex=2效果是 Name='col0'的这一列最终在表格中显示在第三列

    特别注意前提是你的数据库查询的select语句的 每个字段的出现顺序要和表格中显示的顺序一致,不然的话DisplayIndex设置的顺序无效,至于为什么这样,也许他就是为了坑爹的吧。

    4). 禁止列排序 gvSys.Columns["c1"].SortMode=DataGridViewColumnSortMode.NotSortable;

    5)    标题行属性设置: RowHeaderDefaultCellStyle 

      内容行属性设置: RowDefaultCellStyle

    奇数行属性设置: AlternatingRowDefaultCellStyle

>> 其他窗体属性

    都是些常用属性,但是不常用的话,再去找的话就费时费力了,记录下先。

    1). Winfrom设置窗体不能改变大小   设置FormBorderStyle,设置成属性前缀是Fixed的就行了。

    2). Winform窗体如何去除头部的关闭等按钮   ControlBox设置为fasle

    3).设置窗体在屏幕中的现实位置          

      a. 窗体的StartPosition属性是确定窗体位置的

      b. 获取屏幕右下角的坐标,然后减去窗体宽度和高度,就能获取窗体显示在右下角的坐标了。如:   this.Location=new Point(Screen.PrimaryScreen.WorkingArea.Width-this.Width, Screen.PrimaryScreen.WorkingArea.Height-this.Height);

    4).在窗体背景颜色改变时如何消除label的背景色

     lblPstart.BackColor=Color.Transparent;

>> Winform加入.wav声音文件

     step 1:把需要播放的声音文件加入的项目中(wav声音文件)

     step 2:引入命名空间   System.Media.SoundPlayer

     step 3: code实现 

SoundPlayer player = new SoundPlayer();//生命.wav文件的操作类      
player.Stream = Properties.Resources.放松;//加载指定的声音文件 
 
player.PlayLooping();//使用新线程循环播放
player.Play();//使用新线程播放
player.Stop();//若声音文件正在播放,则停止

 >> 获取winform控件上字符串的实际长度

Graphics gSize = this.p_DiseaseType.CreateGraphics();
SizeF size = gSize.MeasureString(strtext, f);

原文地址:https://www.cnblogs.com/eye-like/p/3612300.html