Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Well, I'm trying to code a gui for my engine, but I've got some problems. I know how to make a UI overlay but buttons are still black magic for me. Anything I tried was to compilcated ( if it goes big ). To Example I tried to look if the mouse position is the same as the Pixel that is showing the button. But If I use some bigger areas it's getting to complicated. Now I'm searching for a Tutorial how to implement your own gui. I'm really confused about it. Well I hope you have/ know some good tutorials.

By the way, I took a look at the DXUTSample, but it's to big to get overview.

share|improve this question
I know this question may have been asked couple times, but I'm too confused about it. So I'm searching for help here. – Julien Dec 14 '12 at 21:07
up vote 1 down vote accepted

So what I'm understanding is that you are looking for a quick way to test for GUI button presses?

The way I check is to have each button contain its own rectangle and check for a collision between the mouse and the button rectangle.

#include <windows.h>

class Button
     //The 'rect' variable is the 'Hotspot' for the button
     //Whether that is the whole button or just a select piece of it.
     RECT rect;

     //The constructor sets the 'rect' var accordingly.
     Button(RECT rect){this->rect = rect;};

     //Checks for collision between two RECTs
     bool RectCollision(RECT rectA, RECT rectB)
         if(rectA.left > rectB.right)
             return false;
         if(rectA.right < rectB.left)
             return false;
         if( > rectB.bottom)
             return false;
         if(rectA.bottom <
             return false;

         return true;

     //This function is ran if the mouse button is pressed
     //which is checked in your 'WindowProc' function.
     bool CheckForButtonPress()
          POINT mousePos;
          if(GetCursorPos(&mousePos)) //Gets the mousePos on the screen
              if(ScreenToClient(hwnd, &mousePos)) //Sets the mousePos relative to the window
                  RECT mouseRect = {mousePos.x, mousePos.y, 1, 1};
                  if(RectCollision(rect, mouseRect))
                      return true;
                  return false;

          return false;

Obviously you can do some optimizations and changes. But I hope you get the gist of it.

Sincerely -MasterBaldwin

share|improve this answer
Yeah, that's what I was searching for thumbs up Thank you – Julien Dec 14 '12 at 22:19
Your very welcome. Glad I could help :) – Mike Baldwin Dec 14 '12 at 22:24
I was just confused how to get collision test. Googled whole Google trough. Didn't find anything. German Google is crap. – Julien Dec 14 '12 at 22:28
Just a note if you're going to be using RECT, there is a PtInRect function, as well as a couple of others – bobobobo Dec 14 '12 at 23:33
Thank you, but saw it also now on MSDN. – Julien Jan 30 '13 at 17:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.