简体   繁体   English

PostMessage BM_CLICK到LinkLabel不起作用

[英]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 . 这可以单击一个Button ,但不能单击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.) (将光标移动到那里然后点击是不合适的,除非有办法从它的句柄中获取LinkLabel的坐标。即使那时我也不愿意这样做,除非没有别的办法。另外,这应该在仅限调用者的代码,不更改LinkLablel的应用程序代码。)

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. 诀窍是获取对实际链接标签的引用,以便您可以使用它实现IButtonControl接口的事实。

Edit 1 : how about that then: 编辑1 :那怎么样:

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) 编辑2 :这也应该有效(灵感来自David Heffernan的建议)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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