I have a problem with C# virtual mouse click.. you know that we should use "user32.dll" library and the mouse_event functions..
I use the following function :
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
private const int MOUSEEVENTF_MOVE = 0x0001;
private const int MOUSEEVENTF_XDOWN = 0x0080;
private const int MOUSEEVENTF_XUP = 0x0100;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x0010;
and use this like that :
mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
Guys, It is work on the all applications very good and Ok ! But in Flash Applications no
I have seen a post in here :
Problem automating mouse and interacting with Flash apps
They said that use from the all mouse_event functions , I used all of them but no one couldn't help me...
An other person who says use SendInput , You know better than me that the shapes that made by flash player doesn't have handle. and just main program has access to them. so we can't use SendInput...
I wanna do my problem with this code.. ( I don't like use difficult methods like Dynamic Data Exchange , etc... ) I wanna do it with my learnings... Can i ?
Thank you very much...
I found it...
You should Run your process as Administrator to do that...
Related
I want to make a c# win forms app that will do a left mouse click event whenever the user scrolls even when outside of the application. I am using a thread and have the constants needed but I do not know how to check if the user scrolls, simulate mouse click.
I have this for using the mouse events and created a thread.
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
private const int LEFTUP = 0x0004;
private const int LEFTDOWN = 0x0002;
private const int RIGHTUP = 0x00010;
private const int RIGHTDOWN = 0x0008;
private const int WHEEL = 0x0800;
public int debounceTime = 8;
What would I have to write in the thread for it to check if there is a scroll wheel event, and then send a mouse left event?
I'm making an app that lets you navigate between items using SendKeys. All works pretty well except when I try to send the Application Key (context menu) to do a right click on the selected item.
I use:
SendKeys.Send("{APPSKEY}");
I get an Error saying 'Keyword "APPSKEY" is not valid.'
I googled it and found it on this website:
http://www.autohotkey.com/docs/commands/Send.htm
But i'm guessing that doesn't work for c#.
is there any other way to do a rightclick on the selected item?
is there a way to tell the app where the item is located to move the mouse there and do a right click?
my program can send MouseClicks:
public partial class Form1 : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint
cButtons, uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
private void MoveCursor(Point loc)
{
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = loc;
Cursor.Clip = new Rectangle(0, 0, 0, 0);
}
private void DoMouseClick(bool isLeft)
{
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
if (isLeft) mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (uint)X, (uint)Y, 0, 0);
else mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, (uint)X, (uint)Y, 0, 0);
}
}
But in order to simulate a right click on that item the app has to know where it is located.
Try
SendKeys.Send("+{F10}");
Regards,
We have a WebBrowser that needs to have login information reset.
There is a solution in this question that suggests using INTERNET_HANDLE_TYPE_INTERNET with INTERNET_OPTION_END_BROWSER_SESSION. However, I'm not sure where INTERNET_HANDLE_TYPE_INTERNET should go.
private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
private const int INTERNET_HANDLE_TYPE_INTERNET = 1;
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption,
IntPtr lpBuffer, int lpdwBufferLength);
I call it right before I navigate:
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero,
0);
browserCtrl.WebBrowser.Navigate(loginUrl);
On a WindowsCE platform (custom build) our C# gui uses regular forms to show an "popup menu".
We set the FormBorderstyle to None as we don't want the form controls to be visible.
Some clients reported "Gray boxes" after a while.
After some testing here we could reproduce the problem quite fast. When we open 2 different menu's (forms) constantly the platform shows us an native exception.
Error
A native exception has occurred
in Tiger.CEHost.exe. Select Quit and
then restart this program, or select
Details for more information.
The details:
Error
ExceptionCode: 0xC0000005
ExceptionAdress: 0x00000001
Reading: 0x00000001
at WL.SetSTyle(IntPtr hwnThis, UInt32 dwMask, UInt32 dwStyle)
at Form._SetBorderStyle(AGL_WINDOWSTYLE wstyVal, AGL_WINDOWSTYLE wstyMask)
at Form.set_FormBorderStyle(FormBorderStyle value)
at pDropDown.PopupForm.Show()
at pDropDown.Show()
at pButton.ShowHideDropDown()
at pButton.OnClick(EventArgs e)
at Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at EVL.EnterMainLoop(IntPtr hwnMain)
at Application.Run(Form fm)
at Program.Main(String[] args)
It always seems to fail at the FormBorderStyle property. We've already tried to remove all the pInvokes as perhaps some memory was overwritten, but this didn't help.
We also log each call to the Show method and each call is made in the gui thread and the Form contains a valid handle.
I've never seen this, which tends to make me think that it's less likely to be a problem in the CF or even your app.
Does your device have enough memory to run the app? A low-memory condition should throw an OOM, but I've seen it do other, less predicatble things, so it's always the first thing to check.
If memory is not the issue, are you certain that it's not a platform problem? Remember, since a large portion of the OS is developed by the OEM, you can't rule out problems in the OS.
I'd try two things:
Does the same app run fine on some other hardware (even the emulator) without problems? If it works on other hardware, it heavily implicates the platform as the problem.
Since it's fairly easy to repro with a small app in C#, I'd recommend building an app in C/C++ that does the same functional items to see if it behaves or gives the same issue.
It seems to be a bug in netcfagl3_5.dll (will notify Microsoft about this)
When we set the FormBorderstyle using pinvokes (SetWindowLong) we can't reproduce the problem.
In case someone experiences this rare bug, this is the code to set the formborderstyle without using the .net FormBorderStyle property.
private const uint WS_OVERLAPPED = 0x00000000;
private const uint WS_POPUP = 0x80000000;
private const uint WS_CHILD = 0x40000000;
private const uint WS_MINIMIZE = 0x20000000;
private const uint WS_VISIBLE = 0x10000000;
private const uint WS_DISABLED = 0x08000000;
private const uint WS_CLIPSIBLINGS = 0x04000000;
private const uint WS_CLIPCHILDREN = 0x02000000;
private const uint WS_MAXIMIZE = 0x01000000;
private const uint WS_CAPTION = 0x00C00000;
private const uint WS_BORDER = 0x00800000;
private const uint WS_DLGFRAME = 0x00400000;
private const uint WS_VSCROLL = 0x00200000;
private const uint WS_HSCROLL = 0x00100000;
private const uint WS_SYSMENU = 0x00080000;
private const uint WS_THICKFRAME = 0x00040000;
private const uint WS_GROUP = 0x00020000;
private const uint WS_TABSTOP = 0x00010000;
private const int WS_MINIMIZEBOX = 0x00020000;
private const int WS_MAXIMIZEBOX = 0x00010000;
private const uint WS_EX_DLGMODALFRAME = 0x00000001;
private const uint WS_EX_NOPARENTNOTIFY = 0x00000004;
private const uint WS_EX_TOPMOST = 0x00000008;
private const uint WS_EX_ACCEPTFILES = 0x00000010;
private const uint WS_EX_TRANSPARENT = 0x00000020;
private const uint WS_EX_MDICHILD = 0x00000040;
private const uint WS_EX_TOOLWINDOW = 0x00000080;
private const uint WS_EX_WINDOWEDGE = 0x00000100;
private const uint WS_EX_CLIENTEDGE = 0x00000200;
private const uint WS_EX_CONTEXTHELP = 0x00000400;
private const uint WS_EX_STATICEDGE = 0x00020000;
private const int WS_EX_NOANIMATION = 0x04000000;
public const int GWL_EX_STYLE = -20;
public const int GWL_STYLE = (-16);
public static void SetNoBorder(Form form) {
RemoveFormStyle(form, GWL_STYLE, (int)(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU));
RemoveFormStyle(form, GWL_EX_STYLE, (int)(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
}
public static void RemoveFormStyle(Form f, int modifier, int style) {
int currStyle = GetWindowLong(f.Handle, GWL_EX_STYLE);
currStyle &= ~style;
SetWindowLong(f.Handle, modifier, currStyle);
}
[DllImport("Coredll.dll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("coredll.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
how to change the window style of a form outside your app?hard question?
i am actually trying to move a form witch is topmost and has no border.
i have the handle(hWnd) of the window.
i can write thousands of lines of code if guaranteed to work.
Assuming that this window could be from any app produced from any kind of Win32-based runtime, it looks like you'll have to resort to p/invoke of the core Win32 apis for window operations.
For example, you could use SetWindowPos, which can be imported from user32.dll. It's signature is this:
BOOL SetWindowPos(HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);
I'm not going to assume that you've done a p/invoke import before, so let's go from the top. Let's just bash out a windows forms app:
1) Create a windows forms app and then add these declarations to the Form1 class:
/* hWndInsertAfter constants. Lifted from WinUser.h,
* lines 4189 onwards depending on Platform SDK version */
public static IntPtr HWND_TOP = (IntPtr)0;
public static IntPtr HWND_BOTTOM = (IntPtr)1;
public static IntPtr HWND_TOPMOST = (IntPtr)(-1);
public static IntPtr HWND_NOTOPMOST = (IntPtr)(-2);
/* uFlags constants. Lifted again from WinUser.h,
* lines 4168 onwards depending on Platform SDK version */
/* these can be |'d together to combine behaviours */
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_NOREDRAW = 0x0008;
public const int SWP_NOACTIVATE = 0x0010;
public const int SWP_FRAMECHANGED = 0x0020;
public const int SWP_SHOWWINDOW = 0x0040;
public const int SWP_HIDEWINDOW = 0x0080;
public const int SWP_NOCOPYBITS = 0x0100;
public const int SWP_NOOWNERZORDER = 0x0200; /* Don't do owner Z ordering */
public const int SWP_NOSENDCHANGING = 0x0400; /* Don't send WM_WINDOWPOSCHANGING */
public const int SWP_DRAWFRAME = SWP_FRAMECHANGED;
public const int SWP_NOREPOSITION = SWP_NOOWNERZORDER;
public const int SWP_DEFERERASE = 0x2000;
public const int SWP_ASYNCWINDOWPOS = 0x4000;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowsPos(IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
UInt32 uFlags);
The annoying thing with p/invoke of Win32 windows methods is that you then have to start importing various numeric constants etc that Win32 uses - hence all the gumph beforehand.
Refer to the MSDN link for the SetWindowPos method for an explanation of what they do.
2) Add a button to the form called cmdMakeHidden and then write the handler as follows:
private void cmdMakeHidden_Click(object sender, EventArgs e)
{
//also causes the icon in the start bar to disappear
//SWP_HIDEWINDOW is the 'kill -9' of the windows world without actually killing!
SetWindowPos(this.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_HIDEWINDOW);
}
Replace the 'this.Handle' with the window handle of your choice to hide that window.
This method is actually used to apply multiple changes at once, hence the need to use some of the SWP_NO* options. For example, you should specify SWP_NOSIZE otherwise passing 0 for cx and cy will cause the window to shrink to zero width and height at the same time.
To demonstrate moving a window, add another button your form called cmdMove and then write the click handler as follows:
private void cmdMove_Click(object sender, EventArgs e)
{
SetWindowPos(this.Handle, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION);
}
This code moves your form to 100,100 whenever you hit the button.
Again, replace the this.Handle as you see fit. HWND_TOP here is completely optional, since reordering has been disabled with the SWP_NOZORDER and SWP_NOREPOSITION flags.
Hope this helps get you on the right track!