Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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'.

C++
/*  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;


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.
Posted
Updated 3-Feb-16 4:20am
v4

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
 
Share this answer
 
v2
Comments
Member 12284029 3-Feb-16 9:53am    
@Sergey Thanks for the reply. However, I have tried using WM_GETTEXT and SendMessage function but, it seems it does not still work out. Thanks for encouraging me though.
Sergey Alexandrovich Kryukov 3-Feb-16 9:59am    
If does work. Can you show how did you try it?
—SA
Member 12284029 3-Feb-16 9:54am    
I have updated the code above too.
Why are you sending CB_ADDSTRING to text box?
What is wrong with plain old SetWindowText?

And You say "
C#
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.


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900