Sign in to follow this  
rob64464

[.net] Detecting Mouse Clicks the easy way

Recommended Posts

Hello, I am making a Word Game in GDI+ and I have a few questions about detecting which word was clicked. I have an event handler to detect the mouse click and that works fine but how can i detect if the letter has been clicked? I have tried: if (x>=130 && x<=280 && y>70 && y<=370) { //they have clicked a square } Well that only partly works and that is the very very long way around things. Is there any easier way because this only works if the form is maximized. Moving the form will mess everything up completley!

Share this post


Link to post
Share on other sites
Well I don't know much about GDI+, but try having each letter the same width and height. Then have a RECT around each word, then just scan each letter rect and just increment the letter width.

i.e.

#define LETTER_WIDTH 50
#define LETTER_HEIGHT 50

typedef struct tagWord {

string m_strWord; // the word to display
RECT m_Bounds; // the words bounds

}WORD, *LPWORD;

// this computes what letter was clicked on
// it takes the work to check and the mouse coords
// it returns the index of the letter clicked on, -1 if no letter clicked on
int CheckLetter( LPWORD pWord, int x, int y )
{
if ( !pWord )
return -1;

int nX = pWord->m_Bounds.x, nY = pWord->m_Bounds.y; // temps

// iterate thru the letters and see what was clicked on
int i = pWord->m_strWord.size( );
for ( int i = 0; i < nNum; i++ )
{
// check the letter click
if ( x >= nX && x < nX && y >= nY && y < nY )
return i;

// move to the next letter
nX += LETTER_WIDTH;
} // end for i

return -1;
} // end CheckLetter

Share this post


Link to post
Share on other sites
How about this


using System;
using System.Drawing;

public class WordCrossword
{
struct Word
{
string word;
Rectangle wordRect;
}
// Fill up the Word structure with values for both
// attributes.

public const int maxWords = 60;

public int CheckLetter(Word[] tempWord, int x, int y)
{

for(int i = 0; i < maxWords; i++)
{
if(tempWord[i].wordRect.Contains(new Point(x, y)) )
{
// Found word
}

}

}

public static void Main(string[] args)
{
// ...
WordCrossword app = new WordCrossWord();
app.Run();
}

public void Run()
{
Word[] word = new Word[maxWords];

// Put in mouse down event function
if(CheckLetter(word, x, y) > 0)
{
//...
}

}

}





Anyways you get the idea..

Share this post


Link to post
Share on other sites
In that struct put an unsigned int and each letter has a unique Id say 1-26.

That should work! If your using bitmaps. Do you have 26 individual bitmaps, or one bitmap with 26 letters in it?

If there individual just use the bitmap name as an identifier.

Share this post


Link to post
Share on other sites
I still cannot get this working :'(.

Are there any examples out there that provide efficent,logical ways to detect clicks?

Thanks,
Rob

Share this post


Link to post
Share on other sites
What's wrong with khalligan's example? It should fit just fine into what you're trying to do. It's just a matter of applying the concept he showed of grouping each word (or letter) into a Rectangle struct and checking for a point-to-rect collision using one of the built-in functions.

Share this post


Link to post
Share on other sites
I cannot get

public int CheckLetter(Word[] tempWord, int x, int y)
{

for(int i = 0; i < maxWords; i++)
{
if(tempWord[i].wordRect.Contains(new Point(x, y)) )
{
// Found word
}

}

}

to compile in C#. Any ideas why?

Share this post


Link to post
Share on other sites
Quote:
Original post by rob64464
I cannot get

public int CheckLetter(Word[] tempWord, int x, int y)
{

for(int i = 0; i < maxWords; i++)
{
if(tempWord[i].wordRect.Contains(new Point(x, y)) )
{
// Found word
}

}

}

to compile in C#. Any ideas why?


If I were to take a blind stab in the dark (seeing as you didn't really specify what's going wrong), I'd say that you haven't created anysort of Word class that he used in an example-esque manner. This Word class should have a Rectangle that is set with the bounds of the whole word (pixels), and by this example should be stored in somesort of array. For example:


class Word
{
Rectangle wordRect;
Bitmap wordImage; // Assuming each word is a bitmap, otherwise just write text

Word(Rectangle initRect)
{
wordRect = initRect;
}

public void Draw(int x, int y, Graphics G)
{
G.DrawImage(wordImage, x, y); // Forgot if this is the exact syntax for DrawImage() -- sorry! :/
}
}

// Somewhere else...
Word[] myWords = new Word[100]; // An ArrayList would probably be a more elegant way :P




You have to understand that nobody can write example code in perfect context with your code, without seeing how you're organizing/structuring things. It's up to you to take the theory/examples we've provided and apply it to your existing code. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this