Solved

[HELP]problem with TWebBrowser and keyboard shortcut/popup menu

Posted on 2003-12-02
18
1,171 Views
Last Modified: 2012-06-21
Hi everyone,

i have a major problem with the TWebBrowser component.

i need to create my own Webbrowser in order to prevent anyone to enter a http address. i'm using the TWebbrowser (Delphi 7, BTW). Everything seems fine except that using the keyboard Shortcut Ctrl+N, i can open a completely new IE window with the address bar.
Any one has a solution to prevent those shortcut to be enabled ?

and also, if anyone has a solution to prevent the popup menu from right click ?

as this is quite urgent (but i don't know about the difficulty), i'll put the recommended 250 points for that question.
and if possible a sample would be helpful !

thanks in advance for any help !

Guillaume
0
Comment
Question by:fsurfer
  • 6
  • 5
  • 4
  • +2
18 Comments
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

Not sure if there is a better way to hook the web browser control, but this is one way of doing it.

1.) Drop a TApplicationEvents component on your form, or create a method to handle the Application.OnMessage yourself (take your pick)

2.) Add the following code to handle the messages at the application level, before they reach the webbrowser control.

// This was coded using the TApplicationEvents.OnMessage method
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var  lpClass:    Array [0..1024] of Char;
begin

  // Attempt to get the class name
  if (GetClassName(Msg.hwnd, lpClass, 1024) > 0) then
  begin
     // Check the class against known IE class name
     if (StrIComp(@lpClass, 'Internet Explorer_Server') = 0) then
     begin
        // Message directed to web browse control
        case Msg.message of
           // Eat the message if CTRL-N
           WM_KEYDOWN        :  Handled:=(Msg.wParam = Ord('N')) and (GetKeyState(VK_CONTROL) shr 15 = 1);
           // Eat the right mouse button
           WM_RBUTTONDOWN    :  Handled:=True;
           WM_RBUTTONDBLCLK  :  Handled:=True;
        else
           // Let IE handle it
           Handled:=False;
        end;
     end
     else
        // Not handling the message here
        Handled:=False;
  end
  else
     // Not handling the message here
     Handled:=False;

end;

---

The only caveat I found to doing it this way is that the Cut/Copy/Paste menu for the psuedo "edit" controls in the web page never gets displayed (because the code eats the right mouse button events). I did try catching the WM_INITMENUPOPUP, but the code never fired, most likely because its going to IE's message queue, and not your application. But, it does keep IE's menu from displaying....

Hope this helps some,
Russell


0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
For the right click problem use:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls, SHDocVw_TLB;


type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
procedure MessageLoop(var Msg:TMsg;var Handled:Boolean);
procedure FormCreate(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
procedure TForm1.MessageLoop(var Msg: TMsg; var Handled: Boolean);
begin
if (Msg.Message = WM_RBUTTONDOWN) or (Msg.Message = WM_RBUTTONUP) then
handled:=True
else Handled := False
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := MessageLoop;
end;
end.
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
Use the OnNewWindow2 event. If you want to block all new windows, always set Cancel := True.
0
 

Author Comment

by:fsurfer
Comment Utility
hi,

thanx for your both answers, but i've found myself the solution, found at this page : http://www.euromind.com/iedelphi/embeddedwb.htm . It comes with examples about preventing shortcut keys, popup menu, ...

the advantage of this component is that you can control the component more accurately and it's free :)

if you agree, i'll refund and close the question.

best regards,

Guillaume
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
I use this component ( EmbeddedWB ) and I'm not sure that you can do all these jobs without using my code :)

My comments are exactly what you ask for ( the codes are snaped from my browser that use the EmbeddedWB component ) .
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
... and if you will use the EmbeddedWB will need additional codes to use TAB and ENTER keys in web pages, to modify before use XP themes ... Trust me, I'm an experts :P on this component.
But as a general conclusion ... is a great component !
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

Nick,

My comments are also exactly what was asked for... ;-)

Your code for menu blocking is very close to what I already suggested first, but I opted to block the right mouse down for just the web browser window, and not all child windows. I also found that a right mouse dblclick could be used to get the menu to appear, thus the reason for the WM_RBUTTONDBLCLK check.

