简体   繁体   中英

PostMessage a BM_CLICK to LinkLabel doesn't work

I'm using the following code:

PostMessage(handle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);

This works to click a Button , but not a LinkLabel . Any clue as to why not? And is there a workaround?

(Moving the cursor there and clicking is not suitable unless there's a way to get the LinkLabel's coordinates from its handle only. And I'd rather not do that even then, unless there is no other way. Also, this should be done in the caller's code only, with no changing of the LinkLablel's application code.)

That would be

LinkLabel label = (LinkLabel)Control.FromHandle( handle );
((IButtonControl)label).PerformClick();

(haven't tested it though)

The trick is to get the reference to the actual link label so that you can use the fact that it implements the IButtonControl interface.

Edit 1 : how about that then:

int WM_LBUTTONDOWN = 0x201;
int WM_LBUTTONUP   = 0x202;
int MK_LBUTTON     = 1;

PostMessage( handle, WM_LBUTTONDOWN, MK_LBUTTON, 0 );
PostMessage( handle, WM_LBUTTONUP, MK_LBUTTON, 0 );

Edit 2 : this should also work (inspired by suggestion from David Heffernan)

AutomationElement    label = AutomationElement.FromHandle( handle );
var invokePattern = label.GetCurrentPattern( InvokePattern.Pattern ) as InvokePattern;
invokePattern.Invoke();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM