C#-WinForm-無邊框窗體的移動和陰影-API


//窗體移動API,先導入命名空間,在委托MouseDown事件
//移動前准備
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, 

int IParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, 

IntPtr lParam);
private const int WM_SETREDRAW = 0xB;

//窗體移動事件
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (this.WindowState == FormWindowState.Normal)
    {
        ReleaseCapture();
        SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + 

HTCAPTION, 0);
    }
}
無邊框窗體移動API

復制API代碼到窗體代碼結構函數下方,引用命名空間,委托MouseDown的事件(李獻策lxc)

     //窗體陰影API,放在結構函數上方,修改結構函數即可
        const int CS_DropSHADOW = 0x20000;
        const int GCL_STYLE = (-26);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);


        public Form1()
        {
            InitializeComponent();
            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
        }
窗體陰影API

復制API代碼到結構函數上方,修改結構函數即可

 

API - 通用功能接口

做一個如下無邊框的窗體

一、無邊框窗體移動

復制API代碼到窗體代碼結構函數下方,引用命名空間,委托MouseDown的事件

public Form1()
        {
            InitializeComponent();
        }

        //窗體移動API
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        [DllImport("user32")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
        private const int WM_SETREDRAW = 0xB;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }
無窗體移動代碼 復制后

二、窗體陰影

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM