CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2000
    Location
    Arizona, USA
    Posts
    493

    Adding Items To A Menu

    I want to add an item to an IE menu. I have figured out how to add items to the toolbar,
    the Tools menu, and the default right click menu. But I need to add one to the menu that
    comes up when you highlight text on the web page and right click. When you do this it comes up
    with Copy, Cut, Paste etc. I know it can be done because I have a program installed that has done
    this (Copernic) Can anyone tell me how to do this?
    Thanks for any help!!!!!!!!

    Kris
    Software Engineer
    Phoenix,AZ
    Kris
    Software Engineer
    Phoenix, AZ USA

  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Adding Items To A Menu

    Try this

    Dim gLastElement As Integer

    Private Sub Form_Load()
    '---------------------------
    gLastElement = 0
    mnuDel.Enabled = False
    End Sub

    Private Sub mnuAdd_Click()
    '---------------------------
    mnuItems(0).Visible = True
    If gLastElement = 15 Then
    Exit Sub
    End If
    gLastElement = gLastElement + 1
    Load mnuItems(gLastElement)
    mnuItems(gLastElement).Caption = "Troop" + Str(gLastElement)
    mnuDel.Enabled = True
    End Sub

    Private Sub mnuDel_Click()
    '---------------------------
    mnuAdd.Enabled = True
    Unload mnuItems(gLastElement)
    gLastElement = gLastElement - 1
    If gLastElement = 0 Then mnuDel.Enabled = False: mnuItems(0).Visible = False
    End Sub

    Private Sub mnuGrow_Click()
    '---------------------------
    If gLastElement = 15 Then mnuAdd.Enabled = False
    End Sub



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Mar 2000
    Location
    Arizona, USA
    Posts
    493

    Re: Adding Items To A Menu

    Thanks for the help! But I need to add an item to the menu that comes up when you higlight text
    in Internet Explorer.
    I think I am not going to do it this way now. I have registered a hotkey for my app which works
    very well. I do need to figure out something else. My app runs if IE is open and the F2 key
    is pressed. What I need to do is copy the text that is higlighted on the web page into a textbox
    on my form. Any idea on how to do that?

    Kris
    Software Engineer
    Phoenix,AZ
    Kris
    Software Engineer
    Phoenix, AZ USA

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Adding Items To A Menu

    Can you copy the highlighted text to the clipboard and then paste from clipboard to the text box

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    Mar 2000
    Location
    Arizona, USA
    Posts
    493

    Re: Adding Items To A Menu

    That is exactly what I am trying to do. I have tried using "^C" with the SendKeys function
    but that doesn't work. I think I need to use the SendMessage API but I haven't figured out how.
    I know the hWnd of the IE Window and it is the active foreground window at the time.

    Kris
    Software Engineer
    Phoenix,AZ
    Kris
    Software Engineer
    Phoenix, AZ USA

  6. #6
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Adding Items To A Menu

    You have to find the handle of the text box and sendmessage to set focus on this text box. After that you can copy the content of the text box to the clipboard


    Here is an example which might help you to figure out how to deal with SendMessage API


    When you associate your application with data files, often you don't need to open a new a instance of
    application when a file is doubleclicked. Instead, you just want to send a command string (file name) to the
    previous instance. This application demonstrates a technique that allows you to do this without subclassing,
    using an invisible command button. The same technique can be useful for sending messages between two
    applications.

    Place invisible button on form. When you need send some message to this app, just find hwnd of this
    button by its caption, change caption through API and call its click event through SendMessage. Then
    in Click event read caption, store this string or make some staff and change caption back to 'searchable'
    caption.

    To see this work, run the included.exe file multiple times, with a command string passed to it.




    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

    Const SW_RESTORE = 9
    Const WM_SETTEXT = &HC
    Const BM_CLICK = &HF5 ' Top MS secret? Not included into API32.txt

    'Code

    Private Sub ProcessCommandLine(sCommand As String)
    'Your code here
    ' MsgBox "Excuted with command: " & sCommand
    If Text1.Text <> "" Then Text1.Text = Text1.Text & vbCrLf
    Text1 = Text1 & "Excuted with command: " & sCommand
    End Sub

    Private Sub cmdDummy_Click()
    ProcessCommandLine cmdDummy.Caption
    cmdDummy.Caption = "Ready for command"
    End Sub

    Private Sub Form_Load()
    Dim h As Long, hButton As Long
    Dim sCaption As String, sCommand As String
    cmdDummy.Visible = False
    sCaption = "Your Caption"
    Text1 = ""
    sCommand = "iouri" 'Command
    If App.PrevInstance Then
    h = FindWindow(vbNullString, ByVal sCaption)
    ActivateWindow h
    hButton = FindWindowEx(h, 0, vbNullString, ByVal "Ready for command")
    SendMessage hButton, WM_SETTEXT, 0, ByVal sCommand
    SendMessage hButton, BM_CLICK, 0, ByVal 0&
    Unload Me
    Exit Sub
    Else
    Caption = sCaption
    cmdDummy.Caption = sCommand
    cmdDummy_Click
    End If
    End Sub

    Private Sub ActivateWindow(h As Long)
    If h Then
    If IsIconic(h) Then
    Call ShowWindow(h, SW_RESTORE)
    End If
    Call SetForegroundWindow(h)
    Else
    Exit Sub 'no need to continue.
    End If
    End Sub



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  7. #7
    Join Date
    Mar 2000
    Location
    Arizona, USA
    Posts
    493

    Re: Adding Items To A Menu

    Unfortunately this is just text on a web page that I am trying to copy. I have figured out
    another way to accomplish this. In Internet Explorer you can add menu items to the default
    Right Click menu and have IE execute a web page containing JavaScript. I have figured out how
    to get the higlighted text on the parent page using JavaScript and then open my app using
    Windows Scripting Host. I pass the highlighted text as a command line parameter. Now I only
    have one problem. When you highlight text on a web page and right click a different menu comes
    up. It has selections like Cut, Copy, Paste etc. I need to add a selection to this menu.
    I know it can be done because I use Copernic, which is a web search engine program, and when it
    was installed it added a menu selection here too. Any ideas on how to add a selection to this
    menu? That is all I need to do to get this to work. Thanks for any help you can provide!!!!!!


    Kris
    Software Engineer
    Phoenix,AZ
    Kris
    Software Engineer
    Phoenix, AZ USA

  8. #8
    Join Date
    Mar 2000
    Location
    Arizona, USA
    Posts
    493

    Re: Adding Items To A Menu

    Hey I figured out to add the menu item I was talking about in my previous post.
    Here is the registry hack to do it. The key name is what shows up in the menu. In this case it is
    IE Network Utility's. Just change the name of the key to whatever you want. And then change the path to point to a web page that runs some
    kind of script.

    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\IE Network Utility's]
    @="C:\\winnt\\web\\RunIENetUtil.htm"
    "Flags"=hex:00,00,00,00

    Here is the JavaScript I use:

    <SCRIPT LANGUAGE="JavaScript">
    var parentWin = external.menuArguments;
    if (parentWin)
    {
    var el = parentWin.event.srcElement;
    var tag = el.tagName;
    var doctxt = parentWin.document.body.createTextRange();
    var searchtext = "";

    switch (tag) {
    case "A":
    searchtext = el.innerText;
    break;

    case "IMG":
    searchtext = el.alt;
    break;

    case "INPUT":
    case "TEXTAREA":
    if (el.type == "image")
    searchtext = el.alt
    else if (el.type != "radio") {
    searchtext = parentWin.document.selection.createRange().text;
    if (searchtext == "")
    searchtext = el.createTextRange().text;
    }
    break;
    }

    if (searchtext == "") {
    searchtext = parentWin.document.selection.createRange().text;
    if (searchtext == "") {
    doctxt.moveToPoint(parentWin.event.clientX, parentWin.event.clientY);
    doctxt.moveStart("word", 1);
    doctxt.moveStart("word", -1);
    doctxt.moveEnd("word", -1);
    doctxt.moveEnd("word", 1);
    searchtext = doctxt.text;
    }
    }

    }
    var objWshShell = new ActiveXObject("WScript.Shell");
    objWshShell.Run ("C:\IENetUtil.exe searchtext");
    {
    }
    </SCRIPT>

    Kris
    Software Engineer
    Phoenix,AZ
    Kris
    Software Engineer
    Phoenix, AZ USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured