whats wrong with this?

B

blah

Hello everyone, Ive been trying to get my application to "click" on a
button in another application using SendMessage, Ive gotten this far
but Im not sure whats wrong with this code, here is the whole
application (its small for testing purposes) and it seems that window
wraps the text, at least when I preview this post:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace test
{
public partial class Form1 : Form
{
[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);
public Form1()
{
InitializeComponent();
}
public class Win32
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public const int BM_CLICK = 0xF5;
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClass, string
lpWindow);

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);
}
private void button1_Click(object sender, EventArgs e)
{



int hWnd;
//Thread.Sleep(5000);
if ((txtWindowCaption.Text == "") && (txtClassName.Text ==
""))
{
hWnd = Win32API.FindWindowAny(0, 0);
}
else if ((txtWindowCaption.Text == "") &&
(txtClassName.Text != ""))
{
hWnd =
Win32API.FindWindowNullWindowCaption(txtClassName.Text, 0);

}
else if ((txtWindowCaption.Text != "") &&
(txtClassName.Text == ""))
{
hWnd = Win32API.FindWindowNullClassName(0,
txtWindowCaption.Text);
}
else
{
hWnd = Win32API.FindWindow(txtClassName.Text,
txtWindowCaption.Text);
}
if (hWnd == 0)
{
MessageBox.Show("Specified window is not running.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
// Set the window foreground.
Win32API.SetForegroundWindow(hWnd);
// if window is minimized, simply restore, otherwise
show it. Notice the
// declaration of Win32API.IsIconic defines the return
value bool
// allowing .NET to marshall the integer value to a
bool.

if (Win32API.IsIconic(hWnd))
{
Win32API.ShowWindow(hWnd, Win32API.SW_RESTORE);
}
else
{
Win32API.ShowWindow(hWnd, Win32API.SW_SHOW);
}
int a = Win32.FindWindow(txtClassName2.Text,
Title.Text);
int b = Win32.SendMessage(a, Win32.BM_CLICK, 0, 0);
}
}
}
}
 
S

Stoitcho Goutsev \(100\)

blah,

The sample you posted is not compilable there is bunch of methods that the
code use, but are not posted. The missing class is Win32API.
 
J

Jon Skeet [C# MVP]

Stoitcho said:
The sample you posted is not compilable there is bunch of methods that the
code use, but are not posted. The missing class is Win32API.

.... and the rest of Form1. (It's a partial class, and only the
non-designer part has been posted.)

Jon
 
B

blah

The part with Win32API works, I use Win32 because when I try to use
SendMessage with Win32API it gives me an error saying that it does not
contain a definition for SendMessage. how do I define it?
 
W

Willy Denoyette [MVP]

| The part with Win32API works, I use Win32 because when I try to use
| SendMessage with Win32API it gives me an error saying that it does not
| contain a definition for SendMessage. how do I define it?
|

It is defined (be it not completely correct) by this:

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);

but if you care about portability with 64bit versions of windows you should
definitely change int hWnd to IntPtr hWnd type, the same is true for the
return value, WPARAM and LPARAM types!
So your declaration should look like:
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);

Not sure why you get this compile error though, are you sure this is the
offending line ?

int b = Win32.SendMessage(a, Win32.BM_CLICK, 0, 0);
}
or do you call SendMessage from other classes in you project?
I would suggest you make Win32 a separate class instead of a nested one.


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top