Hey everyone...

I need some help...please...

Using the web browser control, I want to navigate to a URL and move my mouse over ANY element on that website and have a focus rectangle display around it...

I've written a whole bunch of code but none of it seems to work - but I want to believe that I'm close, for example

private: System::Void Document_MouseOver(System::Object ^sender, System::EventArgs ^e){
		elm = inspectorWB->Document->GetElementFromPoint(MousePosition);
		float PenWidth = 5;
		Pen ^Rect_Pen = gcnew Pen(Color::AliceBlue, PenWidth );
		System::Drawing::Rectangle rect = elm->ClientRectangle;
		Graphics ^Area = nullptr;
		Area = Graphics::FromHwnd(inspectorWB->Handle);
		Area->DrawRectangle(Rect_Pen, rect);
	}

Can someone please let me know what it is I'm doing wrong

Hi

Have you tried using:

System::Windows::Forms::ControlPaint::DrawReversibleFrame(rect, color, style)

It draws a rectangle directly to the screen (no graphics surface is required).
Note that the rect is in screen co-ordinates.
It requires 2 calls, the first to draw the rectangle and the second call will erase it.

Let me know how you get on.

Milton

Milton - I will give this option a try tonight and post my results...THANK YOU VERY MUCH!!!

* frustration + determination = my mood *

Milton, I tried using the DrawReversibleFrame, however it did not perform the way i wanted - i am looking to implement / develop a tool where a focus rectangle appears around an HTML element when you mouseover it within the web browser control - am i making sense

The piece of code that i'm having trouble with is highlighted below

#pragma once

#include <mshtml.h>

namespace urban {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections::Generic;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Text;

	/// <summary>
	/// Summary for uInspector
	/// </summary>
	public ref class uInspector : public System::Windows::Forms::Form
	{
	public:
		uInspector(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~uInspector()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::SplitContainer^  splitContainer1;
	protected: 
	public: System::Windows::Forms::WebBrowser^  inspectorWB;
	private: System::Windows::Forms::SplitContainer^  splitContainer2;
	private: System::Windows::Forms::TreeView^  treeView1;
	public: System::Windows::Forms::PropertyGrid^  propertyGrid1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->splitContainer1 = (gcnew System::Windows::Forms::SplitContainer());
			this->inspectorWB = (gcnew System::Windows::Forms::WebBrowser());
			this->splitContainer2 = (gcnew System::Windows::Forms::SplitContainer());
			this->treeView1 = (gcnew System::Windows::Forms::TreeView());
			this->propertyGrid1 = (gcnew System::Windows::Forms::PropertyGrid());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->splitContainer1))->BeginInit();
			this->splitContainer1->Panel1->SuspendLayout();
			this->splitContainer1->Panel2->SuspendLayout();
			this->splitContainer1->SuspendLayout();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->splitContainer2))->BeginInit();
			this->splitContainer2->Panel1->SuspendLayout();
			this->splitContainer2->Panel2->SuspendLayout();
			this->splitContainer2->SuspendLayout();
			this->SuspendLayout();
			// 
			// splitContainer1
			// 
			this->splitContainer1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->splitContainer1->Location = System::Drawing::Point(0, 0);
			this->splitContainer1->Name = L"splitContainer1";
			// 
			// splitContainer1.Panel1
			// 
			this->splitContainer1->Panel1->Controls->Add(this->inspectorWB);
			// 
			// splitContainer1.Panel2
			// 
			this->splitContainer1->Panel2->Controls->Add(this->splitContainer2);
			this->splitContainer1->Size = System::Drawing::Size(1305, 598);
			this->splitContainer1->SplitterDistance = 729;
			this->splitContainer1->TabIndex = 0;
			// 
			// inspectorWB
			// 
			this->inspectorWB->Dock = System::Windows::Forms::DockStyle::Fill;
			this->inspectorWB->Location = System::Drawing::Point(0, 0);
			this->inspectorWB->MinimumSize = System::Drawing::Size(20, 20);
			this->inspectorWB->Name = L"inspectorWB";
			this->inspectorWB->Size = System::Drawing::Size(729, 598);
			this->inspectorWB->TabIndex = 0;
			// 
			// splitContainer2
			// 
			this->splitContainer2->Dock = System::Windows::Forms::DockStyle::Fill;
			this->splitContainer2->Location = System::Drawing::Point(0, 0);
			this->splitContainer2->Name = L"splitContainer2";
			this->splitContainer2->Orientation = System::Windows::Forms::Orientation::Horizontal;
			// 
			// splitContainer2.Panel1
			// 
			this->splitContainer2->Panel1->Controls->Add(this->treeView1);
			// 
			// splitContainer2.Panel2
			// 
			this->splitContainer2->Panel2->Controls->Add(this->propertyGrid1);
			this->splitContainer2->Size = System::Drawing::Size(572, 598);
			this->splitContainer2->SplitterDistance = 288;
			this->splitContainer2->TabIndex = 0;
			// 
			// treeView1
			// 
			this->treeView1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->treeView1->Location = System::Drawing::Point(0, 0);
			this->treeView1->Name = L"treeView1";
			this->treeView1->Size = System::Drawing::Size(572, 288);
			this->treeView1->TabIndex = 0;
			this->treeView1->NodeMouseClick += gcnew System::Windows::Forms::TreeNodeMouseClickEventHandler(this, &uInspector::TreeNodeMouseClickEventHandler);
			// 
			// propertyGrid1
			// 
			this->propertyGrid1->Dock = System::Windows::Forms::DockStyle::Fill;
			this->propertyGrid1->Location = System::Drawing::Point(0, 0);
			this->propertyGrid1->Name = L"propertyGrid1";
			this->propertyGrid1->Size = System::Drawing::Size(572, 306);
			this->propertyGrid1->TabIndex = 0;
			// 
			// uInspector
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(1305, 598);
			this->Controls->Add(this->splitContainer1);
			this->Name = L"uInspector";
			this->Text = L"urban software: Object Inspector";
			this->Load += gcnew System::EventHandler(this, &uInspector::uInspector_Load);
			this->splitContainer1->Panel1->ResumeLayout(false);
			this->splitContainer1->Panel2->ResumeLayout(false);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->splitContainer1))->EndInit();
			this->splitContainer1->ResumeLayout(false);
			this->splitContainer2->Panel1->ResumeLayout(false);
			this->splitContainer2->Panel2->ResumeLayout(false);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->splitContainer2))->EndInit();
			this->splitContainer2->ResumeLayout(false);
			this->ResumeLayout(false);
		}

