开发者

SendMessage API to a java program - How to [duplicate]

This question already has answers here: Simulating Key Press C# (8 answers) Closed 4 years ago.

Im new at programming.

im going to make a program wich can send inputs/text to a game called minecraft - its a game made in Java.

im trying to use the SendMessage API but i dont know how to use开发者_JAVA百科 it..

this is my code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace MinecraftTest2_Sendinput
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImportAttribute("User32.dll")]
        private static extern int FindWindow(String ClassName, String
        WindowName);

        [DllImportAttribute("User32.dll")]
        private static extern int SetForegroundWindow(int hWnd);

        [System.Runtime.InteropServices.PreserveSig]
        [DllImport("User32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Ansi)]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, long lParam);


        private void button1_Click(object sender, EventArgs e)
        {
            int hWnd = FindWindow(null, "Minecraft");
            if (hWnd > 0)
            {
                SetForegroundWindow(hWnd);
                //I need to call the SendMessage here! but what should i type in the arguments? 
            }

        }
    }
}


This depends on the message you want to send to the window. A complete list of Windows Messages is available here and here . the wParam and lParam are message dependent, they act as parameters for the message being sent to the window message queue.

Here is a small snippet to send Left Mouse Button click message to the window The params are both null.

 int WM_LBUTTONDOWN = &H201;
 int WM_LBUTTONUP = &H202;
 SendMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero); // Mouse Down
 SendMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero); // Mouse Up

if for example you want to inform the window that the Control key is pressed, just use MK_CONTROL for wParam, if you want to specify the coordinates, use the lParam like so

 int lParam = X + Y<<16;
 SendMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam); // Mouse Down
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