zoukankan      html  css  js  c++  java
  • c#不规则窗体移动

    1.重写wndProc代码:

    protected override void WndProc(ref Message 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);
            }
    }

    2.创建消息
    代码:

    private const int WM_NCLBUTTONDOWN = 0x00A1;
    private const int WM_NCHITTEST = 0x84;
    private const int HT_CAPTION = 0x2;
    private const int HT_CLIENT = 0x1;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        //this.Capture = false;
        pictureBox1.Capture = false;
        Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
        WndProc(ref   msg);   
    }

    3.调用API代码:

    using System.Runtime.InteropServices;

    [DllImport("user32.dll")]
     public static extern bool ReleaseCapture();
    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
       public const int WM_SYSCOMMAND = 0x0112;
       public const int SC_MOVE = 0xF010;
    public const int HTCAPTION = 0x0002;


       //在窗体的_MouseDown中加入如下代码:
    如:
    private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    ReleaseCapture();
    SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
    }


    如果是wpf:

    给窗口添加鼠标按下(MouseLeftButtonDown)事件:
    xaml代码:
    <Window 
        x:Class="NonRectangularWindow.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="不规则窗口演示"
        SizeToContent="WidthAndHeight" 
        MouseLeftButtonDown="Window_MouseLeftButtonDown">
    </Windows>
    


    private void Window_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)

    {

      if(e.ChangedButton==MouseButton.Left)this.DragMove();

    }
  • 相关阅读:
    Velocity 创建命令缓存
    关于RESTful Web Services和RESTful设计的一些文章
    [转]Restful Web Services浅析
    Silverlight点滴(四)Silverlight访问Web Service报"System.Security.SecurityException: 安全性错误"的处理
    WebDevHelper RESTful服务和Ajax开发时的利器
    [原]读Google Data API源代码一:从创建一个日历(Calendar)开始
    [转]使用 WCF 和 .NET Framework 3.5 进行 HTTP 编程
    [原]Silverlight3 支持所有HTTP方法
    [原]RESTful Web Service之以HTTP PUT方式调用WCF服务
    [原]Google API学习2:Google API稍深入一步
  • 原文地址:https://www.cnblogs.com/fornet/p/2976170.html
Copyright © 2011-2022 走看看