BCB中实现拖拽Panel 改变位置和大小的代码

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <AppEvnts.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TPanel *Panel1;
    TApplicationEvents *ApplicationEvents1;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall Panel1MouseMove(TObject *Sender, TShiftState Shift,
          int X, int Y);
private:    // User declarations
    void __fastcall MyWndProc(Messages::TMessage &Message);
    TWndMethod oldProc;
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;


void __fastcall TForm1::MyWndProc(Messages::TMessage &Message)
{
    HWND hWnd = Panel1->Handle;
    switch(Message.Msg)
    {
        case WM_NCHITTEST:
        {
            TPoint pt;
            GetCursorPos(&pt);
            pt = Panel1->ScreenToClient(pt);
            TRect rcClient = Panel1->ClientRect;

            if (pt.x <= 20  && pt.y <= 20)//左上角,判断是不是在左上角,就是看当前坐标是不是即在左边拖动的范围内,又在上边拖动的范围内,其它角判断方法类似
            {
                Message.Result = HTTOPLEFT;
            }else if (pt.x>rcClient.right-20 && pt.y<rcClient.top+20)//右上角  
            {  
                Message.Result = HTTOPRIGHT;
                
            }else if (pt.x<rcClient.left+20 && pt.y>rcClient.bottom-20)//左下角  
            {  
                Message.Result = HTBOTTOMLEFT;
            }else if (pt.x>rcClient.right-20 && pt.y>rcClient.bottom-20)//右下角  
            {
                Message.Result = HTBOTTOMRIGHT;

            }else if (pt.x<rcClient.left+20)
            {  
                Message.Result = HTLEFT;
            }else if (pt.x>rcClient.right-20)  
            {
                Message.Result = HTRIGHT;
            }else if (pt.y<rcClient.top+20)
            {  
                Message.Result = HTTOP;
            }
            else if (pt.y > rcClient.Bottom - 20)
            {
                Message.Result = HTBOTTOM;
            }
            else
            {
                oldProc(Message);
            }
        }
        break;
        case WM_SIZE:   //要让窗体能够随着缩放改变,要响应WM-SIZE消息
        {
            RECT rcClient = { 0 };
            ::GetClientRect(hWnd, &rcClient);
            InvalidateRect(hWnd,&rcClient,FALSE);
        }
        break;
        default:
            oldProc(Message);
    }
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    oldProc = Panel1->WindowProc;
    Panel1->WindowProc = MyWndProc;    
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
    WORD SC_DragMove =   0xF012   ;
    ReleaseCapture();
    ((TWinControl*)Sender)->Perform(WM_SYSCOMMAND,SC_DragMove,0);
}
//---------------------------------------------------------------------------

原文地址:https://www.cnblogs.com/songr/p/6097935.html