초보 님이 쓰신 글 :
: 여기저기 찾아봐도 답이 안나와서 문의드립니다 워낙 초보라 ..ㅎㅎ
:
: 특정 폼 안에서 마우스 클릭한 상태로 드래그를 하는데 클릭된 상태에서는 폼 밖으로 이동할수없게 하고싶거든요
:
: MouseDown 이벤트에서 뭔가를 해야할것 같은데...
:
: 찾아보니 GetClientRect를 사용하면 된다는데 영역지정이 잘못되는지 이상한곳에서 마우스 이동이 잡혀버리더라구요
:
: 도와주세요 ㅠㅠ
답변:
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
static bool bMouseCaptured = false;
if ([](const auto& _) -> bool {
return _ ? 0 : bMouseCaptured ? (::ReleaseCapture(), bMouseCaptured = false, 1) : 1;
} (::GetAsyncKeyState(VK_LBUTTON) & 0x8000)) return;
bMouseCaptured = true;
::SetCapture(Handle);
if (TPoint pt{X, Y}; ClientRect.Contains(pt)) return;
else {
pt.x = X < 0 ? 0 : X > ClientRect.right ? ClientRect.right : X;
pt.y = Y < 0 ? 0 : Y > ClientRect.bottom ? ClientRect.bottom : Y;
::ClientToScreen(Handle, &pt);
::SetCursorPos(pt.x, pt.y);
}
}
C++17 컴파일러 사용.
|