how to find properties in right click Contextmenu of C# web browser - c#

Hi i have a c# application and an embedded browser in it,its task is to find a link and then right click on it and click on properties!
mouse moves programmatically,so i need to find properties in righ click menu!
can you help me how to do this?
i tried pressing 'r' after right click but it didn't work on some computers!
so i need to do it by moving mouse!
here is my code for finding a link and right clicking:
int x = getXoffset(link);
int y = getYoffset(link);
webBrowser1.Document.Window.ScrollTo(x, y);
Linker.Win32.POINT p2 = new Linker.Win32.POINT();
webBrowser1.Focus();
p2.x = webBrowser1.Left + 10;
p2.y = webBrowser1.Top + 5;
Linker.Win32.ClientToScreen(this.Handle, ref p2);
Linker.Win32.SetCursorPos(p2.x, p2.y);
MouseOperations.GetCursorPosition();
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightUp);
Any other idea for reaching properties on right click meny is welcomed

use this code:
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
public static void PressKey(Keys key, bool up)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
if (up)
{
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
}
else
{
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
}
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://google.com");//Your link
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Find your link and right click(automatically by your code)
webBrowser1.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
}
void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
if (e.MouseButtonsPressed == MouseButtons.Right)
{
Thread.Sleep(1000);
PressKey(Keys.P, true);
PressKey(Keys.P, false);
}
}

Related

Drag PictureBox

I want to drag a PictureBox, and I have managed to do so. But my application doesn't do it as smoothly as Windows photo viewer. I mean the difference isn't huge or anything, but it's noticeable. Is there something I could do to make it a little less choppy? This is my simple code:
int MOUSE_X = 0;
int MOUSE_Y = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
picBox.Image = Image.FromFile(#"D:\test_big.png");
picBox.Width = 3300;
picBox.Height = 5100;
}
private void picBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MOUSE_X = e.X;
MOUSE_Y = e.Y;
}
}
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
picBox.Left = picBox.Left + (e.X - MOUSE_X);
picBox.Top = picBox.Top + (e.Y - MOUSE_Y);
}
}
Here's a demo that illustrates your approach and the suggested one in the comments.
Testing your code produces:
Whereas the suggested code:
using System.Runtime.InteropServices;
//...
private const int WM_SYSCOMMAND = 0x112;
private const int MOUSE_MOVE = 0xF012;
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(
IntPtr hWnd,
int wMsg,
IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll")]
private static extern int ReleaseCapture(IntPtr hWnd);
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
if (!DesignMode && e.Button == MouseButtons.Left)
{
ReleaseCapture(picBox.Handle);
SendMessage(picBox.Handle, WM_SYSCOMMAND, (IntPtr)MOUSE_MOVE, IntPtr.Zero);
}
}
Produces:
Note that, I also use a background image to make the situation worse if I may say that. However, without the background image, it hard to detect which code snippet is used.

moving a form by holding on a label

I am trying to create a label which is centred within a form which requires me to use the label.dock = dockStyle.Fill. Therefore I tried implementing code which means that when I hold down on the label I can move the form around. This is what i have so far:
private void messageIndicator_MouseUp(object sender, MouseEventArgs e)
{
window.AllowTransparency = true;
window.TransparencyKey = window.TransparencyKey = window.BackColor;
isDragging = false;
}
void messageIndicator_MouseHover(object sender, EventArgs e)
{
window.AllowTransparency = false;
}
private void messageIndicator_MouseDown(object sender, MouseEventArgs e)
{
// Set the drag mode
isDragging = true;
// Get the initial location
lastLocation = e.Location;
}
private void messageIndicator_MouseMove(object sender, MouseEventArgs e)
{
// Only drag if in correct state (mouse down)
if (isDragging)
{
// The parameter sender is the form object
Form f = (Form)sender;
// Calculate the new location and update the form
f.Location = new Point((f.Location.X - lastLocation.X) + e.X, (f.Location.Y - lastLocation.Y) + e.Y);
f.Update();
}
}
messageIndicator being the label on the form. It does crash when Form f = (form)sender is executed. Help would be much appreciated :)
Try this,
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void YourLabel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
Source: Article

WinForms ListBox Append Selection

