C++ 检测鼠标按钮是否按下

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18318911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:53:48  来源:igfitidea点击:

Detect if mouse button is down

c++windowswinapimouseevent

提问by Jordan B

I am new to c++ and I am trying to activate a line of code onlywhen the left mouse button is held down. In this example, my code works but it seems that it just toggles it. When I click, it spams the Hkey then, when I click again, it stops.

我是 C++ 新手,我试图在按住鼠标左键时激活一行代码。在这个例子中,我的代码有效,但它似乎只是切换它。当我单击时,它会发送H密钥,然后当我再次单击时,它会停止。

Currently I have this code:

目前我有这个代码:

if ((GetKeyState(VK_LBUTTON)))
{
    keybd_event(VkKeyScan('H'),0,0,0);
    Sleep ( 30 );
}

Edit:

编辑:

I have inside the function:

我在函数里面有:

int WINAPI WinMain ( HINSTANCE hInst, HINSTANCE P, LPSTR CMD, int nShowCmd );

回答by pippin1289

Use this to determine if the button is pressed.

使用它来确定按钮是否被按下。

if((GetKeyState(VK_LBUTTON) & 0x100) != 0)

http://vcpptips.wordpress.com/tag/vk_lbutton/

http://vcpptips.wordpress.com/tag/vk_lbutton/

回答by alk

The application can catch messages and process being sent to your window indicating a state change of any mouse button.

该应用程序可以捕获发送到您的窗口的消息和进程,以指示任何鼠标按钮的状态更改。

When the left button is pressed a

当按下左键时

WM_LBUTTONDOWN

is sent.

已发送。

When it is released

当它被释放

WM_LBUTTONUP

is sent.

已发送。

Please read here for various messages being sent to indicate mouse events.

阅读此处了解发送的各种消息以指示鼠标事件

回答by Lyusten Elder

In first - need DEFINE BUTTON ID(or another object ID) in begin code:

首先 - 需要在开始代码中定义按钮 ID(或另一个对象 ID):

#define ID_BUTTON1      105

Then AFTER creationale of hWnd - we make button:

然后在创建 hWnd 之后 - 我们制作按钮:

   HWND HwndButton1 = CreateWindow( 
    L"BUTTON",  // Predefined class; Unicode assumed 
    L"OK",      // Button text 
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
    10,         // x position 
    10,         // y position 
    100,        // Button width
    100,        // Button height
    hWnd,     // Parent window
   (HMENU) ID_BUTTON1, // ID кнопки в меню
    NULL,            // Сущность мне неведомая 8-)
    NULL);         // Pointer not needed.

And then add trigger in function:

然后在函数中添加触发器:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId=0, wmEvent;  //wmId NEED DEFINE null - if he is not available in event, else be ашипка
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmEvent = HIWORD(wParam);  //  Name of EVENT - имя события
        wmId    = LOWORD(wParam);  // ID element for event - элемент с которым оно случилось

        case WM_LBUTTONDOWN: MessageBox(NULL, L"MouseL_Click", L"WndHeader", MB_OK | MB_ICONEXCLAMATION);  // Left Mouse Button pressed

        if( LOWORD(wParam) == 105 && WM_COMMAND == WM_LBUTTONDOWN){ // Клик по ID_BUTTON1 левым мышком
        EndDialog(hWnd,0);
        }
................  // Many another function
}