There can be no Triumph without Loss,No Victory without Suffering,No Freedom without Sacrifice.
All you have to decide is what to do with the time that is given to you.
Get busy Living, or Get busy Dying?
  首页 | 留言给我 | 订阅 Rss | CLI | 黄白之恋 Posts:158   Hits: 5010726    Comments: 173    
 日历归档
<<  <  2024 - 03  >  >>
SuMoTuWeThFrSa
     12
3456789
10111213141516
17181920212223
24252627282930
31
 About Me
 Name: ZhangSichu
 Sex: Male
 Age: 32
 Email: ZhangSichu@gmail.com
 MSN: ZhangSichu@hotmail.com
 Home: ZhangSichu.com
 WeiBo: weibo.com/zhangsichu
 个人推荐
 分类归档
  ·C++/C(5)  RSS
  ·软件工程(1)  RSS
  ·杂事/随感(26)  RSS
  ·.Net/Java(30)  RSS
  ·面向对象程序设计(5)  RSS
  ·汇编/破解(0)  RSS
  ·平面设计(3)  RSS
  ·SQL(5)  RSS
  ·COM/COM+(2)  RSS
  ·Web开发(81)  RSS
 My Friends
Back Forward Refresh Home 2024年3月28日 星期四 RSS CLI Mine Sweeper. In Javascript.

  将拖拽进行到底 – 终结篇 WindowsMessage
字体大小 [ ]

在《将拖拽进行到底》这篇文(http://www.zhangsichu.com/blogview.asp?Content_Id=15)中的第四种方法,发一个消息告诉系统鼠标在CaptionTitle(每个窗口自己TitleBar)上,这样窗口的拖拽就可以由系统托管了。现在实现了在窗口中任意位置单击鼠标拖拽窗体。这两天,我又发现了一个东东,几乎可以完美的实现这个思想,用SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y))或者用UINT CMFCDragDlg::OnNcHitTest(CPoint point)直接返回HTCAPTION。SendMessage的思想是窗口拖拽就直接由系统托管,不管鼠标点击到那里,都告诉系统,鼠标在窗体的CaptionBar上—发伪装的消息。用UINT CMFCDragDlg::OnNcHitTest(CPoint point)的含义是,直接处理系统的Message,不让系统托管了。在MFC中这两种方法的处理和实现有很多文章在介绍了,这里就不写了。这里就在C#下用类似的方法,来实现SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y))和UINT CMFCDragDlg::OnNcHitTest(CPoint point)。
SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y)) 方案:
Override WndProc 方法当有WM_LBUTTONDOWN消息时处理。
protected override void WndProc(ref Message m)
    {
      Point screenPoint= Control.MousePosition;
      Point clientPoint = this.PointToClient(screenPoint);
      if (m.Msg == UnmanagedMethods.WM_LBUTTONDOWN)
      {
        
        if(rectangleMin.Contains(clientPoint))
        {
          this.WindowState = FormWindowState.Minimized;
          return;
        }
        if(rectangleClose.Contains(clientPoint))
        {
          this.Close();
          return;
        }
        UnmanagedMethods.SendMessage(this.Handle, UnmanagedMethods.WM_NCLBUTTONDOWN, UnmanagedMethods.HTCAPTION, 0);
        = UnmanagedMethods.MakeLParam(screenPoint.X,clientPoint.Y);
        return;

      }
      base.WndProc (ref m);
    }

UnmanagedMethods.SendMessage(this.Handle, UnmanagedMethods.WM_NCLBUTTONDOWN, UnmanagedMethods.HTCAPTION, 0);
        = UnmanagedMethods.MakeLParam(screenPoint.X,clientPoint.Y);
        return;
发消息告诉系统自己在CaptionBar上。

UINT CMFCDragDlg::OnNcHitTest(CPoint point) 方案:
自己处理消息,自己修改截获的消息,然后还给系统,告诉系统自己在CaptionBar上。

    protected override void WndProc(ref Message m)
    {
      Point screenPoint= Control.MousePosition;
      Point clientPoint = this.PointToClient(screenPoint);
      if (m.Msg == UnmanagedMethods.WM_LBUTTONDOWN)
      {
        
        if(rectangleMin.Contains(clientPoint))
        {
          this.WindowState = FormWindowState.Minimized;
          return;
        }
        if(rectangleClose.Contains(clientPoint))
        {
          this.Close();
          return;
        }
        m.WParam = new IntPtr(UnmanagedMethods.HTCAPTION);
        m.Msg = UnmanagedMethods.WM_NCLBUTTONDOWN;
        m.LParam = UnmanagedMethods.MakeLParam(screenPoint.X,clientPoint.Y);

        }
      base.WndProc (ref m);
    }

在Windows 2003 Server Enterprise Edition + VS 2003 Enterprise Edition 编译通过没有问题。


File: 本文实例代码
  Posted @ 4/17/2006 1:41:08 PM | Hits (71340) | Comment (0

  Post Comment
标题 *
作者 *
密码 记住我
评论 *
    


Stable in Firefox 1.5 2.0Stable in IE6 IE7Stable in MozillaStable in Netscape
ZhangSichu.com V0.1.7507
Powered By ZhangSichu
Copyright © ZhangSichu
Download ZhangSichu.com source code. Download source code