Add class AhkMessenger to your c# project, or add the relevant code to a class you already have
C#
Code: Select all
using System;
using System.Runtime.InteropServices;
namespace Whatever
{
public class Test // Example
{
private AhkMessenger _messenger = null;
public Test(int ahkBttnHwnd, int ahkBttnHwnd)
{
_messenger = new(ahkBttnHwnd, ahkBttnHwnd);
Thread.Sleep(5000) // Do Work
_messenger.SendMessage("Class Test Loaded");
}
public void SomeMethod()
{
Thread.Sleep(5000) // Do Work
_messenger.SendMessage("Executed SomeMethod()");
}
}
public class AhkMessenger
{
private IntPtr _bttnHwnd = IntPtr.Zero;
private IntPtr _editHwnd = IntPtr.Zero;
public AhkMessenger(int ahkBttnHwnd, int ahkEditHwnd)
{
_bttnHwnd = new(ahkBttnHwnd);
_editHwnd = new(ahkEditHwnd);
}
public void SendMessage(string msg)
{
SendMessage(_editHwnd, WM_SETTEXT, IntPtr.Zero, msg);
SendMessage(_editHwnd, EM_SETMODIFY, IntPtr.Zero, IntPtr.Zero);
SendMessage(_bttnHwnd, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
}
private const int BM_CLICK = 0x00F5;
private const int WM_SETTEXT = 0x000C;
private const int EM_SETMODIFY = 0xB9;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
}
}
For the ahk setup you add a hidden button and an edit to your existing gui
AHK
Code: Select all
asm := CLR_LoadLibrary("test.dll")
; Your Gui Stuff here
Gui Add, Button, w1 h1 x2 y-4 +HwndhBttn gButtonClicked ; must have a width and height
Gui Add, Button, w1 h1 x4 y-4 +HwndhEdit ; must have a width and height
Gui Show
CLR_ClassTest_Obj := CLR_CreateObject(asm, "Whatever.Test", hBttn+0, hEdit+0) ; +0 to make sure its an int. CLR will error if not
CLR_ClassTest_Obj.SomeMethod()
return
ButtonClicked: ; Recieve Message
GuiControlGet out,, % hEdit
GuiControl,, % hEdit
ToolTip % out
return
Alternatively you could wrap a gui in a class to be the message reciever
AHK
Code: Select all
class MessageReciever
{
__New(messageHandler)
{
Gui New, +HwndhGui
Gui %hGui%:Add, Edit, w1 h1 +HwndhEdit
Gui %hGui%:Add, Button, w1 h1 +HwndhBttn
this.Hwnd := hGui
this.BttnHwnd := hBttn+0
this.EditHwnd := hEdit+0
bh := Func(messageHandler).Bind(this)
GuiControl +g, % hBttn, % bh
}
Message[]
{
get
{
GuiControlGet out,, % this.EditHwnd
GuiControl,, % this.EditHwnd
return out
}
}
Destroy()
{
hwnd := this.Hwnd
Gui %hwnd%:Destroy
this.Hwnd := this.BttnHwnd := this.EditHwnd := hwnd := ""
}
}