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

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.

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

Related

How to clip the corner rectangle created by two scrollbar controls

Let's say you have a resizable window with child scrollbar controls, and the scrollbars come and go depending on whether the window contents are large enough to require scrolling.
When both scrollbars are present, a small rectangle is effectively created in the bottom right corner of the window, at their intersection. Is there a clean strategy for clipping that rectangle when drawing on the window, so that you don't paint on it?
I guess my current approach is to obtain the rectangles for each scrollbar, and if those rectangles are not null, then use the rectangles' locations to determine the rectangle that we want to clip. And then call ExcludeClipRect for that rectangle. I guess a similar approach could be used, except with GetSystemMetrics(SM_CXVSCROLL) and GetSystemMetrics(SM_CYVSCROLL) to get the rectangle dimensions.
But is there a more accepted way of doing this, perhaps using some helpful clipping API functions? Thank you for any input.

Draw button on top of ImageList videos in C++ with winapi

I'm having trouble drawing a button on top of a video with WinAPI. I am grabbing frames from the webcam and drawing those frames as soon as I get them. The problem is that whenever I draw an image the button disappears under it.
I tried using SetWindowPos(button, HWND_TOPMOST, 50, 50, 50, 50, SWP_SHOWWINDOW); to place the button at the topmost of the z-order but it still doesn't work.
The image is drawn on top of the button and this is a required behavior. I want the image to be as big as possible (taking the whole monitor).
I thought maybe I should make a part of the image transparent (the part over the button). Is that an easy solution and how should I go about doing that.
You can add WS_CLIPSIBLINGS style to the button ,
Clips child windows relative to each other; that is, when a particular
child window receives a WM_PAINT message, the WS_CLIPSIBLINGS style
clips all other overlapping child windows out of the region of the
child window to be updated. If WS_CLIPSIBLINGS is not specified and
child windows overlap, it is possible, when drawing within the client
area of a child window, to draw within the client area of a
neighboring child window.
Refer this case: https://stackoverflow.com/a/56470706/11128312

C++/CLI Visual C++ 2010 Express - Drawing different shapes inside one panel

I got a problem. There is panel1 which i want to use as my paiting window. And i also have 2 buttons. One should draw "fillRectangle" in the middle of panel and second one should draw "fillellipse" next to it. I dont have the problem with drawing itself
Color aColor = Color::FromArgb( 255, 0, 0 );
SolidBrush^ aBrush = gcnew SolidBrush(aColor);
Rectangle rect = Rectangle(x, y, 10, 10);
e->Graphics->FillEllipse(aBrush, rect);
But i want to know, how to make that pressing each button draws different shape inside panel. Do i need to put both, ellipse and rectangle in panel1_Paint and use flags like...
if(ellip == 1) FillEllipse;
if(recta == 1) FillRectangle
which are set using buttons? I hoped i can code drawing part inside button or function, and then somehow refer to that panel. Is it possible?
If you want the buttons to have their own painting routines, then they draw themselves and your panel is unnecessary, in terms of drawing anyway, they'll have their own canvas.
If you want a panel that draws shapes on itself dependant on some property, then yes you go down something like the route you suggested, though I'd be tempted by some sort of shape class even if it wasn't a control. Would make adding other shapes or more of a shape much easier.
You could have panel carry a collection of shapes that implement say IDraw which takes a Graphics reference, and then just iterate through them and call it with Panel1's graphics instance.
If it was me though particulary if I wanted enabled, visible, tab, click etc. I'd have a ShapedButton Control.

draw in picturebox with scorll mfc c++

I want to draw in Picture Box control my purpose is picture box have scroll bar that I can draw in it bigger than what It's size I mean have scroll to move It's picture,
I try to draw something more than picturebox's size, It went to main frame panel my code is below.
void Cex133Dlg::OnBnClickedOk()
{
CDC *myDC = GetDlgItem(IDC_DRAWBOX)->GetDC();
myDC->Rectangle(10, 10, 20, 20);
}
You can draw with in the picture box. Definitely it will go outside the picture box frame, if you draw something more than picturebox's size. For that you can calculate the picture box Size, According to that you will draw your object.
You don't draw to the control -- you give it a bitmap, and it does its own drawing.
Hi you can look for http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c14765 and mfc CScrollView

How can I draw a selection rectangle on the screen with Qt?

How can I draw a selection rectangle on my screen with Qt in X11?
I want to be able to drag a rectangle on my screen (outside of the application) and then save the whole rectangle.
Thanks in advance.
Part of the solution will involve using the grabWindow() function of QPixmap like so:
QPixmap::grabWindow(QApplication::desktop()->winId());
Qt has an example program for this here.
There rest of the solution, drawing the area to grab, can probably be achieved by either using a full screen transparent window to render a mouse drawn rectangle and then taking the section it outlines from the grabbed desktop image or using a full screen window with the entire grabbed screen painted on it.