How to draw on a full screen but show drawing on a specific rectangle/window? (C++)

Davit 136 Reputation points
2020-10-30T11:22:01.713+00:00

Hi,

I have a window. I want to draw inside the window (with a pen, like in paint), but I don't want to have the drawing to be attached to the window, i.e. I don't want the drawing to move with the window, when the window is moved. I would like the drawing to be attached to its screen position, and I would like it to disappear when it goes outside of the window area (i.e. when the window is moved). And when the window is moved back to its original position, drawing should be shown again, i.e. should be there. This can be achieved if it is possible to draw on a full screen, but only show drawing on a specific rectangle. In that case I can regularly update the rectangle when my window is moved. Is this possible in winapi?

Thanks

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,613 questions
0 comments No comments
{count} votes

Accepted answer
  1. Strive Sun-MSFT 426 Reputation points
    2020-11-02T03:54:30.037+00:00

    Hello @Davit ,

    This can be achieved if it is possible to draw on a full screen, but only show drawing on a specific rectangle. In that case I can regularly update the rectangle when my window is moved. Is this possible in winapi?

    You can use CreateCompatibleDC to create a memory device context, and draw in memdc. Then use the StretchBlt function to copy the graphics to the drawing window.

    Some code for reference:

        HDC memdc = CreateCompatibleDC(hdc);  
        HDC child_hdc = GetDC(Child_hwnd);  
        HBITMAP membitmap = CreateCompatibleBitmap(hdc, 800, 600);  
        SelectObject(memdc, membitmap);  
        BitBlt(memdc, 0, 0, 800, 600, hdc, 0, 0, SRCCOPY);  
        MoveToEx(memdc, 10, 10, (LPPOINT)NULL);  
        LineTo(memdc, 100, 100);  
        MoveToEx(memdc, 200, 200, (LPPOINT)NULL);  
        LineTo(memdc, 300, 300);  
         
        StretchBlt(child_hdc,0,0,400,300, memdc,0,0,800,600, SRCCOPY);  
    

    Like this:

    g81h8.png

    When you move the main window, you will receive a WM_MOVE message. Use the InvalidateRect function in WM_MOVE to update the rectangle.

    I would like the drawing to be attached to its screen position, and I would like it to disappear when it goes outside of the window area (i.e. when the window is moved).

    PtInRect is what you need.

    Determine whether the four coordinates of the drawing window are in the main window, if not, use ShowWindow to hide the drawing window.

    ShowWindow(Child_hwnd, SW_HIDE);  
    

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.