How to use the mouse event handler in GDI+ under C++?

Posted on 2010-01-10
Medium Priority
Last Modified: 2013-12-21

I am new to Windows programming & have no experience with GDI(+), which is a consequence of not having experience with Windows programming... I have to finish an application that is running under GDI+ by adapting an existing subprogram to the new functional specification.

The subprogram I am going to change is called INHIB, the new subprogram to be created by me is called VRAG. So, I have added a VRAG project with some minor modifications to the INHIB (e.g. by renaming the INHIB.DAT to VRAG.DAT).

INHIB is using the keyboard as the input device. The user clicks the <CR> key to proceed from one trial to the other. In VRAG, the new subprogram to be created by me, the input device must be the mouse.

I have no time to go through details of Windows programming or read a lot about GDI+, as I have to finish some primordial program till Friday next week. So, I need urgent help!

What would help me best? I simple Microsoft Visual Studio project that shows in a simple example how to draw a rectangel by using the mouse, with all #includes, DLLs, resources, but as simple as possible. A hint where I can find a RUNNABLE code would be a welcome answer. Of course, you could create your own set of project files and put them in a ZIP for me. That would be another solution and as good as the first IF it works immediately. :-)

Again, I have no time to go through tutorials that take more than 20 minutes or so...

Technically, the application is a GDI+ "game". It will not be used as such, but technically, it is like programming a non-graphic-intensive game.

Best regards to the expert!
Question by:Ahmet Ekrem SABAN
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 44

Expert Comment

ID: 26277974
>>Again, I have no time to go through tutorials that take more than 20 minutes or so...

Maybe you ought to have - have no understanding of what you are doing is a recipe for disaster.

Author Comment

by:Ahmet Ekrem SABAN
ID: 26278183
OK, you are right.  I have done this things in Java with listeners. All in all, I have more than ten years of OOP experience.
But how is the same event handling working in C++/Windows/GDI+? Any simple examples from where I can learn it in a short period?

Author Comment

by:Ahmet Ekrem SABAN
ID: 26281862
Which tutorials are the best to learn event handling (for a mouse) on the net?

Accepted Solution

Altwies earned 1000 total points
ID: 26304842

The following snippet of code should get you a long way into how to draw data into a Windows DC using Rectangle in the GDI. There are a lot of variations on what you can do depending on your application. This is only one implementation for you to run with.

// Function DrawRubberBand
//      Inputs are       hWnd      -      Handle to the Window
//                  lpP1      -      Pointer to start point of a rectangle
//                  lpP2      -      Pointer to end point of a rectangle
//                  X      -      New Window Horizontal position
//                  Y      -      New Window Vertical position
//      Results            Rectangle with Video inverted in window
//                  X & Y update the contents of the end point
void DrawRubberBand(HWND hWnd, POINT *lpP1, POINT *lpP2, int X, int Y)
    int   OldMixMode;
    HDC   hDc;

    // Create red dash pen
    HPEN hPen = CreatePen( PS_DOT, 1, COLORREF(RGB(255,255,255)) );
    HPEN hOldPen;
    hDc = GetDC(hWnd);

    hOldPen = (HPEN) SelectObject(hDc, hPen);

    OldMixMode = SetROP2(hDc, R2_XORPEN);

    //Is there a real rectangle
    if ((lpP1->x != lpP2->x) || (lpP1->y != lpP2->y))
        // Erase Old Rectangle
        Rectangle(hDc, lpP1->x, lpP1->y, lpP2->x, lpP2->y);

    // Draw New Rectangle
    lpP2->x = X;
    lpP2->y = Y;
    Rectangle(hDc, lpP1->x, lpP1->y, lpP2->x, lpP2->y);

    SetROP2(hDc, OldMixMode); // Restore Old Mix Mode
    SelectObject(hDc, hOldPen);



Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question