|
hmm i think i tried using sendinput and found it sucked. I like using send message because you dont need to actually move the mouse about to click... you can send the coordinates of where a click is to occur... much more sexy right. Their might be problems or something with some windows and i think i had to have the window focused to get it to actually work.... unfortunatelly its not as nice as when doing sendmessage with keystrokes because in that case theirs no need to have the window focused... good for my bot... had it in the background doing stuff while i used the computer lol...
anyway here's what i had for using sendmessage (i think i had code for sendinput and deleted it because while it worked, it required both focus of the window and ownership of the moues cursor):
<br />
using System;<br />
using System.Text;<br />
using System.Runtime.InteropServices;<br />
using System.Drawing;<br />
using System.Windows.Forms;<br />
using System.Threading;<br />
<br />
namespace MouseKeyboardLibrary<br />
{<br />
public class MouseSimulatorMessenger<br />
{<br />
<br />
[DllImport("user32.dll")]<br />
static extern IntPtr GetForegroundWindow();<br />
<br />
[DllImport("user32.dll")]<br />
private static extern int SetForegroundWindow(IntPtr hWnd);<br />
<br />
<br />
<br />
<br />
<br />
static IntPtr? handle = IntPtr.Zero;<br />
public IntPtr? Handle<br />
{<br />
get { return handle; }<br />
set { handle = value; }<br />
}<br />
<br />
#region Windows API Code<br />
<br />
private static IntPtr MakeLParam(int LoWord, int HiWord)<br />
{<br />
return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
}<br />
<br />
const int WM_LBUTTONDOWN = 0x0201;<br />
const int WM_LBUTTONUP = 0x0202;<br />
<br />
const int WM_MBUTTONDOWN = 0x207;<br />
const int WM_MBUTTONUP = 0x208;<br />
<br />
const int WM_RBUTTONDOWN = 0x0204;<br />
const int WM_RBUTTONUP = 0x0205;<br />
<br />
[return: MarshalAs(UnmanagedType.Bool)]<br />
[DllImport("user32.dll", SetLastError = true)]<br />
static extern bool SendMessageA(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);<br />
<br />
<br />
<br />
private static int MouseButtonDownInteger(MouseButton button)<br />
{<br />
switch (button)<br />
{<br />
case MouseButton.Left:<br />
return WM_LBUTTONDOWN;<br />
<br />
case MouseButton.Middle:<br />
return WM_MBUTTONDOWN;<br />
<br />
case MouseButton.Right:<br />
return WM_RBUTTONDOWN;<br />
}<br />
<br />
throw new ArgumentException("button");<br />
}<br />
<br />
private static int MouseButtonUpInteger(MouseButton button)<br />
{<br />
switch (button)<br />
{<br />
case MouseButton.Left:<br />
return WM_LBUTTONUP;<br />
<br />
case MouseButton.Middle:<br />
return WM_MBUTTONUP;<br />
<br />
case MouseButton.Right:<br />
return WM_RBUTTONUP;<br />
}<br />
<br />
throw new ArgumentException("button");<br />
}<br />
<br />
#endregion<br />
<br />
#region Methods<br />
<br />
public void MouseDown(MouseButton button, int x, int y)<br />
{<br />
<br />
if(handle.HasValue)<br />
SendMessageA(handle.Value, MouseButtonDownInteger(button), 0, MakeLParam(x, y));<br />
else<br />
throw new ArgumentNullException("handle");<br />
<br />
}<br />
<br />
public void MouseUp(MouseButton button, int x, int y)<br />
{<br />
<br />
if (handle.HasValue)<br />
SendMessageA(handle.Value, MouseButtonUpInteger(button), 0, MakeLParam(x, y));<br />
else<br />
throw new ArgumentNullException("handle");<br />
<br />
}<br />
<br />
public void MouseDown(MouseButtons button, int x, int y)<br />
{<br />
switch (button)<br />
{<br />
case MouseButtons.Left:<br />
MouseDown(MouseButton.Left, x, y);<br />
break;<br />
case MouseButtons.Middle:<br />
MouseDown(MouseButton.Middle, x, y);<br />
break;<br />
case MouseButtons.Right:<br />
MouseDown(MouseButton.Right, x, y);<br />
break;<br />
}<br />
}<br />
<br />
public void MouseUp(MouseButtons button, int x, int y)<br />
{<br />
switch (button)<br />
{<br />
case MouseButtons.Left:<br />
MouseUp(MouseButton.Left, x, y);<br />
break;<br />
case MouseButtons.Middle:<br />
MouseUp(MouseButton.Middle, x, y);<br />
break;<br />
case MouseButtons.Right:<br />
MouseUp(MouseButton.Right, x, y);<br />
break;<br />
}<br />
}<br />
<br />
public void Click(MouseButton button, int x, int y)<br />
{<br />
IntPtr oldFocus = GetForegroundWindow();<br />
<br />
MouseDown(button, x, y);<br />
<br />
Thread.Sleep(200);<br />
<br />
MouseUp(button, x, y);<br />
<br />
SetForegroundWindow(oldFocus);<br />
}<br />
<br />
public void Click(MouseButtons button, int x, int y)<br />
{<br />
switch (button)<br />
{<br />
case MouseButtons.Left:<br />
Click(MouseButton.Left, x, y);<br />
break;<br />
case MouseButtons.Middle:<br />
Click(MouseButton.Middle, x, y);<br />
break;<br />
case MouseButtons.Right:<br />
Click(MouseButton.Right, x, y);<br />
break;<br />
}<br />
}<br />
<br />
public void DoubleClick(MouseButton button, int x, int y)<br />
{<br />
Click(button, x, y);<br />
<br />
Thread.Sleep(200);<br />
<br />
Click(button, x, y);<br />
}<br />
<br />
public void DoubleClick(MouseButtons button, int x, int y)<br />
{<br />
switch (button)<br />
{<br />
case MouseButtons.Left:<br />
DoubleClick(MouseButton.Left, x, y);<br />
break;<br />
case MouseButtons.Middle:<br />
DoubleClick(MouseButton.Middle, x, y);<br />
break;<br />
case MouseButtons.Right:<br />
DoubleClick(MouseButton.Right, x, y);<br />
break;<br />
}<br />
}<br />
<br />
#endregion<br />
}<br />
}<br />
|
|
|
|
|
Thank you so very much for your code and time. The MakeLParam code is especially cool. The most valuable info was actually your side comment:
(i think i had code for sendinput and deleted it because while it worked, it required both focus of the window and ownership of the moues cursor)
... as that indeed makes SendInput a deal breaker.
I have tried SendMessage, and it did work, but as i recall it too had some undesirable side-effects ... hence the desperate plea. Once again, thank you.
|
|
|
|
|
Here an assembly with everything :P
http://wolfsfiles.googlepages.com/MouseKeyboardLibrary.zip[^]
Basically it started out as some freely available one i found on the internet, and over time i've been rewriting parts of it to suit my needs... like it didn't come with the sendmessage stuff or the new sendinput stuff i wrote today... Anyways if your interested in trying out sendinput, for the mouse atlesat,... it's in there.
Turns out when i said i tried the send input... it was probably the MouseEvent api i tried... nonetheless i saw it did the same behavior as i said... forcing the cursor to move to a position to click that position.
Was fun to write that... lol SendInput even has compatibility issues with 64bit and 32bit that took a bit of googling to get around.
|
|
|
|
|
I was wondering if I could ask a silly question.
Would it be possible to show an example of how to use the .dll with a Visual Basic app?
I understand what it does and everything just never wrote anything is c# and needed a starting point for a VB app i wanted to play with.
|
|
|
|
|
If by vb you mean vb.net, then it's really easy to do... just unpack the solution and copy it into your vb.net project... then go into visual studio, load your vb.net project, and then right click on your solution and click "Add Existing Project" and navigate to the project file in the dll project. After you do that, you have to go to your vb.net project, and expand the area called "References", and then a window pops up... click the "Project" tab... the c# project should be listed... add it.
Now what this means is the classes in the c# project should be callable from the vb.net project (although i never tried it... but that functionality is said to exist :P).
----------------
If by vb you mean... old vb... not vb.net... then you'd have to rewrite all of the code to get it to work. I have no real experience with old vb so can't help with that :P
|
|
|
|
|
from Visudalstudio, Array.Clone reads: Creates a shallow copy of a array
but from the following simple test code, the results (a[0]=0 for two messagebox shows) show that Array.Clone is a deep copy (or else, the second output should be 100, because a and b refere to the same memory), how to understand it? thanks.
int [] a = new int [1];
a[0] = 0;
MessageBox.Show(a[0].ToString());
int[] b = new int[1];
b =(int []) a.Clone();
b[0] = 100;
MessageBox.Show(a[0].ToString());
|
|
|
|
|
Hi,
a shallow copy copies everything at the first level, values get copied (what else could one do?) and references get copied (not cloned).
in your code, b is a new array which first holds a zero (cloned from a), then gets overwritten explicitly, and then you show a[0]???
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I think I am not so clear about shallow copy and deep copy.
it seems to me that shallow copy and deep copy are sometimes same but sometimes are different.
for value copy, are the same?
for reference copy, they are different.
copy of an int array belong to value copy?, but array variable belong to a reference type.
I think that copy of an int array is a reference copy, therefore, I think for shallow copy, it copies only refrence. later, I found that copy of an int array belong to value copy!
|
|
|
|
|
Hi,
a shallow copy copies value types and references; so all data is the same as the original, but no new objects got created except for the one class instance copied.
a deep copy copies value types and clones referenced objects; so all data is the same as the original, AND new objects got created every time there was a reference.
an array is a reference type.
when the array elements are value types (e.g. int[]) a shallow copya and a deep copy do the same: all values are copied in the new array;
when the array elements are objects (e.g. Control[]) a shallow copy just copies all references, a deep copy clones all referenced objects.
In another way: a shallow copy can be implemented as a memcpy(), copying bytes without knowing the types of the data copied; a deep copy needs to know whether the members are values or references.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
|
Why is your example showing the value of a[0] twice?? You're never showing the value of b[0].
|
|
|
|
|
yes, I want to show a[0] twice in order to check whether the change of b[0] results in also the change of a[0].
|
|
|
|
|
I have a .NET application which is using the Microsoft.mshtml PIA. It frequently experienced exceptions with a message saying that the Microsoft.mshtml assembly or one of its dependencies is missing. Weirdly enough, this happens on some machines, but works fine on others! (actually, it works fine on the majority of machines)
I have tried to resolve this problem by adding PIA as private assembly but problem was not resolved.
then I have installed Microsoft.mshtml PIA in client's machienes GAC through installer. But not working.
I am not getting why this dll is not working as private assembly.
I am getting following Error message:
"Error unable to cas COM object of type 'System._ComObject' to class type 'mshtml.HTMLSelectElementClass'.
components that enter the CLR and do not suppor IProvideClassInfo or that do not have any interop assembly registered will be wrappen in the_
ComObject type. Instances of this type cannot be cast to any other class; however they can be cast to interfaces as long as the underlying COM
component supports QueryInterface calls for the IID of the interface."
Thanks for helps,
Manoj
|
|
|
|
|
That error means that the machine in question doesn't have the COM-based mshtml library installed that your code is using, or has an older version that your code doesn't support. I'd start by checking which version(s) of IE are installed on the machines that work and what version(s) are installed on the machines that don't.
|
|
|
|
|
It is working on IE Version: 6.0.2900.2180.xpsp_sp2_gdr.070227-2254.
But not working on IE Version: 6.0.2900.2180.xpsp_sp2_gdr.090206-1233.
How Can I fix this problem.
|
|
|
|
|
Either reintall IE6, or upgrade the machines to IE7 or 8.
|
|
|
|
|
Hi,
can anybody help me regarding,
creating the notepad application with keyboard shortcuts for copy,cut,paste.....etc..in C#.Net
|
|
|
|
|
A TextBox already has those keyboard shortcuts built in. You don't need to add anything extra. Besides that, a notepad application is relatively simple. Try coding it yourself, and posting when you would like help with specific problems
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
So what's the question?? You haven't asked about any kind of a problem you're having...
|
|
|
|
|
He forgot to ask for the code. This is a first, someone failing at failing.
|
|
|
|
|
I cannot get the System.Net.Mail to send out an email message from my Windows application. The Try Catch reports an the error message 'Failure sending message'. What is causing the error? Below is the code I'm using.
MailAddress objFromMailAddress = new MailAddress("myfromemail@mydomain.com, "");
MailAddress objToMailAddress = new MailAddress("mytoemail@mydomain.com");
MailMessage objMailMessage = new MailMessage(objFromMailAddress, objToMailAddress);
SmtpClient objSmtpClient = new SmtpClient();
objMailMessage.Subject = "Subject line";
objMailMessage.IsBodyHtml = false;
objMailMessage.Body = "Body message";
objSmtpClient.Host = "1and1.com;
objSmtpClient.Port = 587;
try
{
objSmtpClient.Send(objMailMessage);
}
catch (Exception pError)
{
}
|
|
|
|
|
Probably because the host name is bad. You just supplied 1and1.com which is a domain name, but no host. You have to supply the full name of the server, like "smtp.1and1.com" or its IP address.
|
|
|
|
|
Thanks.
I've already tried smtp.1and1.com and could not get it to work.
I will try the IP address.
|
|
|
|
|
Come on. This is where you contact the ISP supplying the SMTP server and get its DNS name, the port number you have to use, and any login information you need.
|
|
|
|
|
You also never set credentials to authenticate yourself with your smtp host.
|
|
|
|
|