Advanced? Webbrowser ContextMenu
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
Well this is a long reading tutorial for eyes on how to make maybe advanced webbrowser contextmenu.
What I will be showing you in this tutorial is, how to get various properties of HtmlElements based on the mouse position and how to change the various properties of HtmlElements.
In this purpose we are going to use the normal webbrowser control or what ever you call it, but I am aware people are maybe using some class they made for adding everything like tab's and some controls, but it shouldn't be really difficult to add some codes to the classes but on we go.
What controls are needed:
*Webbrowser called wb(or what you called it, I just call it wb cause its easy to remember)
*ContextMenu/Strip named mMenu(once again or something you like)
Items in the ContextMenu/Strip:
*Open Link(named mnuOpen)
*Open Link Location(named mnuLinkLoc)
*Add a separator(by writing "-")
*Save Image As...(mnuSaveImg)
*Copy Image Location(mnuCopyImgLoc)
*Open Image(mnuOpenImg)
Now you have probable made the "IsWebbrowserContextMenuEnabled" property to false, but if not do it now.
Moving on, if your using ContextMenuStrip you can easily add it through the property called "ContextMenuStrip", but if you are using a ContextMenu you'll have to go to Form_Load and put this simple code in it:
As you might know, the webbrowser control doesn't support MouseMove or Down property event, so we have to do it by codes, so we have to declare a HtmlDocument. Get to the code view if you haven't and write this or copy:
Next go to mMenu.Opening or mMenu.Popup(if your using contextmenu) and add this code:
Now as you might know all links in HTML are declared like this:
Now debug your browser and test it out, you'll notice that there is a different item for links and for images on our contextmenu/strip. But they are not functioned. So stop debugging and start coding again.
Moving on to the links.
On the mnuOpen click event add this code:
And on the mnuCopyLink click event add this code:
Moving on to the images.
On the mnuSaveImg click event add this code:
Now when this is all done, we are nearly finished, but there are few left.
Next go to the mnuCopyImgLoc click event and add this code:
Last and not least, open up the mnuOpenImg click event and add this code:
Now we're finished!
Thanks for reading my tutorial, I hope you like it cooll;
What I will be showing you in this tutorial is, how to get various properties of HtmlElements based on the mouse position and how to change the various properties of HtmlElements.
In this purpose we are going to use the normal webbrowser control or what ever you call it, but I am aware people are maybe using some class they made for adding everything like tab's and some controls, but it shouldn't be really difficult to add some codes to the classes but on we go.
What controls are needed:
*Webbrowser called wb(or what you called it, I just call it wb cause its easy to remember)
*ContextMenu/Strip named mMenu(once again or something you like)
Items in the ContextMenu/Strip:
*Open Link(named mnuOpen)
*Open Link Location(named mnuLinkLoc)
*Add a separator(by writing "-")
*Save Image As...(mnuSaveImg)
*Copy Image Location(mnuCopyImgLoc)
*Open Image(mnuOpenImg)
Now you have probable made the "IsWebbrowserContextMenuEnabled" property to false, but if not do it now.
Moving on, if your using ContextMenuStrip you can easily add it through the property called "ContextMenuStrip", but if you are using a ContextMenu you'll have to go to Form_Load and put this simple code in it:
Code: Select all
Moving on to the coding:wb.ContextMenu = ContextMenu1()
As you might know, the webbrowser control doesn't support MouseMove or Down property event, so we have to do it by codes, so we have to declare a HtmlDocument. Get to the code view if you haven't and write this or copy:
Code: Select all
Now when you are done with this, go to the WebBrowser_Navigated event and add this code:
Private WithEvents CurDoc as HtmlDocument
Dim P as Point
Dim hEle as HtmlElemtn
Code: Select all
And then go to the CurDoc.MouseMove event and add this code:
CurDoc = wb.Document
Code: Select all
What this code will do is telling our program to record where the mouse is on the webpage so it knows which element is being hovered.P = new Point(MousePosition.X, MousePosition.Y)
Next go to mMenu.Opening or mMenu.Popup(if your using contextmenu) and add this code:
Code: Select all
What this will do is is that where hEle and P come in action and makes hEle(the selected HtmlElement, lets say an image, link or maybe a button) what ever HtmlElement the cursor is over on the webbrowser, now because we made a P(as mouse Position) where the cursor was on the webpage when it was moved.hEle = CurDoc.GetElementFromPoint(P)
If hEle.TagName = "A" Then
mnuOpen.Visible = false
mnuLinkLoc.Visible = false
else
mnuOpen.Visible = true
mnuLinkLoc.Visible = true
End If
hEle = CurDoc.GetElementFromPoint(P)
If hEle.TagName = "A" Then
mnuSaveImg.Visible = True
mnuCopyImgLoc.Visible = True
mnuOpenImg.Visible = true
Else
mnuSaveImg.Visible = False
mnuCopyImgLoc.Visible = False
mnuOpenImg.Visible = False
End If
If hEle.TagName = "IMG" Then
mnuSaveImg.Visible = True
mnuCopyImgLoc.Visible = True
mnuOpenImg.Visible = true
Else
mnuSaveImg.Visible = False
mnuCopyImgLoc.Visible = False
mnuOpenImg.Visible = False
End If
Now as you might know all links in HTML are declared like this:
Code: Select all
And all Images are delcared like this:
<a href="http://www.codenstuff.com"> Visit CodenStuff</a>
Code: Select all
So now lets get to the part where the code checks the TagName of hEle, and sees wethere its a link, image or nothing. If it is an image or a link it will show the "mnuSaveImg, MnuCopyImgLoc And OpenImg" in our contextmenu/strip.<img src=http://www.codenstuff.com/forum/styles/prodark/imageset/logoplain.png>
Now debug your browser and test it out, you'll notice that there is a different item for links and for images on our contextmenu/strip. But they are not functioned. So stop debugging and start coding again.
Moving on to the links.
On the mnuOpen click event add this code:
Code: Select all
What this code will does is that it tells the browser to navigate to the selected link when the item is clicked(on the contextmenu/strip).wb.Navigate(hEle.GetAttribute("href"))
And on the mnuCopyLink click event add this code:
Code: Select all
It's obvious what this code does, but if you don't know it will copy the selected Link.Clipboard.SetText(hEle.GetAttribute("href"))
Moving on to the images.
On the mnuSaveImg click event add this code:
Code: Select all
What this code will do it tells our program to get the "src" attribute of the image and it declares it as a string. Next it will be saved on our desired path or path located with a SaveFileDialog, but in this case it's what we want. Then it will download the image to the user's computer.Dim Source as string = hEle.GetAttribute("src")
Dim Path as string = "C:\Images\" 'This can be changed to SaveFileDialog1.InitialDirectory I guess.
My.Computer.Network.DownloadFile(Source, Path)
Now when this is all done, we are nearly finished, but there are few left.
Next go to the mnuCopyImgLoc click event and add this code:
Code: Select all
I have explained what copy link will do, its the same but this one will copy the image not link.Clipboard.SetText(hEle.GetAttribute("src"))
Last and not least, open up the mnuOpenImg click event and add this code:
Code: Select all
This will tell the browser to navigate to the selected image's source location.wb.Navigate(hEle.GetAttribute("src"))
Now we're finished!
Thanks for reading my tutorial, I hope you like it cooll;
You do not have the required permissions to view the files attached to this post.
Last edited by Agust1337 on Thu Feb 17, 2011 6:47 pm, edited 2 times in total.
You're crazy!
I'm not crazy, my mother had me tested. ~Sheldon Cooper
I'm not crazy, my mother had me tested. ~Sheldon Cooper
Nice Tut agust
well done
Dummy1912
well done
Dummy1912
visit us on:
http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
Awesome tutorial, covers everything we need (especially web developers!)
can someone make a project and put 6 credits on it with this? i download it!
Sure, just give me few minutes
You're crazy!
I'm not crazy, my mother had me tested. ~Sheldon Cooper
I'm not crazy, my mother had me tested. ~Sheldon Cooper
agust,i pmed you with what i want...i give you 20 credits....please help me
Agust1337 wrote:Well this is a long reading tutorial for eyes on how to make maybe advanced webbrowser contextmenu.Nice tut but im a noob can any one help me with this like a step - step tutorial. or can some one do the coding simpler or just do it for my WebBrowser?
What I will be showing you in this tutorial is, how to get various properties of HtmlElements based on the mouse position and how to change the various properties of HtmlElements.
In this purpose we are going to use the normal webbrowser control or what ever you call it, but I am aware people are maybe using some class they made for adding everything like tab's and some controls, but it shouldn't be really difficult to add some codes to the classes but on we go.
What controls are needed:
*Webbrowser called wb(or what you called it, I just call it wb cause its easy to remember)
*ContextMenu/Strip named mMenu(once again or something you like)
Items in the ContextMenu/Strip:
*Open Link(named mnuOpen)
*Open Link Location(named mnuLinkLoc)
*Add a separator(by writing "-")
*Save Image As...(mnuSaveImg)
*Copy Image Location(mnuCopyImgLoc)
*Open Image(mnuOpenImg)
Now you have probable made the "IsWebbrowserContextMenuEnabled" property to false, but if not do it now.
Moving on, if your using ContextMenuStrip you can easily add it through the property called "ContextMenuStrip", but if you are using a ContextMenu you'll have to go to Form_Load and put this simple code in it:Code: Select allMoving on to the coding:wb.ContextMenu = ContextMenu1()
As you might know, the webbrowser control doesn't support MouseMove or Down property event, so we have to do it by codes, so we have to declare a HtmlDocument. Get to the code view if you haven't and write this or copy:Code: Select allNow when you are done with this, go to the WebBrowser_Navigated event and add this code:Private WithEvents CurDoc as HtmlDocument Dim P as Point Dim hEle as HtmlElemtn
Code: Select allAnd then go to the CurDoc.MouseMove event and add this code:CurDoc = wb.Document
Code: Select allWhat this code will do is telling our program to record where the mouse is on the webpage so it knows which element is being hovered.P = new Point(MousePosition.X, MousePosition.Y)
Next go to mMenu.Opening or mMenu.Popup(if your using contextmenu) and add this code:Code: Select allWhat this will do is is that where hEle and P come in action and makes hEle(the selected HtmlElement, lets say an image, link or maybe a button) what ever HtmlElement the cursor is over on the webbrowser, now because we made a P(as mouse Position) where the cursor was on the webpage when it was moved.hEle = CurDoc.GetElementFromPoint(P) If hEle.TagName = "A" Then mnuOpen.Visible = false mnuLinkLoc.Visible = false else mnuOpen.Visible = true mnuLinkLoc.Visible = true End If hEle = CurDoc.GetElementFromPoint(P) If hEle.TagName = "A" Then mnuSaveImg.Visible = True mnuCopyImgLoc.Visible = True mnuOpenImg.Visible = true Else mnuSaveImg.Visible = False mnuCopyImgLoc.Visible = False mnuOpenImg.Visible = False End If If hEle.TagName = "IMG" Then mnuSaveImg.Visible = True mnuCopyImgLoc.Visible = True mnuOpenImg.Visible = true Else mnuSaveImg.Visible = False mnuCopyImgLoc.Visible = False mnuOpenImg.Visible = False End If
Now as you might know all links in HTML are declared like this:Code: Select allAnd all Images are delcared like this:<a href="http://www.codenstuff.com"> Visit CodenStuff</a>
Code: Select allSo now lets get to the part where the code checks the TagName of hEle, and sees wethere its a link, image or nothing. If it is an image or a link it will show the "mnuSaveImg, MnuCopyImgLoc And OpenImg" in our contextmenu/strip.<img src=http://www.codenstuff.com/forum/styles/prodark/imageset/logoplain.png>
Now debug your browser and test it out, you'll notice that there is a different item for links and for images on our contextmenu/strip. But they are not functioned. So stop debugging and start coding again.
Moving on to the links.
On the mnuOpen click event add this code:Code: Select allWhat this code will does is that it tells the browser to navigate to the selected link when the item is clicked(on the contextmenu/strip).wb.Navigate(hEle.GetAttribute("href"))
And on the mnuCopyLink click event add this code:Code: Select allIt's obvious what this code does, but if you don't know it will copy the selected Link.Clipboard.SetText(hEle.GetAttribute("href"))
Moving on to the images.
On the mnuSaveImg click event add this code:Code: Select allWhat this code will do it tells our program to get the "src" attribute of the image and it declares it as a string. Next it will be saved on our desired path or path located with a SaveFileDialog, but in this case it's what we want. Then it will download the image to the user's computer.Dim Source as string = hEle.GetAttribute("src") Dim Path as string = "C:\Images\" 'This can be changed to SaveFileDialog1.InitialDirectory I guess. My.Computer.Network.DownloadFile(Source, Path)
Now when this is all done, we are nearly finished, but there are few left.
Next go to the mnuCopyImgLoc click event and add this code:Code: Select allI have explained what copy link will do, its the same but this one will copy the image not link.Clipboard.SetText(hEle.GetAttribute("src"))
Last and not least, open up the mnuOpenImg click event and add this code:Code: Select allThis will tell the browser to navigate to the selected image's source location.wb.Navigate(hEle.GetAttribute("src"))
Now we're finished!
Thanks for reading my tutorial, I hope you like it cooll;
Did you really had to quote the whole thing? You could have simply posted a comment.
This is a step-by-step tutorial GaiaonlineHD.
Agust, there is a spelling error that may ruin this for someone, in your second code block it says HtmlElmtn instead of HtmlElement ;) Great tutorial cooll;
Agust, there is a spelling error that may ruin this for someone, in your second code block it says HtmlElmtn instead of HtmlElement ;) Great tutorial cooll;
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Copyright Information
Copyright © Codenstuff.com 2020 - 2023