이 팁은 천리안 프로그래머포럼에서 제가 담당하고 있는 빌더라운지에
작년에 연재했던 내용들을 그대로 올리는 것입니다.
임펠리테리입니다..
일도 하기 싫고 해서... 헤헤~ 어제 알아낸 팁 하나 알려드리죠..
마우스의 동작을 에뮬레이션을 하려면...?
예를 들면, 특정위치로 이동하고.. 버튼을 누르고 떼고 하는 동작들을
에뮬레이션하려면... 어떤 방법이 제일 좋을까요?
뭐, 마우스 메시지를 강제로 SendMessage나 PostMessage등으로 보내는 수도
있겠습니다만, 좀더 멋지게 코딩하고 싶으시다면..
API의 mouse_event()함수를 쓰면 생각보다 아주 쉽게 할 수 있답니다..
예제를 하나 들어봅시다...
폼에 버튼을 아무렇게나 두개 똑~똑~ 떨어뜨려놓구요...
코딩은 다음과 같이...
(델파이 소스는 아래쪽에 있습니다)
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage("Button 1 clicked");
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Application->ProcessMessages();
// Get the point in the center of button 1
TPoint Pt;
Pt.x = Button1->Left + Button1->Width / 2;
Pt.y = Button1->Top + Button1->Height / 2;
// Convert Pt to screen coordinates
Pt = ClientToScreen(Pt);
// Convert Pt to mickeys
Pt.x = Pt.x * 65535. / Screen->Width;
Pt.y = Pt.y * 65535. / Screen->Height;
// Move the mouse
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, Pt.x, Pt.y, 0, 0);
// Simulate the left mouse button down
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, Pt.x, Pt.y, 0, 0);
// Simulate the left mouse button up
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, Pt.x, Pt.y, 0, 0);
}
그대로 베껴서 한번 실행해보세요..
재미있겠죠? ^^;;
이 팁은 볼랜드의 델파이 faq에서 본 겁니다..
참고로 말씀드리면.. 볼랜드의 빌더와 델파이의 faq와 ti는 아주 유용한 팁들이
가득하답니다.. 영어에 알레르기가 생길정도가 아니신 분들은 한번씩 훑어보시면
도움이 많이 될겁니다..
그 내용중엔, 가끔 사람들이 자기가 알아낸것처럼 생색내면서 공개하는 멋진 팁들도
많이 있죠.. 후후....
그럼 이만....
독립문에서 임펠리테리였습니다.
델파이 소스
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Button 1 clicked');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Pt: TPoint;
begin
Application.ProcessMessages;
// Get the point in the center of button 1
Pt.x := Button1.Left + Button1.Width div 2;
Pt.y := Button1.Top + Button1.Height div 2;
// Convert Pt to screen coordinates
Pt := ClientToScreen(Pt);
// Convert Pt to mickeys
Pt.x := Ceil(Pt.x * 65535. / Screen.Width);
Pt.y := Ceil(Pt.y * 65535. / Screen.Height);
// Move the mouse
mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE, Pt.x, Pt.y, 0, 0);
// Simulate the left mouse button down
mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, Pt.x, Pt.y, 0, 0);
// Simulate the left mouse button up
mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, Pt.x, Pt.y, 0, 0);
end;