beginners question API

H

Hello everyone

Hello everyone, Ive been trying to get this code to work for very long
now, but I havent been able to catch the problem. Im trying to use the
SendMessage method but when I execute the program nothing happens. Thats
the best response Ive gotten from it so far, other times it mdoesnt
compile. Its a short code, Im trying simply to get SendMessage to click
a button in another specified app when I click my button. Im using c#
express with framework 2.0, the example Ive found for this were all for
older framwork versions and didnt work. Can anyone show me where Im
wrong?
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
{
public Form1()
{
InitializeComponent();
}
public class Win32
{
[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);
public const int BM_CLICK = 0x00F5;
[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;
hWnd = Win32API.FindWindow(Class1.Text, caption.Text);
Win32API.SetForegroundWindow(hWnd);
Win32.SendMessage(hWnd, Win32.BM_CLICK, 0, 0);
}
}
}
 
M

Mattias Sjögren

[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);

There's no function called GetNextWindow exported from user32.dll.
GetNextWindow is simply a macro calling GetWindow, so it will probably
work better if you just remove the EntryPoint.

hWnd = Win32API.FindWindow(Class1.Text, caption.Text);
Win32API.SetForegroundWindow(hWnd);
Win32.SendMessage(hWnd, Win32.BM_CLICK, 0, 0);

FindWindow only searches top level windows. Do you really have a
button floating around on the desktop without a parent?


Mattias
 
H

Hello everyone

I understand your first point about the GetNextWindow, but about the
second part with FindWindow, that is the only way I knew how to do it.
What can I use instead? Thanks for your help by the way!
 
M

Mattias Sjögren

I understand your first point about the GetNextWindow, but about the
second part with FindWindow, that is the only way I knew how to do it.
What can I use instead? Thanks for your help by the way!

If the button is a control in some other window, you first have to
find the top level window with FindWindow, then walk your way down the
control tree with functions like FindWindowEx, GetWindow or
GetDlgItem.


Mattias
 

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