去掉/禁止系统菜单里的MOVE的方法 (Winform)

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace deleteSysMenu
{
 /// <summary>
 /// Summary description for Test.
 /// </summary>
 public class Test : System.Windows.Forms.Form
 {
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  [DllImport("user32.dll",EntryPoint="GetSystemMenu")]
  extern static System.IntPtr GetSystemMenu(System.IntPtr hWnd , System.IntPtr bRevert);


  [DllImport("user32.dll",EntryPoint="RemoveMenu")]
  extern static int RemoveMenu (IntPtr hMenu, int nPos, int flags);


  static int MF_BYPOSITION = 0x400;
  static int MF_REMOVE = 0x1000;
  

  public Test()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent(); 

   //
   // TODO: Add any constructor code after InitializeComponent call
   //

   Text                        =  "不让拖动的标题栏"; 
   FormBorderStyle  =  FormBorderStyle.FixedDialog; 
   MaximizeBox          =  false; 
   MinimizeBox          =  false; 

    }

  private const int WM_NCHITTEST = 0x84;
  private const int HTCAPTION = 0x2;
  private const int HTCLIENT = 0x1;

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  //method 1禁止标题栏拖动

  protected  override  void  WndProc(ref  Message  m) 
  { 
   base.WndProc(ref  m); 
   if(m.Msg  ==   WM_NCHITTEST  &&  m.Result  ==  (IntPtr)HTCAPTION)
   { 
    m.Result  =  (IntPtr) HTCLIENT; 
   } 
  } 
 
  //method 2去掉系统菜单
 
  private const int WS_SYSMENU = 0x00080000;

  protected override CreateParams CreateParams
  {
   get
   {
    CreateParams cp =  base.CreateParams;
    cp.Style = cp.Style & ~WS_SYSMENU;
    return cp;
   }
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   //
   // Test
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(292, 266);
   this.Name = "Test";
   this.Text = "Test";

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Test());
  }

 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值