Hi,

I created an object to create buttons in console applications. But somehow, the program doesn't do much when it's checking if the button is clicked. I got this code from the internet, I could give the original code if you want to study it. I have stripped it down to what I needed and modyfied it a bit. This is my object code:

using namespace std;

void color(int foreground,int background);
void cursor(int X,int Y);



class button
{
      public:
             button(int button_X,int button_Y,char* buttonMessage,int button_Xe);
             ~button(){};
             bool buttonClicked();
             void show();
      private:
              int buttonX;
              int buttonY;
              int buttonXe;
              char* message;
              int leng;
};

button::button(int button_X,int button_Y,char* buttonMessage,int button_Xe)
{
             buttonX = button_X;
             buttonXe = button_X + button_Xe + 1;
             buttonY = button_Y;
             message = buttonMessage;
             
}

void button::show()
{
             color(fwhite|fint,0);
             cursor(buttonX-1,buttonY);
             cout << " " << message << " ";

}

bool button::buttonClicked()
{
    HANDLE hIn;
    HANDLE hOut;
    DWORD EventCount;
    int LoopCount = 0;
    int KeyEvents = 0;
    INPUT_RECORD InRec;
    DWORD NumRead;

    hIn = GetStdHandle(STD_INPUT_HANDLE);
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);


    GetNumberOfConsoleInputEvents(hIn,
                                      &EventCount);
        while (EventCount > 0)
        {
            ReadConsoleInput(hIn,
                             &InRec,
                             1,
                             &NumRead);

//Stops here, maybe there is something wrong with InRec..

            if (InRec.EventType == MOUSE_EVENT)
            {
                if (InRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
                {
                        int loop=1;
                        while (loop < buttonXe+1){
                        if (InRec.Event.MouseEvent.dwMousePosition.X==buttonX+loop && InRec.Event.MouseEvent.dwMousePosition.Y==buttonY) return true;
                        loop++;
                        }
                }  
            }
            GetNumberOfConsoleInputEvents(hIn,&EventCount);
         }
         return false;
}

The color() and cursor() functions change color and cursorposition. The headers are in another .h file.
In the cpp file, I made this loop to check the output of the buttonClicked() funtion.

#include <iostream>
#include <windows.h>
#include "object.h"

int main()
{
button myB(40,14,"BUTTON",6);
    
    myB.show();
    
    
    label:{
    cout << myB.buttonClicked() << endl;
    }goto label;
return 0;
}

This somehow returns nothing. Could someone take a closer look to the object funtion and tell me what's wrong?

Im using Dev-cpp, OS is windows Vista.

I've been testing some and I found out that the loop in int main() is only runned twice. This was the loop I used:

while (!myB.buttonClicked()){
cout << "loop ";
}

I tested some more and I think it's just inpossible to make a loop like this with an object... Please tell me if you know anything that could help me out.


someone please respond..:S

Ok...after much headaches, I finally did it.

in my main() function, there was a system("title hey"); what makes the title of the window "hey". This didn't go with my buttonclass.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.