How to use InvalidateRect+Application.DoEvents without slowing down application?












0















I want to draw rectangle on the desktop as I move mouse cursor. Old rectangle needs to be gone before drawing new one.
However InvalidateRect will occur after the new rectangle is drown so all rectangles are gone. So I had to add Application.DoEvents() to process InvalidateRect before draw new rectangle. But it slows down my application. Is there a better way to do this?



void mouseHook_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = (Cursor.Position.X + " : " + Cursor.Position.Y);

if (start == true)
{
IntPtr desktopPtr = GetDC(IntPtr.Zero);
System.Drawing.Graphics g = System.Drawing.Graphics.FromHdc(desktopPtr);

Rectangle bb = new Rectangle(1, 1, 1, 1);

bb = DrawRec(Cursor.Position.X, Cursor.Position.Y, desktopPtr, g);//getting new rectangle to draw on the desktop, rectangle topleft position is fixed
Application.DoEvents();
g.DrawRectangle(new Pen(Color.Red, 1), bb);

ReleaseDC(this.Handle, desktopPtr);
g.Dispose();
}
}


The code works but it runs slow. In the google search I found I might be able to use print event and make all the drawing task in to list and process them one by one but I don't know how I can do that. I need sample codes.










share|improve this question



























    0















    I want to draw rectangle on the desktop as I move mouse cursor. Old rectangle needs to be gone before drawing new one.
    However InvalidateRect will occur after the new rectangle is drown so all rectangles are gone. So I had to add Application.DoEvents() to process InvalidateRect before draw new rectangle. But it slows down my application. Is there a better way to do this?



    void mouseHook_MouseMove(object sender, MouseEventArgs e)
    {
    label1.Text = (Cursor.Position.X + " : " + Cursor.Position.Y);

    if (start == true)
    {
    IntPtr desktopPtr = GetDC(IntPtr.Zero);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromHdc(desktopPtr);

    Rectangle bb = new Rectangle(1, 1, 1, 1);

    bb = DrawRec(Cursor.Position.X, Cursor.Position.Y, desktopPtr, g);//getting new rectangle to draw on the desktop, rectangle topleft position is fixed
    Application.DoEvents();
    g.DrawRectangle(new Pen(Color.Red, 1), bb);

    ReleaseDC(this.Handle, desktopPtr);
    g.Dispose();
    }
    }


    The code works but it runs slow. In the google search I found I might be able to use print event and make all the drawing task in to list and process them one by one but I don't know how I can do that. I need sample codes.










    share|improve this question

























      0












      0








      0








      I want to draw rectangle on the desktop as I move mouse cursor. Old rectangle needs to be gone before drawing new one.
      However InvalidateRect will occur after the new rectangle is drown so all rectangles are gone. So I had to add Application.DoEvents() to process InvalidateRect before draw new rectangle. But it slows down my application. Is there a better way to do this?



      void mouseHook_MouseMove(object sender, MouseEventArgs e)
      {
      label1.Text = (Cursor.Position.X + " : " + Cursor.Position.Y);

      if (start == true)
      {
      IntPtr desktopPtr = GetDC(IntPtr.Zero);
      System.Drawing.Graphics g = System.Drawing.Graphics.FromHdc(desktopPtr);

      Rectangle bb = new Rectangle(1, 1, 1, 1);

      bb = DrawRec(Cursor.Position.X, Cursor.Position.Y, desktopPtr, g);//getting new rectangle to draw on the desktop, rectangle topleft position is fixed
      Application.DoEvents();
      g.DrawRectangle(new Pen(Color.Red, 1), bb);

      ReleaseDC(this.Handle, desktopPtr);
      g.Dispose();
      }
      }


      The code works but it runs slow. In the google search I found I might be able to use print event and make all the drawing task in to list and process them one by one but I don't know how I can do that. I need sample codes.










      share|improve this question














      I want to draw rectangle on the desktop as I move mouse cursor. Old rectangle needs to be gone before drawing new one.
      However InvalidateRect will occur after the new rectangle is drown so all rectangles are gone. So I had to add Application.DoEvents() to process InvalidateRect before draw new rectangle. But it slows down my application. Is there a better way to do this?



      void mouseHook_MouseMove(object sender, MouseEventArgs e)
      {
      label1.Text = (Cursor.Position.X + " : " + Cursor.Position.Y);

      if (start == true)
      {
      IntPtr desktopPtr = GetDC(IntPtr.Zero);
      System.Drawing.Graphics g = System.Drawing.Graphics.FromHdc(desktopPtr);

      Rectangle bb = new Rectangle(1, 1, 1, 1);

      bb = DrawRec(Cursor.Position.X, Cursor.Position.Y, desktopPtr, g);//getting new rectangle to draw on the desktop, rectangle topleft position is fixed
      Application.DoEvents();
      g.DrawRectangle(new Pen(Color.Red, 1), bb);

      ReleaseDC(this.Handle, desktopPtr);
      g.Dispose();
      }
      }


      The code works but it runs slow. In the google search I found I might be able to use print event and make all the drawing task in to list and process them one by one but I don't know how I can do that. I need sample codes.







      c#






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 0:40









      user1785594user1785594

      8228




      8228
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000070%2fhow-to-use-invalidaterectapplication-doevents-without-slowing-down-application%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000070%2fhow-to-use-invalidaterectapplication-doevents-without-slowing-down-application%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'