CIEContextMenu.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. unit CIEContextMenu;
  2. {$WARN SYMBOL_PLATFORM OFF}
  3. interface
  4. uses
  5. Windows, ActiveX, shellApi,Classes, ComObj, Shdocvw, Registry, IEContext_TLB, StdVcl;
  6. type
  7. TIEContextMenu = class(TAutoObject, IIEContextMenu, IObjectSafety)
  8. private
  9. FObjectSafetyFlags: DWORD;
  10. protected
  11. procedure AddFace(FileName: OleVariant); safecall;
  12. function GetInterfaceSafetyOptions(const IID: TIID; pdwSupportedOptions,
  13. pdwEnabledOptions: PDWORD): HResult; stdcall;
  14. function SetInterfaceSafetyOptions(const IID: TIID; dwOptionSetMask,
  15. dwEnabledOptions: DWORD): HResult; stdcall;
  16. procedure OpenUmc; safecall;
  17. end;
  18. const
  19. AppTitle = '办公助手';
  20. iAtom: PChar = 'Winsoft';
  21. AppKey = '\Software\Winsoft\LxTalk';
  22. CompanyKeyValue = '\Winsoft'; //做OEM版时,此处应该改为对应公司的网址或名称
  23. implementation
  24. uses Variants,ComServ, Clipbrd, Dialogs, SysUtils, StrUtils, UrlMon;
  25. //------------------------------------------------------------------------------
  26. function TIEContextMenu.GetInterfaceSafetyOptions(const IID: TIID;
  27. pdwSupportedOptions, pdwEnabledOptions: PDWORD): HResult;
  28. var
  29. Unk: IUnknown;
  30. begin
  31. if (pdwSupportedOptions = nil) or (pdwEnabledOptions = nil) then
  32. begin
  33. Result := E_POINTER;
  34. Exit;
  35. end;
  36. Result := QueryInterface(IID, Unk);
  37. if Result = S_OK then
  38. begin
  39. pdwSupportedOptions^ := INTERFACESAFE_FOR_UNTRUSTED_CALLER or
  40. INTERFACESAFE_FOR_UNTRUSTED_DATA;
  41. pdwEnabledOptions^ := FObjectSafetyFlags and
  42. (INTERFACESAFE_FOR_UNTRUSTED_CALLER or INTERFACESAFE_FOR_UNTRUSTED_DATA);
  43. end
  44. else begin
  45. pdwSupportedOptions^ := 0;
  46. pdwEnabledOptions^ := 0;
  47. end;
  48. end;
  49. //------------------------------------------------------------------------------
  50. function TIEContextMenu.SetInterfaceSafetyOptions(const IID: TIID;
  51. dwOptionSetMask, dwEnabledOptions: DWORD): HResult;
  52. var
  53. Unk: IUnknown;
  54. begin
  55. Result := QueryInterface(IID, Unk);
  56. if Result <> S_OK then Exit;
  57. FObjectSafetyFlags := dwEnabledOptions and dwOptionSetMask;
  58. end;
  59. //--------------------------------------------------
  60. procedure TIEContextMenu.OpenUmc;
  61. var Registry: TRegistry;
  62. ExeFileName:String;
  63. Handle: HWND;
  64. begin
  65. {从注册表中读取 BaseURL}
  66. ExeFileName:='';
  67. Registry := TRegistry.Create;
  68. try
  69. Registry.RootKey := HKEY_LOCAL_MACHINE;
  70. if Registry.OpenKey(AppKey + CompanyKeyValue, True) then
  71. begin
  72. ExeFileName := Registry.ReadString('ExeFileName');
  73. end;
  74. finally
  75. Registry.Free;
  76. end;
  77. Handle := openmutex(mutex_all_access, False, iAtom);
  78. try
  79. if Handle = 0 then
  80. begin
  81. ShellExecute(0, 'open', PChar('"' + ExeFileName + '"'), nil, nil, SW_SHOWNORMAL);
  82. Exit;
  83. end;
  84. finally
  85. closeHandle(Handle);
  86. end;
  87. end;
  88. //------------------------------------------------------------------------------
  89. procedure TIEContextMenu.AddFace(FileName: OleVariant);
  90. var
  91. FVar: OleVariant;
  92. DestFile,
  93. SrcFile,ExeFileName,
  94. FileExt: String;
  95. TempFile: array[0..MAX_PATH] of char;
  96. Handle: HWND;
  97. begin
  98. Handle := openmutex(mutex_all_access, False, iAtom);
  99. try
  100. if Handle = 0 then
  101. begin
  102. MessageBox(0, PChar(AppTitle + ' 还未启动!'), '提示', MB_ICONINFORMATION);
  103. Exit;
  104. end;
  105. finally
  106. closeHandle(Handle);
  107. end;
  108. FVar := CreateOleObject('RealICQ.RealOAMessengerAutoServer');
  109. try
  110. try
  111. if AnsiSameText(Copy(FileName, 1, 7), 'http://') or
  112. AnsiSameText(Copy(FileName, 1, 6), 'ftp://') or
  113. AnsiSameText(Copy(FileName, 1, 7), 'file://') then
  114. begin
  115. DestFile := AnsiReplaceStr(FileName, '\', '/');
  116. while AnsiPos('/', DestFile) > 0 do
  117. begin
  118. DestFile := Copy(DestFile, AnsiPos('/', DestFile) + 1, Length(DestFile));
  119. end;
  120. GetTempPath(MAX_PATH, TempFile);
  121. GetTempFileName(TempFile, 'IMG', GetTickCount, TempFile);
  122. DestFile := ExtractFilePath(TempFile) + DestFile;
  123. FileExt := ExtractFileExt(DestFile);
  124. if (AnsiSameText(Copy(FileExt, 1, 4), '.JPG')) or (AnsiSameText(Copy(FileExt, 1, 5), '.JPEG')) then
  125. DestFile := AnsiReplaceText(DestFile, FileExt, '.JPG')
  126. else if (AnsiSameText(Copy(FileExt, 1, 4), '.GIF')) then
  127. DestFile := AnsiReplaceText(DestFile, FileExt, '.GIF')
  128. else if (AnsiSameText(Copy(FileExt, 1, 4), '.BMP')) then
  129. DestFile := AnsiReplaceText(DestFile, FileExt, '.BMP');
  130. SrcFile := FileName;
  131. UrlDownLoadToFile(nil, PChar(SrcFile), PChar(DestFile), 0, nil);
  132. FileName := DestFile;
  133. end;
  134. FVar.AddFace(FileName);
  135. except
  136. on E: Exception do MessageBox(0, PChar(E.Message), '错误', MB_ICONINFORMATION);
  137. end;
  138. finally
  139. FVar := Unassigned;
  140. end;
  141. end;
  142. type
  143. TIEContextMenuFactory = class(TAutoObjectFactory)
  144. public
  145. procedure UpdateRegistry(Register: Boolean); override;
  146. end;
  147. procedure AddExtMenuItem(MenuText, Url: string; Contexts:DWord);
  148. var
  149. reg: TRegistry;
  150. begin
  151. Reg := TRegistry.Create;
  152. with Reg do try
  153. RootKey := HKEY_CURRENT_USER;
  154. OpenKey('\Software\Microsoft\Internet Explorer\MenuExt\' + MenuText, True);
  155. WriteString('', Url);
  156. WriteInteger('contexts', contexts);
  157. CloseKey;
  158. finally
  159. Free;
  160. end;
  161. end;
  162. procedure RemoveExtMenuItem(MenuText: string);
  163. var
  164. reg: TRegistry;
  165. begin
  166. Reg := TRegistry.Create;
  167. with Reg do try
  168. RootKey := HKEY_CURRENT_USER;
  169. DeleteKey('\Software\Microsoft\Internet Explorer\MenuExt\' + MenuText);
  170. finally
  171. Free;
  172. end;
  173. end;
  174. function GetDllName: string;
  175. var
  176. Buffer: array[0..261] of Char;
  177. begin
  178. GetModuleFileName(HInstance, Buffer, SizeOf(Buffer));
  179. Result := string(Buffer);
  180. end;
  181. procedure TIEContextMenuFactory.UpdateRegistry(Register: Boolean);
  182. begin
  183. inherited;
  184. if Register then
  185. AddExtMenuItem('将此图片添加为' + AppTitle + '表情', ExtractFilePath(GetDllName)+'html\addFace.htm',2)
  186. else
  187. RemoveExtMenuItem('将此图片添加为' + AppTitle + '表情');
  188. end;
  189. initialization
  190. TIEContextMenuFactory.Create(ComServer, TIEContextMenu, Class_IEContextMenu,
  191. ciMultiInstance, tmApartment);
  192. end.