It is possible with a workaround or low-level code.
// class "Toolbar_Subclass.cs"
using Au.Triggers;
partial class Program {
[Toolbars]
void Toolbar_Subclass_Triggers() {
//print.clear();
Toolbar_Subclass();
}
void Toolbar_Subclass(TriggerArgs ta = null) {
KMod mod = 0;
var t = new toolbar();
t["LeftorRightClick"] = o => {
print.it(mod);
};
t["other"] = null;
t.Show(ta);
WndUtil.Subclass(t.Hwnd, _WndProc);
nint _WndProc(wnd w, int msg, nint wp, nint lp) {
//WndUtil.PrintMsg(w, msg, wp, lp);
switch (msg) {
case napi.WM_LBUTTONDOWN:
print.it("WM_LBUTTONDOWN");
mod = keys.gui.getMod(); //save for the LeftorRightClick button code. It will run when mouse button released.
if (!(mod is 0 or KMod.Shift)) { //0 click, Shift move. For others the button action is not called. This is a workaround.
if (mod.Has(KMod.Alt)) keys.more.sendKey(KKey.Alt, false);
if (mod.Has(KMod.Ctrl)) keys.more.sendKey(KKey.Ctrl, false);
if (mod.Has(KMod.Shift)) keys.more.sendKey(KKey.Shift, false);
if (mod.Has(KMod.Win)) keys.more.sendKey(KKey.Win, false);
}
break;
case napi.WM_RBUTTONDOWN:
print.it("WM_RBUTTONDOWN");
break;
case napi.WM_MBUTTONDOWN:
print.it("WM_MBUTTONDOWN");
//Your LeftorRightClick button code will not run on right or middle click.
//There is no public function to get the clicked button. But it is possible.
int i = t.GetClickedButton(lp);
print.it("Button index: ", i);
break;
}
//To steal a message from the toolbar, return without calling DefSubclassProc. Can return 0, or `api.DefWindowProc(w, msg, wp, lp)`, or whatever.
//If window messages are new to you, need some learning.
//See also _WndProc in: https://github.com/qgindi/LibreAutomate/blob/master/Au/GUI/toolbar/toolbar.cs
return WndUtil.DefSubclassProc(w, msg, wp, lp);
}
}
}
static class Ext8745 {
/// <summary>
/// When called on a mouse button message, returns 0-based index of the clicked button.
/// Returns -1 if not a button.
/// Uses a private function vis reflection, therefore may stop working in the future.
/// </summary>
public static int GetClickedButton(this toolbar t, nint lp) {
var p = Math2.NintToPOINT(lp);
_HitTestD ??= typeof(toolbar).GetMethod("_HitTest", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).CreateDelegate<_HitTestT>(t);
return _HitTestD(p);
}
delegate int _HitTestT(POINT p);
static _HitTestT _HitTestD;
}
#pragma warning disable 649, 169 //field never assigned/used
unsafe class napi : NativeApi {
internal const ushort WM_LBUTTONDOWN = 0x201;
internal const ushort WM_RBUTTONDOWN = 0x204;
internal const ushort WM_MBUTTONDOWN = 0x207;
}
#pragma warning restore 649, 169 //field never assigned/used