C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[917] [Controls] GraphicControl 마우스로 움직이기 3
장성호 [nasilso] 13145 읽음    2009-09-23 11:46
지난번 소개한 방법들
이전에 두번에 걸쳐서 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해서 움직이는것에 대한 고민은 끝낼수 있으려나?....

+ -

관련 글 리스트
917 [Controls] GraphicControl 마우스로 움직이기 3 장성호 13145 2009/09/23
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.