如何检查是否在C ++中按下了按钮 [英] How to check if a button is being pressed in C++

查看:118
本文介绍了如何检查是否在C ++中按下了按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在窗口中创建一个常规窗口(而不是DOS窗口),我想检查是否在.cpp文件中按下了键盘和屏幕上的按钮"(窗口项设置在.cpp文件中). .h文件),如下所示:

I am making a regular window in windows (not a DOS window) and I want to check if a keyboard and a "on-screen button" is being pressed in a .cpp file (the window items are set up in a .h file), something like this:

while(true)
{
     if(keyboard button 1 is being pressed)
     {
           variable = variable + 1;
     }
}



或:



or:

while(true)
{
     if("on-screen button" is being pressed)
     {
           variable = variable + 1;
     }
}


.

另外,如何在标签中打印变量?

[修改:添加了前置标记...他们是您的朋友!]


.

Also, how can i print a variable in a label?

[Modified: added pre tags...they are your friend!]

推荐答案

Windows应用程序的工作方式有所不同,因为它是事件驱动的. 您不会像以前那样继续轮询并检查属性.

因此,在Win32应用程序的情况下,通常会有一个对话框过程;在MFC应用程序的情况下,如果有单击按钮,通常会有一个回调函数.在这里,您可以编写单击按钮时需要完成的代码.

要在标签中显示变量的值,可以使用SetWindowText API.
Windows applications work differently in that it is event driven.
You do not keep polling and check for attributes as you have done.

So you generally have a dialog procedure in case of a Win32 application or a callback function in case of an MFC application for a button click. Here you write the code that needs to be done when the button is clicked.

To show the value of a variable in a label you can use the SetWindowText API.


您最好学习一些基本的Windows编程. http://www.winprog.org/tutorial/ [ http://msdn.microsoft.com/en-us/library/ms646293 (v = vs.85).aspx [
You better learn some basic windows programming. http://www.winprog.org/tutorial/[^]
anyway you could know the key state using GetAsyncKeyState function

http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx[^]


正如超人所说,Windows编程是事件驱动的.为了能够完全捕获这些事件,您需要注册为应用程序,创建消息传递循环并在某个可以接收消息的地方创建可寻址窗口.在无限循环中检查状态可能不是解决问题的最佳方法.
As Superman already stated, Windows programming is event driven. In order to be able to catch those events at all, you need to register as an application, create a messaging loop, and create an addressable Window somewhere that can accept the messages. Checking a state in an infinite loop is probably not the best way to approach your problem.


这篇关于如何检查是否在C ++中按下了按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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