Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4049038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:53:38+00:00 2026-05-20T13:53:38+00:00

I have a win32 application and i want to draw a line by dragging

  • 0

I have a win32 application and i want to draw a line by dragging the mouse. I use double buffering as well but the problem is it draws multiple lines in the path of the mouse. Here is my drawing code:

hdc = BeginPaint(hWnd, &ps);
hdcBack = CreateCompatibleDC(hdc); 
GetClientRect(hWnd, &windowRect); 
backBuffer = CreateCompatibleBitmap(hdc, windowRect.right, windowRect.bottom);
SelectObject(hdcBack, backBuffer); 
FloodFill(hdcBack, 0, 0, RGB(255, 255, 255)); 
BitBlt(hdcBack,0,0,windowRect.right,windowRect.bottom,hdc,0,0,SRCCOPY);
color = RGB(rand() % 255, rand() % 255, rand() % 255); 
hBrush = CreateSolidBrush(color); 
SelectObject (hdcBack, hBrush); 

MoveToEx(hdcBack,x1,y1,NULL); //x1,y1,x2,y2 are the initial click point and the current position of the mouse when keeping the left button down and dragging
LineTo(hdcBack,x2,y2);
BitBlt(hdc, 0, 0, windowRect.right, windowRect.bottom, hdcBack, 0, 0, SRCCOPY);

DeleteObject(hBrush); 
DeleteDC(hdcBack);
DeleteObject(backBuffer);
EndPaint(hWnd, &ps);

I tried also not copying the background into the buffer before drawing the line and it draws the line correctly but when i draw a new line the previously drawn line disappears.
So how can i draw multiple lines with double buffering and keeping the previous drawn lines?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:53:38+00:00Added an answer on May 20, 2026 at 1:53 pm

    The problem’s inherent in the design, if I understand the requirement correctly.

    The first BitBlt() copies the previous contents on to the buffer, then you draw the line, and then you apply the changes to the screen, which gives the exact results you described. This way you’re only adding graphics rather than replacing, and you see several lines instead of one.

    If you want to display a line that’s being dragged by the mouse, you need to first fill the back-buffer with whatever background you had in mind (call it the constant-data), and paint on it the relevant graphics (call it changing-data) every mouse move. Anyway, I believe that commenting that first BitBlt() should do the trick.

    Also, you’re selecting a brush before calling the line functions, which use a pen. Shouldn’t that call come before calling FloodFill()?

    EDIT:

    Use a third buffer to hold the most recent data as suggested in my comment to yours. In your “mouse-up” handler, finally draw the new line on that buffer.
    So you read from that buffer in your mouse-move handler and on-paint handler, and write to it in the mouse-up handler, when the user is finally sure how he wants his line drawn.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a win32 application, in which I want to use openGL just for
Is it possible to use jQuery in win32 application? I have seen interesting stuff
Very simple problem. I have a Delphi application and I want to restrict access
We have a command line application that could benefit from a GUI. We want
I have win32 API application in visual c++. I want to create a directory
For my cross-platform application I have started to use Boost, but I can't understand
In a win32 application, I want to have a button with an icon which
I have small problem with my .net 2.0 winforms application. I want to embed
I need to have a Win32 application load a hard coded AES-256 key, ideally
I have developed a Win32 application using C/C++, which runs on Vista and XP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.