c#实现 无边框可移动最上面源代码

本文介绍如何使用C#实现Windows窗体的拖动功能及如何将窗体始终保持在屏幕最上方。通过重写WndProc方法并利用.NET Framework提供的API,实现了窗体的拖拽操作,并通过调用SetWindowPos函数来控制窗体的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

          
namespace sharpWin
{
/ /设置窗体可移动
    //  Private   Declare   Function   SetWindowPos   Lib   "user32"   Alias   "SetWindowPos"   (ByVal   hwnd   As   Long,   ByVal   hWndInsertAfter   As   Long,   ByVal   x   As   Long,   ByVal   y   As   Long,   ByVal   cx   As   Long,   ByVal   cy   As   Long,   ByVal   wFlags   As   Long)   As   Long 

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
   //设置可移动
        protected override void WndProc(ref Message m)
        {
           // base.WndProc(ref m);

            const int WM_NCHITTEST = 0x84;
            const int HTCLIENT = 0x01;
            const int HTCAPTION = 0x02;

            if (m.Msg == WM_NCHITTEST)
            {
                this.DefWndProc(ref m);
                if (m.Result.ToInt32() == HTCLIENT)
                    m.Result = new IntPtr(HTCAPTION);
                else
                    base.WndProc(ref m);
            }
            else
                base.WndProc(ref m);
        }
      //设置位置最上方或者最下方
        private void ShoveToBackground()
        {
            Win32.SetWindowPos((int)this.Handle,
               Win32.HWND_BOTTOM,
               0, 0, 0, 0,
               Win32.SWP_NOMOVE | Win32.SWP_NOSIZE |
               Win32.SWP_SHOWWINDOW);
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            ShoveToBackground();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            ShoveToBackground();
        }
   
    }

    class Win32
    {
        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern bool SetWindowPos(
           int hWnd,               // window handle
           int hWndInsertAfter,    // placement-order handle
           int X,                  // horizontal position
           int Y,                  // vertical position
           int cx,                 // width
           int cy,                 // height
           uint uFlags);           // window positioning flags
        public const int HWND_BOTTOM = -1;    //最上方
        //public const int HWND_BOTTOM = 0x1;   //最下方
        public const uint SWP_NOSIZE = 0x1;
        public const uint SWP_NOMOVE = 0x2;
        public const uint SWP_SHOWWINDOW = 0x40;


 
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值