I have a ListBox with SelectionMode = MultiExtended. I want the default behavior for the ListBox to be "append". In other words, the behavior you get when holding down the control key should be the default, passive functionality for the ListBox.
How would I do this? Do I need to subscribe to the "Mouse Down" and "Key Down" events manually? Is there a setting I'm missing?
Thanks.
Ugly solution but the best I could do.
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const byte KEYEVENTF_KEYUP = 0x02;
public const int VK_CONTROL = 0x11;
private void listBox1_MouseEnter(object sender, EventArgs e)
{
keybd_event(VK_CONTROL, (byte)0, 0, 0);
}
private void listBox1_MouseLeave(object sender, EventArgs e)
{
keybd_event(VK_CONTROL, (byte)0, KEYEVENTF_KEYUP, 0);
}
Use MultiSimple Mode
http://msdn.microsoft.com/en-us/library/system.windows.forms.selectionmode(v=vs.80).aspx
SelectionMode = SelectionMode.MultiSimple

Why isn't my simulated mouse click doing anything?

I've got this code:
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[Flags]
public enum MouseEventFlags
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
Absolute = 0x8000
}
public void SimMouseEvent(MouseEventFlags e, int x, int y)
{
mouse_event((uint)e, (uint)x, (uint)y, 0, UIntPtr.Zero);
}
public void SimLeftClick(int x, int y)
{
SimMouseEvent(MouseEventFlags.LeftUp | MouseEventFlags.RightUp, x, y);
}
My form looks like this:
When you click "Button" it runs this:
private void button3_Click(object sender, RoutedEventArgs e)
{
SimLeftClick(50, 50);
}
And on my Window I also have this:
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("click");
}
When I click the window it says "click" as expected, but when I click "Button" it doesn't seem to do anything.
Are the coordinates absolute, or relative? What about with multiple monitors? Do they only work on the focused application?
I would expect that 50,50 to either hit my window somewhere and trip the "click" handler or click on some random window because it missed my app completely and focus that instead... why isn't anything happening?
You handle a mouse button down message, but send a mouse button up message. A click needs to be button down followed by the same button up.
The coordinates are ignored, because you didn't pass the Move flag.
Try reading the documentation.
Fixed, with Ben's suggestions.
public void SimLeftClick(int x, int y)
{
var scr = Screen.PrimaryScreen.Bounds;
SimMouseEvent(MouseEventFlags.LeftDown | MouseEventFlags.LeftUp | MouseEventFlags.Move | MouseEventFlags.Absolute,
(int)(x / (double)scr.Width * 65535),
(int)(y / (double)scr.Height * 65535));
}

Move window without border

How do I move a window that does not have a border. There is no empty space on the application, all that is available is a webbrowser and a menustrip. I would like the users to be able to move the window by dragging the menu strip. How do I code this? I have tried a few code blocks I have found online, but none of them worked.
This Code Project article should help you accomplish this. I've used this myself with no problems. This is the jist of it:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
This will basically "trick" the window manager into thinking that it is grabbing the title bar of the winform.
To apply it to your project, just use the MouseDown event from the MenuStrip.
Here is the .Net Way
private bool dragging = false;
private Point dragCursorPoint;
private Point dragFormPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
dragCursorPoint = Cursor.Position;
dragFormPoint = this.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
this.Location = Point.Add(dragFormPoint, new Size(dif));
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}
that's it.
Just put the start point into an 2D Array like this:
public partial class mainForm : Form
{
//Global variables for Moving a Borderless Form
private bool dragging = false;
private Point startPoint = new Point(0, 0);
public mainForm()
{
InitializeComponent();
}
private void mainForm_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
startPoint = new Point(e.X, e.Y);
}
private void mainForm_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}
private void mainForm_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point p = PointToScreen(e.Location);
Location = new Point(p.X - this.startPoint.X, p.Y - this.startPoint.Y);
}
}
}
You can fake your menustrip, for example using a panel with a label instead. And then you can handle this manually: when the user clicks the label, a popup menu will open, and when the user drags the label, the window will move. But I would advise against such workarounds, because it's not a standard GUI behavior, and you might get your users confused.
I haven't tried it, but if you can handle the "OnMouseDown" and "onMouseUp" events on the menu bar:
On mouse down - Move the window according to the mouse movement
Stop tracking the mouse movement on mouse up, or mouse out
If you are using a Panel you have to add this in the
YourForm.Designer.cs
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
and this in the
YourForm.cs
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
Mbithi Kioko is on the right track but i would do it this way.
bool dragging = false;
int xOffset = 0;
int yOffset = 0;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
xOffset = Cursor.Position.X - this.Location.X;
yOffset = Cursor.Position.Y - this.Location.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
this.Location = new Point(Cursor.Position.X - xOffset, Cursor.Position.Y - yOffset);
this.Update();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}
I had to use System.Runtime.InteropServices.DllImportAttribute - just thought I would comment and let you all know.

Categories

Resources