InPowerS.Net

 找回密碼
 註冊
搜索
查看: 1757|回復: 1

C#无边框窗体移动的三种方法

[複製鏈接]
發表於 2009-6-23 11:34:36 | 顯示全部樓層 |閱讀模式
1.重写wndProc代码:

  1. protected override void WndProc(ref Message m)
  2. {        
  3.         const int WM_NCHITTEST = 0x84;
  4.         const int HTCLIENT = 0x01;
  5.         const int HTCAPTION = 0x02;
  6.          if (m.Msg == WM_NCHITTEST)
  7.         {
  8.                 this.DefWndProc(ref m);
  9.                 if (m.Result.ToInt32() == HTCLIENT)
  10.                     m.Result = new IntPtr(HTCAPTION);
  11.                 else
  12.                     base.WndProc(ref m);
  13.         }
  14.         else
  15.         {
  16.                 base.WndProc(ref m);
  17.         }
  18. }
  19. 2.创建消息
  20. 代码:
  21. private const int WM_NCLBUTTONDOWN = 0x00A1;
  22. private const int WM_NCHITTEST = 0x84;
  23. private const int HT_CAPTION = 0x2;
  24. private const int HT_CLIENT = 0x1;
  25. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  26. {
  27.     //this.Capture = false;
  28.     pictureBox1.Capture = false;
  29.     Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
  30.     WndProc(ref   msg);   
  31. }
  32. 3.调用API代码:
  33. using System.Runtime.InteropServices;
  34. [DllImport("user32.dll")]
  35. public static extern bool ReleaseCapture();
  36. [DllImport("user32.dll")]
  37. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  38.    public const int WM_SYSCOMMAND = 0x0112;
  39.    public const int SC_MOVE = 0xF010;
  40. public const int HTCAPTION = 0x0002;

  41.    //在窗体的_MouseDown中加入如下代码:
  42. 如:
  43. private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  44. {
  45. ReleaseCapture();
  46. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  47. }
複製代碼
發表於 2010-3-12 15:00:22 | 顯示全部樓層
提示: 作者被禁止或刪除 內容自動屏蔽
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

小黑屋|Archiver|手機版|InPowerS.Net

GMT+8, 2018-8-21 12:52

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表