지난번 소개한 방법들
이전에 두번에 걸쳐서 GraphicControl을 Mouse로 클릭해서 위치이동 하는 방법에 대해 소개한적이 있습니다.
http://cbuilder.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_tip&no=908
http://cbuilder.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_tip&no=909
첫번재 방법보다 두번째 방법에 약간의 개선이 있지만 ..
그래도 아주 심플하지는 않네요..
이번에 소개하는 방법은 다음과 같이
TImage 나 TLabel , TShape같은 GraphictControl의 OnMouseDown에서
딱 코드 한줄이면 되도록 했습니다.
델파이의 경우
uses
MoveGControl2;
procedure TForm2.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
StartMoveGraphicControl(Label1);
end;
C++Builder의 경우
#include "MoveGControl2.hpp"
//---------------------------------------------------------------------------
void __fastcall TForm1::Label1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
StartMoveGraphicControl(Label1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Shape1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
StartMoveGraphicControl(Shape1);
}
위와같이 GraphicControl의 OnMouseDown 이벤트에서 StartMoveGraphicControl 함수 한번만 호출해주면 됩니다.
그럼
StartMoveGraphicControl 함수가 어떻게 되어있는지 궁금하지 않습니까?
위 코드에 include한 MoveGControl2 유닛에 StartMoveGraphicControl 함수가 구현되어있습니다.
MoveGControl2 유닛
unit MoveGControl2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,Buttons,Gauges;
type
TGraphicControlSubClass = class(TComponent)
private
FPrePt,FCurPt: TPoint;
FControl : TGraphicControl;
FOrgWndProc: TWndMethod;
procedure SubWndProc(var Message:TMessage);
public
procedure Notification(AComponent: TComponent;
Operation: TOperation); virtual;
constructor Create2(Control: TGraphicControl);
destructor Destroy; override;
end;
procedure StartMoveGraphicControl(Control : TGraphicControl);
implementation
{ TGraphicControlSubClass }
constructor TGraphicControlSubClass.Create2(Control: TGraphicControl);
begin
inherited Create(Control);
FOrgWndProc:=Control.WindowProc;
Control.WindowProc:=SubWndProc;
FControl:=Control;
FPrePt:=Mouse.CursorPos;
end;
destructor TGraphicControlSubClass.Destroy;
begin
FControl.WindowProc:=FOrgWndProc;
inherited;
end;
procedure TGraphicControlSubClass.Notification(AComponent: TComponent;
Operation: TOperation);
begin
if(Operation=opRemove)then
begin
self.Free;
end;
end;
procedure TGraphicControlSubClass.SubWndProc(var Message: TMessage);
begin
if (Message.Msg = WM_MOUSEMOVE) then
begin
FCurPt:=Mouse.CursorPos;
FControl.Left:=FControl.Left+(FCurPt.X-FPrePt.X);
FControl.Top := FControl.Top+(FCurPt.Y - FPrePt.Y);
FPrePt:=FCurPt;
end
else if (Message.Msg = WM_LBUTTONUP) then
begin
self.Free;
end;
FOrgWndProc(Message);
end;
procedure StartMoveGraphicControl(Control : TGraphicControl);
begin
TGraphicControlSubClass.Create2(Control);
end;
end.
이상입니다.
그럼...
P.S
이쯤되면 GraphicControl을 Mouse로 drag해서 움직이는것에 대한 고민은 끝낼수 있으려나?....