ParentWindow가 없는 기본 컨텍스트 윈도우를 조물딱거리기 위해 핸들을 가져오다 보니 이상한 일이 일어남.
아래처럼 하면 핸들값이 65562로 나오고 GetWindowRect로 가져온 Rect값이 (0, 0, 100, 100) 으로 나옴.
function GetContextMenuRect: TRect;
var
hContextMenu: HWND;
cRect: TRect;
begin
// 메뉴키 눌러 ContextMenu 호출
Keybd_Event(VK_APPS, 0,0,0);
Keybd_Event(VK_APPS, 0,KEYEVENTF_KEYUP,0);
// 핸들 가져오기
hContextMenu:=FindWindow('#32768', nil);
// Rect 가져오기
GetWindowRect(hContextMenu, cRect);
Result:=cRect;
end;
var
hContextMenu: HWND;
cRect: TRect;
begin
// 메뉴키 눌러 ContextMenu 호출
Keybd_Event(VK_APPS, 0,0,0);
Keybd_Event(VK_APPS, 0,KEYEVENTF_KEYUP,0);
// 핸들 가져오기
hContextMenu:=FindWindow('#32768', nil);
// Rect 가져오기
GetWindowRect(hContextMenu, cRect);
Result:=cRect;
end;
그래서 저 상태에서 GetWindowLong으로 이것저것 값을 때려보고 내린 결론은,
ContextMenu가 아직 완전히 생성되지 않은 상태에서 가져온 것이 아닐까 하는 것.
핸들값이 특정값으로 일정하고 GWL_WNDPROC의 값이 0 인 것 등 완전히 동작하는 형태의 윈도우가 아니었음.
시간차를 두고 (그래봐야 찰나의 시간) 여러번 시도하면 언젠가는 제대로 된 윈도우를 가져옴.
그러므로 ContextMenu Window를 가지고 뭘 할려면 핸들을 받아올 때 제대로 된 놈이 나올때까지 받아야 함.
아래처럼.
기준을 GWL_WNDPROC이 제값을 가질 때로 결정.
function GetContextMenuRect(iLimit: DWORD=0): TRect;
var
hContextMenu: HWND;
cRect: TRect;
Tick: DWORD;
begin
// 메뉴키 눌러 ContextMenu 호출
Keybd_Event(VK_APPS, 0,0,0);
Keybd_Event(VK_APPS, 0,KEYEVENTF_KEYUP,0);
// 핸들 가져오기
Tick:=GetTickCount;
repeat
hContextMenu:=FindWindow('#32768', nil);
until ((GetWindowLong(hContextMenu, GWL_WNDPROC)>0) or ((iLimit>0) and ((GetTickCount-Tick)>iLimit)));
// Rect 가져오기
GetWindowRect(hContextMenu, cRect);
Result:=cRect;
end;
var
hContextMenu: HWND;
cRect: TRect;
Tick: DWORD;
begin
// 메뉴키 눌러 ContextMenu 호출
Keybd_Event(VK_APPS, 0,0,0);
Keybd_Event(VK_APPS, 0,KEYEVENTF_KEYUP,0);
// 핸들 가져오기
Tick:=GetTickCount;
repeat
hContextMenu:=FindWindow('#32768', nil);
until ((GetWindowLong(hContextMenu, GWL_WNDPROC)>0) or ((iLimit>0) and ((GetTickCount-Tick)>iLimit)));
// Rect 가져오기
GetWindowRect(hContextMenu, cRect);
Result:=cRect;
end;
'Delphi' 카테고리의 다른 글
[Delphi] 로컬 파일 크기 가져오기 (0) | 2010.05.06 |
---|---|
[Delphi] 핫키 등록하고 내가 쓰기 (0) | 2010.05.04 |
[Delphi] CreateProcess 완료 시점 (WaitForInputIdle) (0) | 2010.04.28 |
[Delphi] DLL의 Form의 Deactivate 처리 (0) | 2010.04.26 |
[Delphi] 다른 어플리케이션에 Ctrl+V 보내기 (0) | 2010.04.26 |