CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2004
    Posts
    204

    GDI+ and Rubberband/Selection Rectangle

    I cannot find how to draw a simple selection rectangle (rubberband) using
    GDI+.

    This should be a simple thing, but I'm beginning to wonder if it is even possible.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: GDI+ and Rubberband/Selection Rectangle

    It's so simple then is not needed GDI+ for doing it (although you are using GDI+ for other stuff).
    Here is an example using "classic" GDI functions:
    Code:
    void _DrawRubberBand(HWND hWnd, RECT& rc)
    {
       HDC hDC = ::GetDC(hWnd);
       HBRUSH hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
       HBRUSH hOldBrush = (HBRUSH)::SelectObject(hDC, hBrush);
       ::SetROP2(hDC, R2_NOT);
    
       ::Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
    
       ::SelectObject(hDC, hOldBrush);
       ::ReleaseDC(hWnd, hDC);
    }
    And here is another one
    Code:
    void _DrawRubberBand(HWND hWnd, int left, int top, int right, int bottom)
    {
       HDC hDC = ::GetDC(hWnd);
       ::SetROP2(hDC, R2_NOT);
       ::MoveToEx(hDC, left, top, NULL);
       POINT pt[4] = {{right, top}, {right, bottom}, {left, bottom}, {left, top}};
       ::PolylineTo(hDC, pt, 4);
       ::ReleaseDC(hWnd, hDC);
    }
    Of course, you have to further handle WM_LBUTTONDOWN, WM_MOUSEMOVE and WM_LBUTTONUP messages, in order to draw a selection rectangle on user action.
    Last edited by ovidiucucu; November 27th, 2011 at 08:25 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Apr 2004
    Posts
    204

    Re: GDI+ and Rubberband/Selection Rectangle

    I for some reason didn't think you could mix GDI and GDI+

    Curiosity then, this cannot be done using GDI+ alone?

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: GDI+ and Rubberband/Selection Rectangle

    Like any other library, GDI+ could not implement everything our muscles want.
    If something is missing in there, then one ultimate solution is to use native Windows API.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured