---
title: "通过控件移动窗体"
author: "zero.fire"
date: "2008-05-20T16:13:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2008-11-22T18:10:10Z"
id: 274189
url: "https://zero-fire.iteye.com/blog/274189"
---

所谓通过控件移动窗体，常用于无边框窗口的移动，需要通过拖动某个控件来使整个窗体移动。

简单总结下两种方法

### 方法1：HTCLIENT转换为HTCAPTION

函数定义

   private  
    { Private declarations }  
    procedure  WMNCHitTest(var Msg:TWMNCHitTest);message  WM\_NCHITTEST;

函数实现

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) procedure TmForm.WMNCHitTest(var Msg:TWMNCHitTest);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) var  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   p: TPoint;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   DefaultHandler(Msg);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    if  Msg.Result  =  HTCLIENT then begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   with JvHTLabel1  do  begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     p.X: =  Left; p.Y: =  Top;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     p : =  Application.MainForm.ClientToScreen(p);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      if  (Msg.XPos  &gt;  p.X) and (Msg.XPos  &lt;  p.X  +  Width) and  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        (Msg.YPos  &gt;  p.Y) and (Msg.YPos  &lt;  p.Y  +  Height) then  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       Msg.Result: =  HTCAPTION;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       SetCursor(LoadCursor( 0 ,IDC\_SIZEALL));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     end;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   end;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   end;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) end;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

### 方法2：发送系统消息

给目标控件添加OnMouseDown消息处理函数，实现方法很简单

procedure TmForm.JvHTLabel1MouseDown(Sender: TObject; Button: TMouseButton;  
  Shift: TShiftState; X, Y: Integer);  
begin  
  ReleaseCapture;  
  SetCursor(LoadCursor( 0 ,IDC\_SIZEALL));  
  Perform(WM\_SYSCOMMAND,$F012, 0 );  
end;