首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

没有菜单栏的窗体不能移动解决方法

2012-02-07 
没有菜单栏的窗体不能移动传的程序附有代码。不知道哪里的问题??????[解决办法]C# codeusing System using

没有菜单栏的窗体不能移动
传的程序附有代码。不知道哪里的问题??????

[解决办法]

C# code
using System; using System.Windows.Forms; using System.Runtime.InteropServices; class Test : Form {   const int MF_BYPOSITION = 0x0400;   const int MF_REMOVE     = 0x1000;   [DllImport("user32.dll",EntryPoint="GetSystemMenu")]   extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);   [DllImport("user32.dll",EntryPoint="RemoveMenu")]   extern static int RemoveMenu(IntPtr hMenu, int nPos, int flags);   Test()   {     Text            =  "不能移动和改变大小的窗口";     FormBorderStyle = FormBorderStyle.FixedSingle;     MaximizeBox     = false;     MinimizeBox     = false;     RemoveMenu(GetSystemMenu(Handle,IntPtr.Zero),1,MF_BYPOSITION|MF_REMOVE);   }   static void Main()   {     Application.Run(new Test());   } }
[解决办法]
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if ((int)m.Result == HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
return;
break;
}
base.WndProc(ref m);
}
[解决办法]
-_-b
是没标题栏不能移动吧……

按下面的方法,你想要在什么地方移动窗体都行:

C# code
private const int WM_NCLBUTTONDOWN = 0xA1; //消息:左键点击private const int HT_CAPTION = 0x2; //标题栏[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern int SendMessage(System.IntPtr hWnd, int Msg, int wParam, int lParam); //发送消息[System.Runtime.InteropServices.DllImport("user32.dll")]private static extern bool ReleaseCapture();private void Form1_MouseDown(object sender, MouseEventArgs e){  if (e.Button == MouseButtons.Left)  {    ReleaseCapture(); //释放鼠标捕捉    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); //发送左键点击的消息至该窗体(标题栏)  }} 

热点排行