Mark Achrin
Mark Achrin

Reputation: 367

Win 32 C++, Drawing a draggable rectangle to the screen

This is a problem I've been having for a while now. I'm trying to have a draggable rectangle to show the user an area that they've selected in my screenshot program. The reason I want this is for users to be able to select a portion of the screen that they want to take a screenshot of. I have tried the following method with little success:

void drawRect(){
HDC screenDC = ::GetDC(0);
::Rectangle(screenDC, 200, 200, 300, 300);
::ReleaseDC(0, screenDC);
}

Now, to give this method due credit, it does draw a rectangle to the screen in a way that I'd expect however once the dragging has stopped the rectangle persists. I've looked at ways of getting rid of this such as updating the windows where the rectangle continues to be shown however I haven't managed to remove it. Further to this problem since you must redraw the rectangle every monitor refresh and the rectangles persist, I am left with lots of rectangles being drawn all over the screen and I then have to mouse over or click on any windows these are drawn over to remove them.

Upvotes: 1

Views: 580

Answers (1)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10425

Use DrawFocusRect instead. Drawing the same rect again removes it from the screen.

Upvotes: 1

Related Questions