C ++执行导致监视器断开连接 [英] C++ execution causes monitor to disconnect

查看:117
本文介绍了C ++执行导致监视器断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是执行此代码后,它会单击,然后我的监视器变成蓝色并显示"HDMI没有连接电缆",或者在1秒钟后恢复正常,或者停留在该屏幕上直到我执行了一项操作(点击,alt等).

the issue I'm having is that upon executing this code, it clicks, and then my monitor turns blue and says "HDMI no cable connected", and either returns back to normal after 1 second, or stays on that screen until I perform an action (click, alt etc).

任何对此有任何想法的人都将不胜感激.

Anybody with any idea on why this is happening would be appreciated.

#include "stdafx.h"
#include <Windows.h>

using namespace std;

void function() {
    INPUT Input;
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    SendInput(true, &Input, sizeof(Input));
    Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(true, &Input, sizeof(Input));
};


int main()
{
    Sleep(3000);
    function();
    return 0;
}

此外,我正在使用Visual Studio 2017社区.调试代码和运行可执行文件时,会发生此问题.我的代码没有错误或警告.

Also, I'm using Visual Studio 2017 Community. This problem occurs when debugging the code, and when running the executable. There are no errors or warnings with my code.

推荐答案

您没有初始化未定义的行为.使用以下方法将结构字段初始化为零:

You are not initializing the INPUT structure properly thus (probably) invoking undefined behavior. Initialize the structure fields to zeros using:

INPUT Input = {};

您还可以使用包含两个元素的数组:

You can also go with an array of two elements:

INPUT Input[2] = {0};

后跟一个:

Input[0].type = INPUT_MOUSE;
Input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
Input[1].type = INPUT_MOUSE;
Input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;

和一个呼叫 SendInput :

SendInput(2, Input, sizeof(INPUT));

这篇关于C ++执行导致监视器断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