Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [Win32] How draw a rectangle on the desktop

[Win32] How draw a rectangle on the desktop

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 2965471
    wrote on last edited by
    #1

    Hi, I'm trying to draw a rectangle on the screen with MouseHook, but i can't:

    LRESULT CALLBACK MouseProc(int nCode,WPARAM wParam,LPARAM lParam)
    {
    LPMOUSEHOOKSTRUCT info=(LPMOUSEHOOKSTRUCT) lParam;
    static BOOL capturing = 0;
    Pen p(Color(255,255,0,0),5);
    Graphics g(GetDC(0));

    switch(wParam)
    {
    case WM_LBUTTONDOWN:
    capturing = TRUE;
    rcCaptured.left = info->pt.x;
    rcCaptured.top = info->pt.y;
    break;
    case WM_LBUTTONUP:
    if( capturing )
    {
    rcCaptured.right = info->pt.x;
    rcCaptured.bottom = info->pt.y;
    capturing = FALSE;
    }
    break;
    case WM_MOUSEMOVE:
    InvalidateRect(GetDesktopWindow(),NULL,FALSE);
    g.DrawRectangle(&p,rcCaptured.left,rcCaptured.top,info->pt.x-rcCaptured.left,info->pt.y-rcCaptured.top);
    break;
    }
    }

    how can force repaint of the screen? and how remove hook after select a rectangle?

    M B 2 Replies Last reply
    0
    • M Member 2965471

      Hi, I'm trying to draw a rectangle on the screen with MouseHook, but i can't:

      LRESULT CALLBACK MouseProc(int nCode,WPARAM wParam,LPARAM lParam)
      {
      LPMOUSEHOOKSTRUCT info=(LPMOUSEHOOKSTRUCT) lParam;
      static BOOL capturing = 0;
      Pen p(Color(255,255,0,0),5);
      Graphics g(GetDC(0));

      switch(wParam)
      {
      case WM_LBUTTONDOWN:
      capturing = TRUE;
      rcCaptured.left = info->pt.x;
      rcCaptured.top = info->pt.y;
      break;
      case WM_LBUTTONUP:
      if( capturing )
      {
      rcCaptured.right = info->pt.x;
      rcCaptured.bottom = info->pt.y;
      capturing = FALSE;
      }
      break;
      case WM_MOUSEMOVE:
      InvalidateRect(GetDesktopWindow(),NULL,FALSE);
      g.DrawRectangle(&p,rcCaptured.left,rcCaptured.top,info->pt.x-rcCaptured.left,info->pt.y-rcCaptured.top);
      break;
      }
      }

      how can force repaint of the screen? and how remove hook after select a rectangle?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I'm guessing your InvalidateRect() call is causing a repaint that ends up covering up what you just drawed to the screen. What is happening with your current code??

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        I'm guessing your InvalidateRect() call is causing a repaint that ends up covering up what you just drawed to the screen. What is happening with your current code??

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M Offline
        M Offline
        Member 2965471
        wrote on last edited by
        #3

        InvalidateRect doesn't work, the screen is still dirty.

        M 1 Reply Last reply
        0
        • M Member 2965471

          InvalidateRect doesn't work, the screen is still dirty.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Yes, you'd have to set the erase background flag to true in the call and call UpdateWindow() to force an immediate paint. That worked on older OSs but on Windows 7 the updates don't happen while you have the mouse button pressed it seems. It does work on the LBUTTONUP message (just tested it). You may have to double buffer - grab the image for the rect from the screen, draw your rectangle, draw the captured image back to the screen to erase, etc.

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • M Member 2965471

            Hi, I'm trying to draw a rectangle on the screen with MouseHook, but i can't:

            LRESULT CALLBACK MouseProc(int nCode,WPARAM wParam,LPARAM lParam)
            {
            LPMOUSEHOOKSTRUCT info=(LPMOUSEHOOKSTRUCT) lParam;
            static BOOL capturing = 0;
            Pen p(Color(255,255,0,0),5);
            Graphics g(GetDC(0));

            switch(wParam)
            {
            case WM_LBUTTONDOWN:
            capturing = TRUE;
            rcCaptured.left = info->pt.x;
            rcCaptured.top = info->pt.y;
            break;
            case WM_LBUTTONUP:
            if( capturing )
            {
            rcCaptured.right = info->pt.x;
            rcCaptured.bottom = info->pt.y;
            capturing = FALSE;
            }
            break;
            case WM_MOUSEMOVE:
            InvalidateRect(GetDesktopWindow(),NULL,FALSE);
            g.DrawRectangle(&p,rcCaptured.left,rcCaptured.top,info->pt.x-rcCaptured.left,info->pt.y-rcCaptured.top);
            break;
            }
            }

            how can force repaint of the screen? and how remove hook after select a rectangle?

            B Offline
            B Offline
            bob16972
            wrote on last edited by
            #5

            Deja Voodoo. It almost feels like I posted an answer[^] to this problem some days back? Use SetROP2 to rubberband it. Yes, you can mix GDI and GDI+ together in the same application. You just need to understand how each works to make them complement each other.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups