• 无Border可移动窗体


     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace NoBoarder
    11 {
    12     public partial class FormParent : Form
    13     {
    14         public FormParent()
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         protected bool isMouseDown = false;
    20         protected Point FormLocation;
    21         protected Point mouseOffset;
    22 
    23         private void FormParent_MouseDown(object sender, MouseEventArgs e)
    24         {
    25             if (e.Button == MouseButtons.Left)
    26             {
    27                 isMouseDown = true;
    28                 FormLocation = this.Location;
    29                 mouseOffset = Control.MousePosition;
    30 
    31             }  
    32         }
    33 
    34         private void FormParent_MouseMove(object sender, MouseEventArgs e)
    35         {
    36             int x = 0;
    37             int y = 0;
    38             if (isMouseDown)
    39             {
    40                 Point pt = Control.MousePosition;
    41                 x = mouseOffset.X - pt.X;
    42                 y = mouseOffset.Y - pt.Y;
    43                 this.Location = new Point(FormLocation.X - x, FormLocation.Y - y);
    44             }  
    45         }
    46 
    47         private void FormParent_MouseUp(object sender, MouseEventArgs e)
    48         {
    49             isMouseDown = false; 
    50         }
    51     }
    52 }

            将Form的FormBorderStyle设置为None, 运行就是没边界的窗口可移动的窗口啦

      程序中多个无Border的窗口就可以直接继承这个类就可以了, 为了便于理解, 这个继承自Form的类叫FormParent, 且在上面添加一个Button, Text = "ParentForm"

      其Click事件添加代码

    1 private void button1_Click(object sender, EventArgs e)
    2         {
    3             FormSon f = new FormSon();
    4             f.Show();
    5             this.Hide();
    6         }

      新建一个Windows Form, 取名叫FormSon, 让其继承于FormParent

    1 public partial class FormSon : FormParent //!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    2     { 
    3         public FormSon()
    4         {
    5             InitializeComponent();
    6         }
    7     }

      运行程序点击FormParent就会弹出一模一样的FormSon出来, 说明还真是亲生的

      但是人总是要有个性的, 我们修改FormSon的Button控件, 发现修改不了, 其实这不是家族遗传病, 是因为FormParent的Modifires属性设置为了Private, 改成Public就可以啦!

           

  • 相关阅读:
    HDU 1828 Picture
    HDU 2656 Counting Game
    HDU 3265 Posters
    Android颜色选择器之案例解析
    实现半透明的popupwindow的源码
    用activity实现半透明的、淡入的menu【原创】
    Android webservice的用法详细讲解
    Android Fragments 详细使用详细介绍
    在Android中扫描wifi热点演示实例教程
    用Dialog创建带箭头的对话框
  • 原文地址:https://www.cnblogs.com/iMirror/p/3883566.html
Copyright © 2020-2023  润新知