| [541] | 1 | //*********************************************************** | 
|---|
|  | 2 | //                       TEWBFocusControl unit              * | 
|---|
|  | 3 | //                                                          * | 
|---|
|  | 4 | //                       For Delphi 5 to 2009               * | 
|---|
|  | 5 | //                     Freeware Component                   * | 
|---|
|  | 6 | //                            by                            * | 
|---|
|  | 7 | //                          (smot)                          * | 
|---|
|  | 8 | //                                                          * | 
|---|
|  | 9 | //  Documentation and updated versions:                     * | 
|---|
|  | 10 | //                                                          * | 
|---|
|  | 11 | //               http://www.bsalsa.com                      * | 
|---|
|  | 12 | //*********************************************************** | 
|---|
|  | 13 | {*******************************************************************************} | 
|---|
|  | 14 | {LICENSE: | 
|---|
|  | 15 | THIS SOFTWARE IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTY OF ANY KIND, | 
|---|
|  | 16 | EITHER EXPRESSED OR IMPLIED INCLUDING BUT NOT LIMITED TO THE APPLIED | 
|---|
|  | 17 | WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | 
|---|
|  | 18 | YOU ASSUME THE ENTIRE RISK AS TO THE ACCURACY AND THE USE OF THE SOFTWARE | 
|---|
|  | 19 | AND ALL OTHER RISK ARISING OUT OF THE USE OR PERFORMANCE OF THIS SOFTWARE | 
|---|
|  | 20 | AND DOCUMENTATION. BSALSA PRODUCTIONS DOES NOT WARRANT THAT THE SOFTWARE IS ERROR-FREE | 
|---|
|  | 21 | OR WILL OPERATE WITHOUT INTERRUPTION. THE SOFTWARE IS NOT DESIGNED, INTENDED | 
|---|
|  | 22 | OR LICENSED FOR USE IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE CONTROLS, | 
|---|
|  | 23 | INCLUDING WITHOUT LIMITATION, THE DESIGN, CONSTRUCTION, MAINTENANCE OR | 
|---|
|  | 24 | OPERATION OF NUCLEAR FACILITIES, AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, | 
|---|
|  | 25 | AIR TRAFFIC CONTROL, AND LIFE SUPPORT OR WEAPONS SYSTEMS. BSALSA PRODUCTIONS SPECIFICALLY | 
|---|
|  | 26 | DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR SUCH PURPOSE. | 
|---|
|  | 27 |  | 
|---|
|  | 28 | You may use, change or modify the component under 4 conditions: | 
|---|
|  | 29 | 1. In your website, add a link to "http://www.bsalsa.com" | 
|---|
|  | 30 | 2. In your application, add credits to "Embedded Web Browser" | 
|---|
|  | 31 | 3. Mail me  (bsalsa@gmail.com) any code change in the unit | 
|---|
|  | 32 | for the benefit of the other users. | 
|---|
|  | 33 | 4. Please, consider donation in our web site! | 
|---|
|  | 34 | {*******************************************************************************} | 
|---|
|  | 35 |  | 
|---|
|  | 36 | unit EwbFocusControl; | 
|---|
|  | 37 |  | 
|---|
|  | 38 | {$I EWB.inc} | 
|---|
|  | 39 |  | 
|---|
|  | 40 | interface | 
|---|
|  | 41 |  | 
|---|
|  | 42 | uses | 
|---|
|  | 43 | Windows, Messages, Classes, Forms, Controls; | 
|---|
|  | 44 |  | 
|---|
|  | 45 | // -- TEWBFocusControl --------------------------------------------------------- | 
|---|
|  | 46 |  | 
|---|
|  | 47 | type | 
|---|
|  | 48 | TEWBFocusControl = class { Singleton } | 
|---|
|  | 49 | private | 
|---|
|  | 50 | constructor Create; | 
|---|
|  | 51 | public | 
|---|
|  | 52 | class procedure Activate(Value: Boolean); | 
|---|
|  | 53 | end; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | // Enable TEWBApplicationHook, if accessible at runtime only | 
|---|
|  | 56 | // Enable_HookMainWindow must be defined in EWB.inc | 
|---|
|  | 57 | // EWBHookMainWindow is activated by default. | 
|---|
|  | 58 |  | 
|---|
|  | 59 | var | 
|---|
|  | 60 | EWBEnableFocusControl: Boolean = True; // DO NOT CHANGE HERE | 
|---|
|  | 61 |  | 
|---|
|  | 62 | implementation | 
|---|
|  | 63 |  | 
|---|
|  | 64 | uses | 
|---|
|  | 65 | EwbCoreTools; | 
|---|
|  | 66 |  | 
|---|
|  | 67 | // -- FreeAndNil (SysUtils) ---------------------------------------------------- | 
|---|
|  | 68 |  | 
|---|
|  | 69 | procedure FreeAndNil(var Obj); | 
|---|
|  | 70 | var | 
|---|
|  | 71 | Temp: TObject; | 
|---|
|  | 72 | begin | 
|---|
|  | 73 | try | 
|---|
|  | 74 | Temp := TObject(Obj); | 
|---|
|  | 75 | finally | 
|---|
|  | 76 | Pointer(Obj) := nil; | 
|---|
|  | 77 | end; | 
|---|
|  | 78 | Temp.Free; | 
|---|
|  | 79 | end; | 
|---|
|  | 80 |  | 
|---|
|  | 81 | // -- TAppHookWindow ---------------------------------------------------------- | 
|---|
|  | 82 |  | 
|---|
|  | 83 | type | 
|---|
|  | 84 | TAppHookWindow = class(TWinControl) | 
|---|
|  | 85 | private | 
|---|
|  | 86 | FHookSet: Boolean; | 
|---|
|  | 87 | function MessageHook(var Msg: TMessage): Boolean; | 
|---|
|  | 88 | public | 
|---|
|  | 89 | destructor Destroy; override; | 
|---|
|  | 90 | procedure Activate; | 
|---|
|  | 91 | procedure Deactivate; | 
|---|
|  | 92 | end; | 
|---|
|  | 93 |  | 
|---|
|  | 94 | var | 
|---|
|  | 95 | EWBAppHookInstance: TAppHookWindow; | 
|---|
|  | 96 |  | 
|---|
|  | 97 | // -- TEWBFocusControl --------------------------------------------------------- | 
|---|
|  | 98 |  | 
|---|
|  | 99 | constructor TEWBFocusControl.Create; | 
|---|
|  | 100 | begin | 
|---|
|  | 101 | inherited; | 
|---|
|  | 102 | if EWBEnableFocusControl then | 
|---|
|  | 103 | begin | 
|---|
|  | 104 | if EWBAppHookInstance = nil then | 
|---|
|  | 105 | begin | 
|---|
|  | 106 | EWBAppHookInstance := TAppHookWindow.Create(nil); | 
|---|
|  | 107 | EWBAppHookInstance.Activate; | 
|---|
|  | 108 | end; | 
|---|
|  | 109 | end; | 
|---|
|  | 110 | end; | 
|---|
|  | 111 |  | 
|---|
|  | 112 | class procedure TEWBFocusControl.Activate(Value: Boolean) | 
|---|
|  | 113 | {: TEWBApplicationHook}; | 
|---|
|  | 114 | const | 
|---|
|  | 115 | {$J+} | 
|---|
|  | 116 | Instance: TEWBFocusControl = nil; | 
|---|
|  | 117 | {$J-} | 
|---|
|  | 118 | begin | 
|---|
|  | 119 | if EWBEnableFocusControl then | 
|---|
|  | 120 | case Value of | 
|---|
|  | 121 | True: | 
|---|
|  | 122 | begin | 
|---|
|  | 123 | if not Assigned(Instance) then | 
|---|
|  | 124 | Instance := Create; | 
|---|
|  | 125 | end; | 
|---|
|  | 126 | False: | 
|---|
|  | 127 | begin | 
|---|
|  | 128 | if Assigned(EWBAppHookInstance) then | 
|---|
|  | 129 | FreeAndNil(EWBAppHookInstance); | 
|---|
|  | 130 | if Assigned(Instance) then | 
|---|
|  | 131 | FreeAndNil(Instance); | 
|---|
|  | 132 | end; | 
|---|
|  | 133 | end; | 
|---|
|  | 134 | end; | 
|---|
|  | 135 |  | 
|---|
|  | 136 | // -- TAppHookWindow ----------------------------------------------------------- | 
|---|
|  | 137 |  | 
|---|
|  | 138 | function TAppHookWindow.MessageHook(var Msg: TMessage): Boolean; | 
|---|
|  | 139 | var | 
|---|
|  | 140 | ActiveControl: TWinControl; | 
|---|
|  | 141 | ActiveForm: TCustomForm; | 
|---|
|  | 142 | bContinue: Boolean; | 
|---|
|  | 143 | //  s: string; | 
|---|
|  | 144 | begin | 
|---|
|  | 145 | Result := False; | 
|---|
|  | 146 | if (Msg.Msg = WM_WINDOWPOSCHANGING) or (Msg.Msg = CM_ACTIVATE) then | 
|---|
|  | 147 | begin | 
|---|
|  | 148 | ActiveForm := Screen.ActiveForm; | 
|---|
|  | 149 | if Assigned(ActiveForm) then | 
|---|
|  | 150 | begin | 
|---|
|  | 151 | if Screen.ActiveForm.FormStyle = fsMDIChild then // Check if MDI | 
|---|
|  | 152 | bContinue := IsChild(GetActiveWindow, ActiveForm.Handle) | 
|---|
|  | 153 | else | 
|---|
|  | 154 | bContinue :=  not Forms.Application.Terminated and (ActiveForm.Handle = GetActiveWindow); | 
|---|
|  | 155 |  | 
|---|
|  | 156 | if bContinue and (ActiveForm.CanFocus) then | 
|---|
|  | 157 | begin | 
|---|
|  | 158 | ActiveControl := ActiveForm.ActiveControl; | 
|---|
|  | 159 | // s := '** MessageHook ' + ActiveControl.ClassName + ' ' + Inttostr(ActiveForm.Handle) + ' ' +  Inttostr(GetFocus); | 
|---|
|  | 160 | //  OutputDebugString(PChar(s)); | 
|---|
|  | 161 | if Assigned(ActiveControl) and ((ActiveControl.ClassName = 'TEmbeddedWB') or | 
|---|
|  | 162 | (ActiveControl.ClassName = 'TEWBCore')) then | 
|---|
|  | 163 | if GetFocus <> ActiveControl.Handle then | 
|---|
|  | 164 | begin | 
|---|
|  | 165 | PostMessage(ActiveControl.Handle, WM_SETWBFOCUS, Integer(ActiveControl), 0); | 
|---|
|  | 166 | //  OutputDebugString(PChar('Focus set')); | 
|---|
|  | 167 | //  ActiveControl.SetFocus doesn't work when switching between forms. | 
|---|
|  | 168 | end; | 
|---|
|  | 169 | end; | 
|---|
|  | 170 | end; | 
|---|
|  | 171 | end; | 
|---|
|  | 172 | end; | 
|---|
|  | 173 |  | 
|---|
|  | 174 | destructor TAppHookWindow.Destroy; | 
|---|
|  | 175 | begin | 
|---|
|  | 176 | inherited; | 
|---|
|  | 177 | Deactivate; | 
|---|
|  | 178 | end; | 
|---|
|  | 179 |  | 
|---|
|  | 180 | procedure TAppHookWindow.Activate; | 
|---|
|  | 181 | begin | 
|---|
|  | 182 | if (not FHookSet) and EWBEnableFocusControl then | 
|---|
|  | 183 | if Assigned(Application) then | 
|---|
|  | 184 | begin | 
|---|
|  | 185 | Application.HookMainWindow(MessageHook); | 
|---|
|  | 186 | FHookSet := True; | 
|---|
|  | 187 | end; | 
|---|
|  | 188 | end; | 
|---|
|  | 189 |  | 
|---|
|  | 190 | procedure TAppHookWindow.Deactivate; | 
|---|
|  | 191 | begin | 
|---|
|  | 192 | if Assigned(Application) then | 
|---|
|  | 193 | begin | 
|---|
|  | 194 | Application.UnHookMainWindow(MessageHook); | 
|---|
|  | 195 | FHookSet := False; | 
|---|
|  | 196 | end; | 
|---|
|  | 197 | end; | 
|---|
|  | 198 |  | 
|---|
|  | 199 | end. | 
|---|