zoukankan      html  css  js  c++  java
  • C# WinForm设置窗口无边框、窗口可移动、窗口显示在屏幕中央、控件去边框

    1)窗口去除边框

    在组件属性中FormBorderStyle设为None

    2)窗口随着鼠标移动而动

    添加引用using System.Runtime.InteropServices;

    在初始化控件{InitializeComponent();}代码后添加

     1         [DllImport("user32.dll")]
     2         public static extern bool ReleaseCapture();
     3         [DllImport("user32.dll")]
     4         public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
     5         bool beginMove = false;//初始化鼠标位置
     6         int currentXPosition;
     7         int currentYPosition;
     8      //获取鼠标按下时的位置
     9      private void QRCode_MouseDown(object sender, MouseEventArgs e)
    10         {
    11             if (e.Button == MouseButtons.Left)
    12             {
    13                 beginMove = true;
    14                 currentXPosition = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
    15                 currentYPosition = MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标
    16             }
    17         }
    18      //获取鼠标移动到的位置
    19         private void QRCode_MouseMove(object sender, MouseEventArgs e)
    20         {
    21             if (beginMove)
    22             {
    23                 this.Left += MousePosition.X - currentXPosition;//根据鼠标x坐标确定窗体的左边坐标x
    24                 this.Top += MousePosition.Y - currentYPosition;//根据鼠标的y坐标窗体的顶部,即Y坐标
    25                 currentXPosition = MousePosition.X;
    26                 currentYPosition = MousePosition.Y;
    27             }
    28         }
    29      //释放鼠标时的位置
    30         private void QRCode_MouseUp(object sender, MouseEventArgs e)
    31         {
    32             if (e.Button == MouseButtons.Left)
    33             {
    34                 currentXPosition = 0; //设置初始状态
    35                 currentYPosition = 0;
    36                 beginMove = false;
    37             }
    38         }

    3)窗口居中显示

    利用C# Form中的StartPosition属性CenterScreen将界面显示在屏幕中央

    若是用代码实现,显示窗体前,应设置此属性,可在调用Show()或是ShowDialog()方法之前或在窗体构造函数中设置此属性,不要在load()事件中改变此属性,不起作用。

    4)界面大小固定

    AutoSizeMode有GrowOnly和GrowAndShrink两种属性

    GrowOnly:生成的窗体可用鼠标调节

    GrowAndShrink:生成的窗体不可用鼠标调节

    5)控件设为透明,无边框

    将控件的FlatStyle设为flat,为解决内边框出现,将FlatAppearance下的BorderSize设为0.

  • 相关阅读:
    Best Time to Buy and Sell Stock III
    Valid Palindrome
    Longest Substring Without Repeating Characters
    Copy List with Random Pointer
    Add Two Numbers
    Recover Binary Search Tree
    Anagrams
    ZigZag Conversion
    Merge k Sorted Lists
    Distinct Subsequences
  • 原文地址:https://www.cnblogs.com/hhhhan1025/p/11065531.html
Copyright © 2011-2022 走看看