如何根据win32代码块中的任何输入文本进行按钮响应? [英] How do I make a button respond according to any input text in win32 codeblocks?

查看:81
本文介绍了如何根据win32代码块中的任何输入文本进行按钮响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究这个项目。我希望用户输入特定数字(如'8123')并点击'显示计数',它会输出一条消息,例如'你有8123'。此外,如果用户输入'812'并点击'显示计数',则应输出'这是数字812'。



I have this project I am working on. Where I want the user to input a specific number like '8123' and clicks on 'Show Count', it outputs a message like 'You have 8123'. Also if the user inputs '812' and clicks on 'Show Count', it should output 'This is number 812'.

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
 char str1[5], str2[5];


HWND TextBox;


          case WM_CREATE: {

            HWND hWndTextBox   =  CreateWindow (TEXT("EDIT"), TEXT (""),
                WS_VISIBLE | WS_CHILDWINDOW | WS_BORDER | WM_GETTEXT,
                100, 20, 130, 20,
                hwnd,(HMENU)  NULL, NULL, NULL);


               CreateWindow (TEXT("BUTTON"), TEXT ("Process "),
                WS_VISIBLE | WS_CHILD ,
                200, 100, 70, 20,
                hwnd,(HMENU) 1, NULL, NULL);

  if (
        SendMessage  (
        hWndTextBox  ,
        (UINT) CB_ADDSTRING,
        (WPARAM) 0, (LPARAM) TEXT ("8123" )));


        {

            ::MessageBox(hwnd, "You have 8123" , "Show Count",  MB_OK | MB_ICONINFORMATION);

        }

        if (
        SendMessage  (
        hWndTextBox  ,
        (UINT) CB_ADDSTRING,
        (WPARAM) 0, (LPARAM) TEXT ("812" )));


        {

            ::MessageBox(hwnd, "This is number 812" , "Show Count",  MB_OK | MB_ICONINFORMATION);

        }



   return 0;
         }
                     break;





我的问题就是这样,如果用户第一次打开应用程序,那么首先会出现你有8123的命令,甚至没有任何输入。最糟糕的是,当用户输入数字并点击显示计数时,没有弹出消息。好像还不够,它适用于用户输入的所有输入(8123,812)。我被告知发送'WM_GETTEXT'然后'SendMessage'功能,但是,我认为我没有把它们传递好。还有一件事,我不知道如何使用strcmp作为旁白。我正在使用Code :: Blocks win32任何帮助都会非常感激。



My problem is that, if a user opens the application for the first time, there command comes first as in 'Your have 8123' without even any input. Worst of all, when the user inputs the numbers and clicks 'Show Count', there is no pop-up message. As if not enough, it applies to all inputs (8123, 812) the user would type in. I am told to send a 'WM_GETTEXT' then 'SendMessage' function, however, I think I did not pass them well. One more thing, I do not know how to use strcmp as an aside. I am using Code::Blocks win32 Any help would be most appreciating.

推荐答案

首先,处理按钮单击/按下。较低级别的处理已由按钮的Windows类完成。消息 WM_COMMAND 被发送到其父窗口。请参阅:按钮消息(Windows) [ ^ ]。



当用户点击按钮时,其状态发生变化,按钮发送通知代码,形式为: WM_COMMAND 消息。查看通知代码的任何页面。你会看到 wParam (32位)包含按钮的控制标识符作为低位字和通知代码作为高位字,并且 lParam 包含按钮的句柄(在您的情况下为复选框)。



这样,您可以捕获消息并处理它。您已经开始这样做,只需删除不良代码,例如案例1 ;通常,始终删除所有硬编码的立即常量。请记住,其他控件可以发送相同的消息,因此您必须如上所述识别它们。



现在,您可以通过发送文本输入控件的内容来阅读它消息 WM_GETTEXT

WM_GETTEXT消息(Windows) [ ^ ],

SendMessage函数(Windows) [ ^ ]。



根据结果组织您的逻辑。再次,避免使用硬编码的魔术字符串。这就是全部。



关于你工作的几点说明。你运用纯粹的原始Windows API开发是件好事;它有助于理解事物的发展方式。但人们很难使用原始API(不是面向对象的)来开发应用程序。相反,他们使用它来开发面向对象的库,这些库包含基本的Windows机制,例如消息发送,发布和处理,以及许多Windows类。这种中间产品节省了大量时间,只是毫无意义的手工劳动。如果您使用一些可用的库,这是一回事,但如果您不使用任何第三方,我也可以理解它。那么你自己的面向对象的库可以节省大量的开发时间和枯燥的劳动力,除非你计划只开发1-2个应用程序。 :-)



-SA
First, handling a button click/press. The lower-level handling is already done by the button's Windows class. The message WM_COMMAND is sent to its parent window. Please see: Button Messages (Windows)[^].

"When the user clicks a button, its state changes, and the button sends notification codes, in the form of WM_COMMAND messages." Look at any page on notification code. You will see that wParam (32 bits) contains the button's control identifier as the lower word and notification code as the higher word, and lParam contains a handle to a button (check box, in your case).

This way, you can capture the message and handle it. You already started to do it, only you have to remove bad code, such as case 1; generally, always remove all hard-coded immediate constants. Remember that other controls can send the same message, so you have to identify them as described above.

Now, you can read the content of text input control by sending it a message WM_GETTEXT:
WM_GETTEXT message (Windows)[^],
SendMessage function (Windows)[^].

Organize your logic depending on results. Again, avoid hard-coded magic strings. That's all.

Just few notes on your work. It's good that you exercise pure raw Windows API development; it helps to understand how things development. But people hardly use raw API, which is not object-oriented, for development of application. Rather, they use it for development of object-oriented libraries which wrap both basic Windows mechanisms, such as message sending, posting and handling, as well as many Windows classes. Such intermediate product save a lot of time and just pointless "manual" labor. If you use some available library, this is one thing, but if you don't use anything 3rd-party, I can understand it, too. Then your own object-oriented library can save a lot of development time and boring labor, unless if you plan to develop just 1-2 applications. :-)

—SA


为什么要将CB_ADDSTRING发送到文本框?

普通旧的SetWindowText有什么问题?



你说
Why are you sending CB_ADDSTRING to text box?
What is wrong with plain old SetWindowText?

And You say "
if a user opens the application for the first time, there command comes first as in 'Your have 8123' without even any input. Worst of all, when the user inputs the numbers and clicks 'Show Count', there is no pop-up message.





,因为你有代码在WM_CREATE事件中显示弹出窗口..对于按钮事件,因为上一个答案说你必须处理WM_COMMAND事件。



一般来说你需要阅读有关win32编程的更多信息。 Win32教程 - 第1课:Win32简介 [ ^ ]是一个很好的起点。



because you have code to show popup in WM_CREATE event.. Also for button events as previous answer said you have to handle WM_COMMAND event.

In general you need to read more about win32 programming. Win32 Tutorial - Lesson 1: Introduction to Win32[^] is a good place to start.


这篇关于如何根据win32代码块中的任何输入文本进行按钮响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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