2,497
社区成员
发帖
与我相关
我的任务
分享
{
Microsoft Windows 文本服务框架(TSF)
輸入法相關函數
author:HAB
}
unit uTSFUtil;
interface
uses
System.SysUtils,
System.Classes;
type
TTSFUtil = class
private
class procedure DoImes(var AInputMethods: TStrings; const AName: string = '');
public
class function GetImes: TStrings; //獲得輸入法列表
class procedure SetImeName(const AName: string); //設置系統當前輸入法
end;
implementation
uses
Winapi.MsCTF;
var
mImeList: TStrings;
type
TLanguageProfile = class
private
FName: string;
FProfile: TF_LANGUAGEPROFILE;
public
property Name: string read FName write FName;
property Profile: TF_LANGUAGEPROFILE read FProfile write FProfile;
end;
{ TTSFUtil }
class procedure TTSFUtil.DoImes(var AInputMethods: TStrings; const AName: string);
var
profiles: PPTfInputProcessorProfiles;
Iprofiles: ITfInputProcessorProfiles;
plangid: Word;
enumerator: IEnumTfLanguageProfiles;
langProfile: TF_LANGUAGEPROFILE;
FetchedItems: Cardinal;
pbstrProfile: WideString;
pfEnable: Integer;
oLanPro: TLanguageProfile;
begin
if not IsMSCTFAvailable then Exit;
New(profiles);
try
if TF_CreateInputProcessorProfiles(profiles) = S_OK then
begin
Iprofiles := ITfInputProcessorProfiles(profiles^);
Iprofiles.GetCurrentLanguage(plangid);
if plangid > 0 then
begin
Iprofiles.EnumLanguageProfiles(plangid, enumerator);
if enumerator <> nil then
begin
enumerator.Next(1, langProfile, FetchedItems);
while FetchedItems > 0 do
begin
//langProfile.fActive; // -1时为当前激活输入法
pbstrProfile := '';
if Iprofiles.GetLanguageProfileDescription(langProfile.clsid, langProfile.langid, langProfile.guidProfile, pbstrProfile) = S_OK then
begin
if Iprofiles.IsEnabledLanguageProfile(langProfile.clsid, langProfile.langid, langProfile.guidProfile, pfEnable) = S_OK then
begin
if pfEnable = 1 then
begin
//显示TSF输入法名称
oLanPro := TLanguageProfile.Create;
oLanPro.Name := pbstrProfile;
oLanPro.Profile := langProfile;
AInputMethods.AddObject(pbstrProfile, oLanPro);
//激活指定输入法(clsid、 guidProfile为空时,取消激活)
if not AName.IsEmpty and SameText(pbstrProfile, AName) then
Iprofiles.ActivateLanguageProfile(langProfile.clsid, langProfile.langid, langProfile.guidProfile);
end;
end;
end;
enumerator.Next(1, langProfile, FetchedItems);
end;
end;
end;
end;
finally
Dispose(profiles);
end;
end;
class function TTSFUtil.GetImes: TStrings;
begin
if mImeList = nil then
begin
mImeList := TStringList.Create(True);
DoImes(mImeList, '');
end;
Result := mImeList;
end;
class procedure TTSFUtil.SetImeName(const AName: string);
var
sList: TStrings;
iIndex: Integer;
profiles: PPTfInputProcessorProfiles;
Iprofiles: ITfInputProcessorProfiles;
plangid: Word;
oLanPro: TLanguageProfile;
langProfile: TF_LANGUAGEPROFILE;
begin
sList := GetImes;
iIndex := sList.IndexOf(AName);
if iIndex > - 1 then
begin
oLanPro := TLanguageProfile(sList.Objects[iIndex]);
langProfile := oLanPro.Profile;
New(profiles);
try
if TF_CreateInputProcessorProfiles(profiles) = S_OK then
begin
Iprofiles := ITfInputProcessorProfiles(profiles^);
Iprofiles.GetCurrentLanguage(plangid);
if plangid > 0 then
begin
Iprofiles.ActivateLanguageProfile(langProfile.clsid, langProfile.langid, langProfile.guidProfile);
end;
end;
finally
Dispose(profiles);
end;
end;
end;
initialization
finalization
FreeAndNil(mImeList);
end.