如题呐.新人刚刚学习的C#表示....然后想做一个界面.去掉标题栏窗口也可以自由的移动.
然后就去网上搜索相差的代码.
但是....发现非常无语的是..我找到了代码.却不知道怎么用呢....
表示付上我找到的一段代码..
还前辈们指点下..应该如何使用呢? // 洪星 ‎2006‎年‎11‎月‎14‎日
  // www.hongcing.com 
  // QQ 219402
 
  // 获取窗体的屏幕坐标和鼠标光标的位置(屏幕坐标)
  private void MainForm_MouseDown(object sender, MouseEventArgs e)
 {
     mousePoint = Control.MousePosition;
     formPoint = this.Location;
 }
 
  // 鼠标光标的屏幕坐标
 Point mousePoint;
 
 // 窗体的屏幕坐标
 Point formPoint;
 
 // 响应鼠标移动,并移动窗口
 private void MainForm_MouseMove(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Left)
     {
         Point mousePos = Control.MousePosition;
         this.Location = new Point(mousePos.X - mousePoint.X + formPoint.X, mousePos.Y - mousePoint.Y + formPoint.Y);
     }
 }

解决方案 »

  1.   

    推荐LZ巩固下基本知识吧,
     // 获取窗体的屏幕坐标和鼠标光标的位置(屏幕坐标)
      private void MainForm_MouseDown(object sender, MouseEventArgs e)
     {
         mousePoint = Control.MousePosition;
         formPoint = this.Location;
     }
    这个方法是窗体鼠标按下的事件
     // 响应鼠标移动,并移动窗口
     private void MainForm_MouseMove(object sender, MouseEventArgs e)
     {
         if (e.Button == MouseButtons.Left)
         {
             Point mousePos = Control.MousePosition;
             this.Location = new Point(mousePos.X - mousePoint.X + formPoint.X, mousePos.Y - mousePoint.Y + formPoint.Y);
         }
     }
    这个是鼠标在窗体上移动的事件,
    套学会看事件
    MainForm_MouseMove(object sender, MouseEventArgs e)
    其中MainForm是控件名,MouseMove是事件名sender响应事件的控件,e 键
    以上是我本人个人的理解,有什么地方不对的请随意指出
      

  2.   

    MainForm_MouseMove就是窗体的mousemove事件,你得绑定才能用
      

  3.   


    const int HTLEFT = 10;
    const int HTRIGHT = 11;
    const int HTTOP = 12;
    const int HTTOPLEFT = 13;
    const int HTTOPRIGHT = 14;
    const int HTBOTTOM = 15;
    const int HTBOTTOMLEFT = 0x10;
    const int HTBOTTOMRIGHT = 17;protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case 0x0084:
                base.WndProc(ref m);
                Point vPoint = new Point((int)m.LParam & 0xFFFF,
                    (int)m.LParam >> 16 & 0xFFFF);
                vPoint = PointToClient(vPoint);
                if (vPoint.X <= 5)
                    if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOPLEFT;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOMLEFT;
                    else m.Result = (IntPtr)HTLEFT;
                else if (vPoint.X >= ClientSize.Width - 5)
                    if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOPRIGHT;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOMRIGHT;
                    else m.Result = (IntPtr)HTRIGHT;
                else if (vPoint.Y <= 5)
                    m.Result = (IntPtr)HTTOP;
                else if (vPoint.Y >= ClientSize.Height - 5)
                    m.Result = (IntPtr)HTBOTTOM;
                break;
            case 0x0201://鼠标左键按下的消息 
                m.Msg = 0x00A1;//更改消息为非客户区按下鼠标 
                m.LParam = IntPtr.Zero;//默认值 
                m.WParam = new IntPtr(2);//鼠标放在标题栏内 
                base.WndProc(ref m);
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }
      

  4.   

    重写WndProc,然后捕获WM_LBUTTONDOWN消息,捕获到后向窗体发送鼠标按下标题栏消息:
    protected override void WndProc(ref Message m){
        const int WM_LBUTTONDOWN = 513;
        const int WM_NCLBUTTONDOWN = 161;
        const int HTCAPTION = 2;
        
        switch (m.Msg){
        case WM_LBUTTONDOWN:
            PostMessage(this.handle, WM_NCLBUTTONDOWN, HTCAPTION, m.lParam);
            return ;
        default:
            base.WndProc(ref m);
            break;
        }
    }
    自己修改一下,没有编译过
      

  5.   

    首先贴上这个
    using System.Runtime.InteropServices;
    然后在窗体类的空白处贴上这个
            const int WM_LBUTTONDOWN = 513;
            const int WM_NCLBUTTONDOWN = 161;
            const int HTCAPTION = 2;        [DllImport("user32.dll", SetLastError = true)]
            public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);         protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case WM_LBUTTONDOWN:
                        PostMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, m.LParam);
                        return;
                    default:
                        base.WndProc(ref m);
                        break;
                }
            }
    最终的代码类似这个:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        const int WM_LBUTTONDOWN = 513;
            const int WM_NCLBUTTONDOWN = 161;
            const int HTCAPTION = 2;        [DllImport("user32.dll", SetLastError = true)]
            public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);         protected override void WndProc(ref Message m)
            {
                switch (m.Msg)
                {
                    case WM_LBUTTONDOWN:
                        PostMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, m.LParam);
                        return;
                    default:
                        base.WndProc(ref m);
                        break;
                }
            }    }
    }