In regards to the new window blocking, I opted for catching/tossing the CTRL-N key combination, because I found that the OnNewWindow2(...) event was NOT getting fired (at least on my system).

Best Regards,
Russell
0
 

Author Comment

by:fsurfer
Comment Utility
hi both,

First, ginsonic, with this control, i've been able to prevent just the keyboard shortcuts involving the Ctrl key. All others are allowed (especially TAB or ENTER). Now i may not know all the keyboard shortcuts used in IE :)

(tip : in the OnTranslateAccelerator event, put this code, and everything goes fine :
function TNewNav.EmbeddedWB1TranslateAccelerator(const lpMsg: PMsg;
  const pguidCmdGroup: PGUID; const nCmdID: Cardinal): HRESULT;
begin
     if (getkeystate(VK_CONTROL) < 0) then
        result := S_OK
     else
         result := S_FALSE;
end;

Ok, i know that this code prevent copy/paste... but... :)
)

Both of you gave a correct answer to the question. As i've found my self the solution - unfortunately for me - before i read your answers, i read the FAQ & Help of EE site and found something "when i answer my self a question".

But i will consider the fact that you have both answered correctly the question.
So i will split the points between you two, if you find it convenient ?

best regards,

Guillaume
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

As far as I'm concerned, keep the points if you found the answer yourself (I'm assuming you found another way to circumvent the popup menu as well?).

Regards,
Russell
0
 

Author Comment

by:fsurfer
Comment Utility
Russel,

True. With this component, you have an event OnShowContextMenu.
To prevent it to open, just one line of code :

function TNewNav.EmbeddedWB1ShowContextMenu(const dwID: Cardinal;
  const ppt: PPoint; const pcmdtReserved: IInterface;
  const pdispReserved: IDispatch): HRESULT;
begin
     result := S_OK;
end;

for the question points, you'll have them also... just waiting for ginsonic answer.

best regards,

Guillaume
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
For me the points aren't important, too. I have more than 100.000 points ;P

I wished to say that you must add couple features (codes) to can use it . Go to Google and input in search box couple words. After that press Enter key. How you will see this action don't have any result.

In same time try to visit a webpage that use XP style. Compare IE with your browser.

Nick

P.S. Whay you wish to block a new browser session ? When you try to create a new browser windows you can force the application to create a new own browser windows and not IE. Couple lines to do that.
0
 

Author Comment

by:fsurfer
Comment Utility
hi Nick,

i'm working in a cybercafe. i have an application to manage the cybercafe and secure the computers. But i want  for some special cases the user to be able to see some special websites without being able to surf on any other website.

So, the thing is quite simple : i want to create a browser that cannot browse on any other website than the one i have selected by a mean of mine. if i can't disable the keyboard shortcut, i can't prevent a user from opening a new browser with Ctrl+N. if i can't prevent the popup menu to open, i can't prevent the user from opening a new browser also. That's why i wanted to block the shortcut and menu.

just using the EmbeddedWB (meaning with no address bar nor other toolbar) isn't enough for me. i want to have my own browser where i disable almost everything except to see the content of a selected website.

i don't care it doesn't look like IE. i just want a window with a frame where i can display almost every html pages ;)

thanks for your interest :)

as you wish, guys, for the points.

best regards,

Guillaume
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
I understand now :)
Good luck !
0
 

Author Comment

by:fsurfer
Comment Utility
Thanks again for your answers guys !

such help is very much appreciated, and it's always a pleasure to learn workarounds or other way to do things ! :)

so if you don't mind, i'll refund and close the question :)

(i have another question in my bag ;)

best regards,

Guillaume
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
No problem here.

Regards,
Russell
0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
PAQ-ing this question and refunding the 250 points by CS request.

modulo

Community Support Moderator
Experts Exchange
0
 
LVL 2

Expert Comment

by:-Karamja-
Comment Utility
Topic handled but not PAQ?
I will leave a message in CleanUp to get this topic PAQ-ing,
Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Karamja (Alan)
EE Cleanup Volunteer
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now