在Delphi中怎样才可以得到Windows中当前活动窗体的Handle?(100分)

  • 主题发起人 主题发起人 dreamdelphi
  • 开始时间 开始时间
D

dreamdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
在Delphi中怎样才可以得到Windows中当前活动窗体的Handle?[?]
 
vc有GetActiveWindow函数,没有查过delphi
 
delphi一样,因为GetActiveWindow是API。
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp;TheWindow: HWND; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this will hold the active window handle<br>&nbsp; &nbsp;WindowText: array[0..255] of char; // this will hold the text of that window<br>begin<br>&nbsp; &nbsp;{get the handle to the active window associated with this thread}<br>&nbsp; &nbsp;TheWindow:=GetActiveWindow;<br><br>&nbsp; &nbsp;{get the text of that window}<br>&nbsp; &nbsp;GetWindowText(TheWindow,WindowText,255);<br><br>&nbsp; &nbsp;{display the text}<br>&nbsp; &nbsp;Label1.Caption:='Active Window Text: '+string(WindowText);<br>end;<br>
 
那么怎样才能得到输入法的Handle呢?比如微软ABC,(我想做一个虚拟键盘来控制输入法)
 
用GetForegroundWindow得到Windows中当前活动窗体;<br><br>---- 常 用 函 数 有: <br>API函数:BOOL ImmSimulateHotKey<br>(HWND hWnd,DWORD dwHotKeyID);//模拟热键<br>其中Hwnd为程序窗口的句柄,dwHotHKeyID<br>为模拟的热键,若成功则返回True<br>HKL GetKeyboardLayout(DWORD dwLayout);<br>//获得当前键盘状态<br>BOOL ImmIsIME(HKL hKL);<br>//判断当前是否处于中文输入状态,若是则返回True<br>自定义函数:<br>打开相应输入法:OpenIme(imename:string),<br>例OpenIme('全拼输入法');<br>关闭中文输入法:CloseIme;<br>
代码:
<br>procedure Tform1.OpenIme(imename:string);<br><br>var<br>I:integer;<br>myhkl:hkl;<br>begin<br>if ImeName&lt;&gt;'' then<br>begin<br>if Screen.Imes.Count&lt;&gt;0 then<br>begin<br>I:=screen.Imes.indexof(imename);<br>if I&gt;=0 then<br>&nbsp; myhkl:=hkl(screen.Imes.objects[i]);<br>&nbsp; activatekeyboardlayout(myhkl,<br>&nbsp; KLF_ACTIVATE);//设置相应的输入法<br>end;<br>end; <br>end;<br><br><br>procedure TForm1.closeime;<br><br>var<br><br>myhkl:hkl;<br><br>begin<br>&nbsp; myhkl:=GetKeyBoardLayOut(0);<br>&nbsp; if ImmIsIME(myhkl) then <br>&nbsp; //判断是否在中文状态,若是则关闭它<br>&nbsp; immsimulateHotkey(handle,<br>&nbsp; IME_CHotKey_IME_NonIME_Toggle);<br>end;<br><br>
<br><br><br>使用时uses中加上imm <br><br>
 
多人接受答案了。
 
后退
顶部