利用SendMessage模拟鼠标左键双击打开文件,但是文件没打开,请熟悉Windows编程的大神指点

利用SendMessage模拟鼠标左键双击打开文件,但是文件没打开。打印4次结果0。请熟悉Windows编程的大神指点

// SendMessage.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "stdlib.h"
#include "Winuser.h"

int main(int argc, char* argv[])
{
    HWND desktop = GetDesktopWindow();//屏幕窗口句柄
    desktop=GetWindow(desktop,GW_CHILD);
    /*8DWORD ProcessId;//要打开窗口的进程标识符
    ::GetWindowThreadProcessId(desktop, &ProcessId);
    HANDLE processH = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);*/
    int x = 30;
    int y = 24;
    ::SetCursorPos(x,y);
    //Sleep(1000);
    LRESULT result1 = ::SendMessage(desktop, WM_LBUTTONDOWN,VK_LBUTTON,MAKELPARAM(x,y));
    LRESULT result2 = ::SendMessage(desktop, WM_LBUTTONUP,0,MAKELPARAM(x,y));
    LRESULT result3 = ::SendMessage(desktop, WM_LBUTTONDOWN,VK_LBUTTON,MAKELPARAM(x,y));
    LRESULT result4 = ::SendMessage(desktop, WM_LBUTTONUP,0,MAKELPARAM(x,y));
    printf("%d %d %d %d\n",result1,result2,result3,result4);
    //::SendMessage(NULL, WM_LBUTTONDOWN, 0,(y<<16)|x);
    //::SendMessage(NULL, WM_LBUTTONUP, 0,(y<<16)|x);

    return 0;
}

3个回答

1.把SendMessage 改成PostMessage
2.在两次点击中间插入sleep
3.以上两个方法不行,就用单击然后发送回车按键

不需要这种模拟,直接用 ShellExecute 就可以实现打开文件了。

识别双击的本质是识别两次单机的间隔和点击的区域是否在类同区域,可以试试发送消息WM_LBUTTONDBLCLK

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!