#pragma endregion
	MSHTML::IHTMLElement ^element;
	HtmlElement ^elm;

	private: System::Void uInspector_Load(System::Object ^sender, System::EventArgs ^e) {
				inspectorWB->Navigate("about:blank");
				try{
					inspectorWB->Document->Click += gcnew HtmlElementEventHandler(this, &uInspector::Document_Click);
					inspectorWB->Document->MouseOver += gcnew HtmlElementEventHandler(this, &uInspector::Document_MouseOver);
				}

				catch(NullReferenceException ^e){
					//do nothing
				}
			 }

	private: System::Void Document_Click(System::Object ^sender, HtmlElementEventArgs ^e) {
				propertyGrid1->SelectedObject = (safe_cast<HtmlDocument^>(sender))->ActiveElement;
			 }

	[B]private: System::Void Document_MouseOver(System::Object ^sender, HtmlElementEventArgs ^e) {
				elm = inspectorWB->Document->GetElementFromPoint(MousePosition);
				float PenWidth = 5;
				
				Pen ^Rect_Pen = gcnew Pen(Color::AliceBlue, PenWidth );
				System::Drawing::Rectangle rect = elm->ClientRectangle;
				Graphics ^Area;
				Area = Graphics::FromHwnd(inspectorWB->Handle);
				//Area->DrawRectangle(Rect_Pen, rect);
				ControlPaint::DrawFocusRectangle( Area, rect );
			 }[/B]

	private: System::Void CopyElementToTree(MSHTML::IHTMLElement ^element, TreeNode ^treeNode) {
				MSHTML::IHTMLElementCollection ^children = safe_cast<MSHTML::IHTMLElementCollection^>(element->children);

				if (children->length != 0){
					for each (MSHTML::IHTMLElement ^e in children){
						TreeNode ^treeChild = gcnew TreeNode();
						treeChild->Text = getAttrs(e);
						treeChild->Tag = e;
						treeNode->Nodes->Add(treeChild);
						CopyElementToTree(e, treeChild);
					}
				}else{
					if (! (System::String::IsNullOrEmpty(element->innerText))){
						TreeNode ^textNode = gcnew TreeNode();
						textNode->Text = System::String::Format("\"{0}\"", element->innerHTML);
						textNode->Tag = element;
						treeNode->Nodes->Add(textNode);
					}
				}
			 }

	private: System::String ^getAttrs(MSHTML::IHTMLElement ^element){
				StringBuilder ^result = gcnew StringBuilder("<");
				result->Append(element->tagName->ToLower());
				MSHTML::IHTMLDOMNode ^domnode = safe_cast<MSHTML::IHTMLDOMNode^>(element);
				MSHTML::IHTMLAttributeCollection ^attrs = safe_cast<MSHTML::IHTMLAttributeCollection^>(domnode->attributes);
				if (attrs != nullptr){
					for each (MSHTML::IHTMLDOMAttribute ^attr in attrs){
						if (! attr->specified){
							continue;
						}
						result->Append(" ");
						System::String ^nodeValue = System::String::Empty;
						if (attr->nodeValue != nullptr){
							nodeValue = attr->nodeValue->ToString();
						}
						result->Append(System::String::Format("{0}=\"{1}\"", attr->nodeName, nodeValue));
					}
				}
				result->Append(">");
				return result->ToString();
			 }

	public: System::Void UpdateDOM(MSHTML::IHTMLDocument3 ^HTMLDocument){
				treeView1->Nodes->Clear();
				treeView1->BeginUpdate();
				MSHTML::IHTMLElement ^domRoot = HTMLDocument->documentElement;
				TreeNode ^treeRoot = gcnew TreeNode();
				treeRoot->Text = "<html>";
				treeRoot->Tag = domRoot;
				treeView1->Nodes->Add(treeRoot);
				CopyElementToTree(domRoot, treeRoot);
				treeView1->EndUpdate();
				treeView1->ExpandAll();
			}

	public: System::Void TreeNodeMouseClickEventHandler(System::Object ^sender, System::Windows::Forms::TreeNodeMouseClickEventArgs ^e){
				propertyGrid1->SelectedObject = inspectorWB->Document->ActiveElement;
				
			}
};
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.