1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
| //------------------------------------------------------------------------------
(* SoLuTions is an Versatile Library for Delphi -
* -
* Commentaire publié sur "www.developpez.net" -
* Post : "Logiciel création de fichier *.CHM " -
* Post Number : 6467889 -
* Post URL = "http://www.developpez.net/forums/d1178097/environnements-developpement/delphi/edi/logiciel-creation-fichier-chm/#post6467889"
* -
* Copyright "SLT Solutions", (©2006) -
* contributeur : ShaiLeTroll (2012) - Renommage Fichier et Correction XE2 -
* contributeur : ShaiLeTroll (2012) - Documentation Insight -
* contributeur : ShaiLeTroll (2014) - Reprise de la SLT<2012> sous C++Builder XE2/XE3 vers la SLT<2013> sous Delphi XE2
* -
* -
* Ce logiciel est un programme informatique servant à aider les développeurs -
* Delphi avec une bibliothèque polyvalente, adaptable et fragmentable. -
* -
* Ce logiciel est régi par la licence CeCILL-C soumise au droit français et -
* respectant les principes de diffusion des logiciels libres. Vous pouvez -
* utiliser, modifier et/ou redistribuer ce programme sous les conditions -
* de la licence CeCILL-C telle que diffusée par le CEA, le CNRS et l'INRIA -
* sur le site "http://www.cecill.info". -
* -
* En contrepartie de l'accessibilité au code source et des droits de copie, -
* de modification et de redistribution accordés par cette licence, il n'est -
* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, -
* seule une responsabilité restreinte pèse sur l'auteur du programme, le -
* titulaire des droits patrimoniaux et les concédants successifs. -
* -
* A cet égard l'attention de l'utilisateur est attirée sur les risques -
* associés au chargement, à l'utilisation, à la modification et/ou au -
* développement et à la reproduction du logiciel par l'utilisateur étant -
* donné sa spécificité de logiciel libre, qui peut le rendre complexe à -
* manipuler et qui le réserve donc à des développeurs et des professionnels -
* avertis possédant des connaissances informatiques approfondies. Les -
* utilisateurs sont donc invités à charger et tester l'adéquation du -
* logiciel à leurs besoins dans des conditions permettant d'assurer la -
* sécurité de leurs systèmes et ou de leurs données et, plus généralement, -
* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. -
* -
* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez -
* pris connaissance de la licence CeCILL-C, et que vous en avez accepté les -
* termes. -
* -
*----------------------------------------------------------------------------*)
/// <remarks>En incluant SLT.Common.Winapi.HtmlHelp qui utilise Vcl.HtmlHelpViewer, cela implique l'enregistrement implicite du Viewer HH (HtmlHelp) conçu pour les CHM</remarks>
/// <remarks>Le Viewer HH (HtmlHelp) conçu pour les CHM supporte aussi un accès direct à une URL HTTP</remarks>
unit SLT.Common.Winapi.HtmlHelp;
interface
uses System.SysUtils, System.Classes, System.Types,
Winapi.Windows,
Vcl.Forms, Vcl.AppEvnts,
Vcl.HtmlHelpViewer;
type
TSLTHtmlHelpManager = class(TObject)
type
THandleArray = array of THandle;
strict private
// Membres privés
FApplicationEvents: TApplicationEvents;
FAutoPosition: Boolean;
FWindowHandles: THandleArray;
private
// Méthodes privées - Gestionnaire du CallBack EnumWindows
procedure ClearWindowHandles();
function IsHelpEventWindowHandle(AWindowHandle: THandle): Boolean;
procedure AddWindowHandle(AWindowHandle: THandle);
// Méthodes privées
function OpenMicrosoftOfficeWord(const AFileName: string): Boolean;
protected
// Méthodes protégées - Gestionnaire d'Evenement
function HelpEventHandler(Command: Word; Data: THelpEventData; var CallHelp: Boolean): Boolean; virtual;
public
// Constructeurs
constructor Create();
destructor Destroy(); override;
// Méthodes publiques
function FindHTMLHelpWindowHandles(var AHandles: THandleArray): Boolean;
function GetHelpFile(): string;
// Propriétés publiques
property AutoPosition: Boolean read FAutoPosition write FAutoPosition;
public
// Singleton : Point d'Accès unique du TSLTHtmlHelpManager
class function GetInstance(): TSLTHtmlHelpManager; static;
class property Instance: TSLTHtmlHelpManager read GetInstance;
end;
TSLTHtmlHelpFormPositionner = class(TThread)
private
// Membres privés
FWorkArea: TRect;
FCurrentWindows: TSLTHtmlHelpManager.THandleArray;
FSynchronize: record
Result: Boolean;
Handles: ^TSLTHtmlHelpManager.THandleArray;
end;
// Méthodes privées
function FindHTMLHelpWindowHandles(var AHandles: TSLTHtmlHelpManager.THandleArray): Boolean;
procedure FindHTMLHelpWindowHandlesSynchronized();
protected
// Méthodes redéfinies
procedure Execute(); override;
public
// Méthodes publiques
class procedure WaitNewWindow(const AHandles: TSLTHtmlHelpManager.THandleArray);
class procedure RefreshWindow(const AHandles: TSLTHtmlHelpManager.THandleArray);
end;
implementation
uses System.StrUtils,
SLT.Common.DesignPattern,
SLT.Common.Winapi.ShellApi,
SLT.Common.Office.Word;
{ TSLTHtmlHelpManager }
//------------------------------------------------------------------------------
procedure TSLTHtmlHelpManager.AddWindowHandle(AWindowHandle: THandle);
var
L: Integer;
begin
L := Length(FWindowHandles);
SetLength(FWindowHandles, L + 1);
FWindowHandles[L] := AWindowHandle;
end;
//------------------------------------------------------------------------------
procedure TSLTHtmlHelpManager.ClearWindowHandles();
begin
SetLength(FWindowHandles, 0);
end;
//------------------------------------------------------------------------------
constructor TSLTHtmlHelpManager.Create();
begin
TSLTSingletonThreadSafe<TSLTHtmlHelpManager>.CheckInstance(Self);
inherited Create();
FApplicationEvents := TApplicationEvents.Create(nil);
FApplicationEvents.OnHelp := HelpEventHandler;
end;
//------------------------------------------------------------------------------
destructor TSLTHtmlHelpManager.Destroy();
begin
FreeAndNil(FApplicationEvents);
TSLTSingletonThreadSafe<TSLTHtmlHelpManager>.NotifyDestroy(Self);
inherited Destroy();
end;
//------------------------------------------------------------------------------
// Cette fonction sera appelée pour chaque processus trouvé par l'API EnumWindows
function FindHTMLHelpEnumWindowsCallback(hWnd: HWND; lParam: LPARAM): BOOL; stdcall; forward;
//------------------------------------------------------------------------------
function TSLTHtmlHelpManager.FindHTMLHelpWindowHandles(var AHandles: THandleArray): Boolean;
begin
ClearWindowHandles();
// J'ai essayé d'utilise IHelpSystem.Hook avec HH_GET_WIN_HANDLE sans grand résultat
// car IHelpManager.GetHandle et IHelpManager.GetHelpFile ne fournissait rien de bon !
// Méthode plus directe : EnumWindows !
// EnumWindows va appeler la fonction FindHTMLHelpEnumWindowsCallback pour chaque fenetre présente
EnumWindows(@FindHTMLHelpEnumWindowsCallback, 0);
AHandles := FWindowHandles; // Compteur de Référence !!!
Result := Length(FWindowHandles) > 0;
end;
//------------------------------------------------------------------------------
function FindHTMLHelpEnumWindowsCallback(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
begin
// To continue enumeration, the callback function must return TRUE; to stop enumeration, it must return FALSE.
Result := TRUE;
with TSLTHtmlHelpManager.Instance do
begin
if IsHelpEventWindowHandle(hWnd) then
AddWindowHandle(hWnd);
end;
end;
//------------------------------------------------------------------------------
function TSLTHtmlHelpManager.GetHelpFile(): string;
begin
if Assigned(Screen.ActiveForm) and (Screen.ActiveForm.HelpFile <> '') then
Result := Screen.ActiveForm.HelpFile
else
Result := Application.HelpFile;
end;
//------------------------------------------------------------------------------
class function TSLTHtmlHelpManager.GetInstance(): TSLTHtmlHelpManager;
begin
Result := TSLTSingletonThreadSafe<TSLTHtmlHelpManager>.Instance;
end;
//------------------------------------------------------------------------------
function TSLTHtmlHelpManager.HelpEventHandler(Command: Word; Data: THelpEventData; var CallHelp: Boolean): Boolean;
var
CurrentHelpFile: string;
WindowHandles: TSLTHtmlHelpManager.THandleArray;
begin
Result := True; // Le gestionnaire d'événements renvoie True en cas de succès
if Command in [HELP_CONTEXT, HELP_FINDER] then
begin
CurrentHelpFile := GetHelpFile();
if SameText(ExtractFileExt(CurrentHelpFile), '.doc') or SameText(ExtractFileExt(CurrentHelpFile), '.docx') then
begin
CallHelp := not OpenMicrosoftOfficeWord(CurrentHelpFile);
end
else if StartsText('http://', CurrentHelpFile) or SameText(ExtractFileExt(CurrentHelpFile), '.chm') then
begin
if FAutoPosition then
begin
// Cherche les fenêtres d'aide !
FindHTMLHelpWindowHandles(WindowHandles);
// un CHM conserve ses positions (en Base de registre, je suppose)
// un accès HTTP direct ne mémorise pas la position de la fenêtre d'aide, je force une taille et position plus conviviale !
// Seules les nouvelles fenêtres seront positionnés sous la souris !
if (Command = HELP_FINDER) or (Length(WindowHandles) = 0) then
TSLTHtmlHelpFormPositionner.WaitNewWindow(WindowHandles)
else if (Command = HELP_CONTEXT) and (Length(WindowHandles) <> 0) then
TSLTHtmlHelpFormPositionner.RefreshWindow(WindowHandles);
end;
end
else
begin
CallHelp := not TSLTShellExecuteWrapper.Execute(CurrentHelpFile);
end;
end;
end;
//------------------------------------------------------------------------------
function TSLTHtmlHelpManager.IsHelpEventWindowHandle(AWindowHandle: THandle): Boolean;
const
WINDOW_CLASS = 'HH Parent';
var
lpClassName: array[0..MAX_PATH-1] of Char;
begin
// If the function succeeds, the return value is the number of characters copied to the buffer, not including the terminating null character.
Result := (GetClassName(AWindowHandle, lpClassName, MAX_PATH) = Length(WINDOW_CLASS)) and (lpClassName = WINDOW_CLASS);
end;
//------------------------------------------------------------------------------
function TSLTHtmlHelpManager.OpenMicrosoftOfficeWord(const AFileName: string): Boolean;
begin
with TSLTWordViewer.Create() do
try
Result := ViewFileInWord(AFileName);
finally
Free();
end;
end;
{ TSLTHtmlHelpFormPositionner }
//------------------------------------------------------------------------------
procedure TSLTHtmlHelpFormPositionner.Execute();
//--------------------------------------------------------------------------
function IsNewWindow(AWindowHandle: THandle): Boolean;
var
I: Integer;
begin
for I := Low(FCurrentWindows) to High(FCurrentWindows) do
if FCurrentWindows[I] = AWindowHandle then
Exit(False);
Result := True;
end;
const
MAX_ITERATION = 10;
WAIT_ITERATION = 10;
var
Iteration, I: Integer;
MousePoint: TPoint;
WindowHandles: TSLTHtmlHelpManager.THandleArray;
WindowSize: TSize;
begin
Iteration := 1;
repeat
Sleep(WAIT_ITERATION * Iteration);
if FindHTMLHelpWindowHandles(WindowHandles) and Winapi.Windows.GetCursorPos(MousePoint) then
begin
WindowSize.Width := FWorkArea.Width div 2;
WindowSize.Height := FWorkArea.Height div 2;
Dec(MousePoint.X, WindowSize.Width div 2);
if (MousePoint.X + WindowSize.Width) > FWorkArea.Width then
MousePoint.X := WindowSize.Width;
if (MousePoint.Y + WindowSize.Height) > FWorkArea.Height then
MousePoint.Y := WindowSize.Height;
for I := Low(WindowHandles) to High(WindowHandles) do
begin
if IsNewWindow(WindowHandles[I]) then
begin
Winapi.Windows.SetWindowPos(WindowHandles[I], 0, MousePoint.X, MousePoint.Y, WindowSize.Width, WindowSize.Height, SWP_SHOWWINDOW);
Inc(MousePoint.X, 20);
Inc(MousePoint.Y, 20);
end;
end;
end;
Inc(Iteration);
until (Length(WindowHandles) <> 0) or (Iteration >= MAX_ITERATION);
end;
//------------------------------------------------------------------------------
function TSLTHtmlHelpFormPositionner.FindHTMLHelpWindowHandles(var AHandles: TSLTHtmlHelpManager.THandleArray): Boolean;
begin
FSynchronize.Handles := @AHandles;
Synchronize(FindHTMLHelpWindowHandlesSynchronized);
Result := FSynchronize.Result;
end;
//------------------------------------------------------------------------------
procedure TSLTHtmlHelpFormPositionner.FindHTMLHelpWindowHandlesSynchronized();
begin
// FindHTMLHelpWindowHandles pourrait être utilisée à la fois par :
// - le OnHelp
// - le Thread
// Il est donc nécessaire de sécuriser l'accès, le Synchronize est suffisant pour ce cas même si bien moins élégant qu'une Section Critique
FSynchronize.Result := TSLTHtmlHelpManager.Instance.FindHTMLHelpWindowHandles(FSynchronize.Handles^);
end;
//------------------------------------------------------------------------------
class procedure TSLTHtmlHelpFormPositionner.RefreshWindow(const AHandles: TSLTHtmlHelpManager.THandleArray);
var
I: Integer;
begin
for I := Low(AHandles) to High(AHandles) do
begin
Winapi.Windows.EnableWindow(AHandles[I], True); // Anti-Modale !
Winapi.Windows.SetForegroundWindow(AHandles[I]);
end;
end;
//------------------------------------------------------------------------------
class procedure TSLTHtmlHelpFormPositionner.WaitNewWindow(const AHandles: TSLTHtmlHelpManager.THandleArray);
begin
with TSLTHtmlHelpFormPositionner.Create(True) do
begin
FreeOnTerminate := True;
FWorkArea := Screen.WorkAreaRect;
FCurrentWindows := AHandles; // Compteur de Référence !!!
Start();
end;
end;
end. |
Partager