C#自定义格式窗体

一.自定义格式窗体

  1. 设置窗体无边框: this.FormBorderStyle = FormBorderStyle.None;
  2. 设置窗体大小: this.Size = new Size(500, 400);
  3. 设置窗体居中:this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
    this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
  4. 事件内部的代码严格按照从上到下顺序执行。
  5. 类,对象的抽象化,既对象。
  6. 实例化-创建一个新的对象。 Panel title = new Panel();
  7. 设置title的基本属性:
    title.Width = this.Width;
    title.Height = 30;
    title.BackgroundImage = Image.FromFile("…/…/Image/Title.PNG");(插入背景图片)
  8. 将title添加进Form1中:
    this.Controls.Add(title);
  9. 设置背景图片布局为拉伸:
    title.BackgroundImageLayout = ImageLayout.Stretch;
  10. 设置下半部分窗体的属性,背景
    Panel border = new Panel(); Panel border = new Panel();
    border.Width = this.Width;
    border.Height = this.Height - title.Height;
    border.BackgroundImage = Image.FromFile("…/…/Image/Border.PNG");
    this.Controls.Add(border);
    border.Top = title.Height;
    border.BackgroundImageLayout = ImageLayout.Stretch;
  11. 在这里插入图片描述
  12. 要求鼠标按下title时移动,窗体form1跟着变化,用布尔型变量标记鼠标按下时是true,前提是鼠标按下时。
    bool isDown = false;
  13. 事件中两个参数e、sender,e参数代表事件相对应的数据对象.
  14. if小括号中的条件语句结果是true或者false,里面填写bool类型数据
  15. 添加变量记录鼠标点击的Panle的坐标系中的位置
    Point cilckPoint;
  16. 获取鼠标的位置
    Point currentPosition = MousePosition;
    this.Location = new Point(currentPosition.X-cilckPoint.X,currentPosition.Y-cilckPoint.Y);
  17. 要求当鼠标在拖着窗体移动的时候,可以隐藏
  18. bool isShow = false;(代表窗体目前的一个状态,如果是true代表处于显示状态,只有是false的时候才可以隐藏)
  19. timer1控制隐藏,timer2控制显示
    在这里插入图片描述
  20. 通过if判断隐藏和显示条件,并且要求鼠标拖动到隐藏区域是离开窗体隐藏。
  21. MousePosition:获取鼠标光标的位置
  22. BackgroundImageLayout:获取或设置背景图的布局
  23. .BackgroundImage:设置背景图
  24. !既“非”
  25. 注意隐藏跟显示条件冲突

猜你喜欢

转载自blog.csdn.net/qq_43434300/article/details/83475247