Mouse Event

This is a discussion on Mouse Event within the C++ Programming forums, part of the General Programming Boards category; Just trying to handle a simple left-click. Code doesn't work. Tried everything. Code: #include<iostream> #include<cstdlib> #include<cstring> #include<windows.h> using namespace std; ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Mouse Event

    Just trying to handle a simple left-click. Code doesn't work. Tried everything.


    Code:
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<windows.h>
    using namespace std;
    
    #define WHITE  BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY
    #define RED    FOREGROUND_RED|FOREGROUND_INTENSITY	
    #define BLACK  0
    
    typedef struct card
    {
    	unsigned int face_value;
    	char suit;
    	card *next;
    
    	card(){next=NULL;}
     
    }*mycard;
    
    
    void gotoxy(int x, int y);
    void paint_cards(HANDLE, card*);
    
    
    int main()
    {
        system("cls");
    
        HANDLE hIn;
        HANDLE hOut;
       
        int LoopCount = 0;
        int KeyEvents = 0;
        INPUT_RECORD InRec;
        DWORD NumRead;
    
    	hIn  = GetStdHandle(STD_INPUT_HANDLE );
    	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    	
    
    	mycard new_card = new card;
    
    	srand(GetTickCount());
    
    	system("color 20");
    	
    	paint_cards(hOut, new_card);
    
    
        for(;;)
        {
    	ReadConsoleInput(hIn, &InRec, 1, &NumRead);
    
    		if (InRec.EventType == MOUSE_EVENT)            
    
    			if (InRec.Event.MouseEvent.dwEventFlags == 0)                
                    
                                   if (InRec.Event.MouseEvent.dwButtonState & 0x01)
                                   {  
    					system("color 70");
    					system("cls");
    					cout << "\a\tIt works!!";						
    		               } 
         }
     
    
        return 0;
    }
    
    
    void paint_cards(HANDLE hOut, card* new_card)
    {
    	int cx=5, cy=5;
    
    	for(;cx<46;cx+=10)
    	{
    		
    		new_card->face_value = static_cast<unsigned>(rand()%8+2);
    
    		new_card->suit = static_cast<char>(rand()%4+3);	
    
    		SetConsoleTextAttribute(hOut, new_card->suit<5?WHITE|RED:WHITE|BLACK);
    
    		gotoxy(cx, cy+0);	cout << endl << endl << endl;
    		gotoxy(cx, cy+1);	cout << new_card->face_value	<< new_card->suit << "   \n";
    		gotoxy(cx, cy+2);	cout << "     \n"; 
    		gotoxy(cx, cy+3);	cout << "  "  << new_card->suit << "  \n";
    		gotoxy(cx, cy+4);	cout << "     \n"; 
    		gotoxy(cx, cy+5);	cout << "   " << new_card->suit << new_card->face_value;
    		gotoxy(cx, cy+6);	cout << endl  << endl  << endl  << endl;	
    
    		new_card = new card;
    
    	}
    
    	SetConsoleTextAttribute(hOut, WHITE|BLACK);
    
    	gotoxy(10, 16);		cout << "         "; 
    	gotoxy(10, 17);		cout << " Hit Me! ";
    	gotoxy(10, 18);         cout << "         ";
    
    	cout << endl << endl << endl;
    }
    
    
    
    void gotoxy(int x, int y)
    {
      COORD coord;
      coord.X = x;
      coord.Y = y;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    Last edited by The Brain; 06-01-2005 at 06:12 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Last edited by xErath; 06-01-2005 at 10:38 PM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,668
    I had to be in full screen mode for the mouse events to work on my Win2K machine.

    gg

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    You know.. It was those pesky system( ) calls..!! For some reason, they screw everything up..! and when I took them out.. code works perfect..!!!

    (i was just using system() as a cheap and easy solution until i got the program done.. and then go back and optimize/make more c++ standard compiant)


    :: props to adrianxw ::
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intercept mouse event and change to different event
    By ByThaBay in forum Windows Programming
    Replies: 1
    Last Post: 03-19-2008, 05:44 PM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  3. How to do a mouse over event on a button?
    By GUIPenguin in forum C# Programming
    Replies: 1
    Last Post: 07-06-2006, 10:34 AM
  4. mouse event when resizing window
    By SuperNewbie in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2004, 05:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21