PostMessage没有点击按钮

时间:2015-09-29 03:33:58

标签: c++ winapi

我使用PostMessage只是点击看似有效的不同标签。但我尝试自动按下某些按钮,当运行此功能时,它会突出显示该按钮,就好像我将鼠标悬停在按钮上但没有点击一样。 我以为某种程度上按钮更改颜色使得布尔值为假,所以我添加了按钮颜色的异常,同时它会徘徊。没有区别,我不想使用SetCursorPos&使用SendInput模拟鼠标点击。我希望了解问题/问题我为什么不点击。

void click(const std::vector<uint_16>& x, const uint_16& y)
{
    for(uint_8 i = 0; i < 5; i++)
    {
        if(content::MyClass().firstMatch(GetPixel(hdc, x[i], y)))
        {
            PostMessage(hwnd, WM_LBUTTONDOWN, 0, MAKELPARAM(x[i], y));
            return;
        }
    }
    if(content::MyClass().secondMatch(GetPixel(hdc, x[4], y)))
    {
        PostMessage(hwnd, WM_LBUTTONDOWN, 0, MAKELPARAM(x[4], y));
    }
}

1 个答案:

答案 0 :(得分:4)

您使用的解决方案不可靠,因为您在窗口上短路输入系统而没有专门针对您尝试按下的按钮。

至于您的代码当前没有工作的原因,您只需向窗口发送WM_LBUTTONDOWN消息。由于大多数按钮都是WM_LBUTTONDOWNWM_LBUTTONUP的组合,因此您的程序不会导致按钮单击方法激活。

在鼠标按下后添加PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(x[i], y));将导致按钮单击进行注册。

将来作为一个更可靠的解决方案,专门针对窗口上的按钮并单击它,您可能需要查看BM_CLICK PostMessage参数。使用它而不是尝试模拟鼠标单击更为正确,因为Windows将触发在使用鼠标按下并鼠标上发布命令时可能会忘记的事件。

一个例子:

int retVal = 0;
HANDLE hwndDialog;
HANDLE hwndButton;

/* First, see if the dialog box (titled "Inactivity Warning" ) is currently open. */
hwndDialog = FindWindow( 0, "Inactivity Warning" );
if ( hwndDialog == 0 ) return;

/* Now get a handle to the "Resume" button in the dialog. */
hwndButton = FindWindowEx( hwndDialog, 0, 0, "Resume" );

/* After making sure that the dialog box is the active window, click "Resume". */
retval = SetActiveWindow( hwndDialog );

/* converted from SendMessage. */
retval = PostMessage( hwndButton, BM_CLICK, 0, 0 ); 

来源found Here,由我转换自VB。

有关输入系统Here is a good article的进一步阅读 Blog Post by Raymond Chen详细介绍了这些命令及其注意事项。

相关问题