?
Solved

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

Posted on 2003-12-02
18
Medium Priority
?
1,241 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
17 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 9861600

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
ID: 9864426
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
ID: 9864437
Use the OnNewWindow2 event. If you want to block all new windows, always set Cancel := True.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:fsurfer
ID: 9865161
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
ID: 9867058
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
ID: 9867075
... 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
ID: 9867146

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
ID: 9867325
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 9867371

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
ID: 9867411
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
ID: 9868951
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
ID: 9869306
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
ID: 9874850
I understand now :)
Good luck !
0
 

Author Comment

by:fsurfer
ID: 9881216
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
ID: 9882385
No problem here.

Regards,
Russell
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 9884547
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-
ID: 12382239
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

750 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