BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Here's one solution to the missing keystrokes in an Embedded

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation
View previous topic :: View next topic  
Author Message
Sean
Guest





PostPosted: Wed Dec 17, 2003 2:16 pm    Post subject: Here's one solution to the missing keystrokes in an Embedded Reply with quote



Many thanks to Martin Larsson for providing the guts to this solution,
as found in his posting:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=MPG.133a2222959bc089896bc%40forums.inprise.com&rnum=2

This is a very reliable workaround for the issue where an embedded
ActiveForm does not respond to various keystrokes (Tab, Arrow Keys,
etc.). It also addresses the issue of trying to make an ActiveForm
visible with late binding. Essentially the OleContainer object has
been inherited and augmented to provide the new keystroke information
to the activeform within it.

To use this method, observe the following sample code:

----------------------

....

uses
FixOleContainers

....

procedure TForm1.Button1Click(Sender: TObject);
var
EmployeeContainer: TNewOleContainer;
begin
EmployeeContainer := TNewOleContainer.EmbedActiveForm(TabSheet1,
'Employee.EmployeeX'); // instantiates the ActiveForm in a TabSheet
EmployeeContainer.OleObject.MyActiveFormProperty := True; //example
of working with the ActiveForm
end;

----------------------

And here follows the unit containing the code for the
TNewOleContainer. Just cut and paste into a fresh unit, save as
FixOleContainers.pas, and then you can use this in your application as
demonstrated above.

----------------------

unit FixOleContainers;

interface

uses
Windows, Controls, Messages, Forms, OleCtnrs;

type
IFixOleContainers = interface
end;

TNewOleContainer = class(TOleContainer)
private
fFixOleContainer: IFixOleContainers;
public
class function EmbedActiveForm(AParent: TWinControl;
AOleClassName: string): TNewOleContainer;
end;

TFixOleContainers = class(TInterfacedObject, IFixOleContainers)
public
hFixOleContainersHook: hHook;
constructor Create;
destructor Destroy; override;
class function EmbedActiveForm(AParent: TWinControl;
AOleClassName: string): TNewOleContainer;
end;

function GetMessageProc(nCode: Integer; wp: WParam; lp: LParam)
:LResult; stdcall;

var
gFixOleContainers: TFixOleContainers;

implementation

constructor TFixOleContainers.Create;
begin
hFixOleContainersHook := SetWindowsHookEx(WH_GETMESSAGE,
GetMessageProc,
HInstance,
GetCurrentThreadID);
end;

destructor TFixOleContainers.Destroy;
begin
UnHookWindowsHookEx(hFixOleContainersHook);
gFixOleContainers := nil;
inherited;
end;

function GetMessageProc(nCode: Integer; wp: WParam; lp: LParam)
:LResult;
var
msg : TMsg;
begin
msg := PMsg(lp)^;
if (msg.message >= WM_KEYFIRST) and (msg.message <= WM_KEYLAST) then
begin
if Screen.ActiveControl is TOleContainer then begin
TranslateMessage(Msg);
DispatchMessage(Msg);
PMsg(lp)^.message := WM_NULL;
PMsg(lp)^.lParam := 0;
PMsg(lp)^.wParam := 0;
end;
end;
Result := CallNextHookEx(gFixOleContainers.hFixOleContainersHook,
nCode, wp, lp);
end;

class function TFixOleContainers.EmbedActiveForm(AParent: TWinControl;
AOleClassName: string): TNewOleContainer;
begin
if not assigned(gFixOleContainers) then gFixOleContainers := Create;
result := TNewOleContainer.Create(AParent);
with result do begin
Parent := AParent;
Align := alClient;
BorderStyle := bsNone;
AutoActivate := aaGetFocus;
CreateObject(AOleClassName, false);
fFixOleContainer := gFixOleContainers;
end;
end;

class function TNewOleContainer.EmbedActiveForm(AParent: TWinControl;
AOleClassName: string): TNewOleContainer;
begin
result := TFixOleContainers.EmbedActiveForm(AParent, AOleClassName);
result.SetFocus;
keybd_event(VK_TAB, 0, 0, 0);
Application.ProcessMessages;
end;

initialization

finalization
if assigned(gFixOleContainers) then
UnHookWindowsHookEx(gFixOleContainers.hFixOleContainersHook);

end.

--------------------------

Sean
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.