Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 2
LVL 45

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

580 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