Create "drawings" with windows API

Asked

Viewed 133 times

2

Hello, I need help with the windows API when it comes to handling "drawings". My problem is that if you resize or minimize the window, everything that was printed on the console disappears. This is what I have been able to do on this subject:

#include <Windows.h>
#include<windows.h>
#include<iostream>

using namespace std;

int main() {
    cin.ignore();
    //Get a console handle
    HWND myconsole = GetConsoleWindow();
    //Get a handle to device context
    HDC mydc = GetDC(myconsole);

    //Choose any color
    COLORREF COLOR= RGB(255,255,255);
    HPEN hBluePen = CreatePen(PS_SOLID, 1, COLOR);
    HGDIOBJ hPen = SelectObject(mydc, hBluePen);

    //Lines
    MoveToEx(mydc, 10, 40, NULL);
    LineTo(mydc, 44, 10);
    LineTo(mydc, 78, 40);

    //Rectangles
    cin.ignore();
    Rectangle(mydc, 16, 36, 72, 70);
    Rectangle(mydc, 60, 80, 80, 90);

    //Elipse
    cin.ignore();
    Ellipse(mydc, 40, 55, 48, 65);

    ReleaseDC(myconsole, mydc);
    cin.ignore();
    return 0;
}

This draws 5 objects, two lines, two rectangles and an ellipse.

EDIT:

I tried to make an approach with Windowproc but also unsuccessful. I leave the code below:

        LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
            switch (message) {
                case WM_PAINT:
                    cout << "text";
                default:
                    return DefWindowProc(hwnd, message, wParam, lParam);
           }
      }
  • 3

    Basically you have to do the repaint every time the window visibility occurs. Windows is all the time sending messages to your application indicating that something has occurred and that you will probably want to do something like redesign the screen. That is, it is an event system. Search by WNDPROC. http://keithditch.powweb.com/Games/html/wndproc.html, http://www.winprog.org/tutorial/window_click.html and https://msdn.microsoft.com/en-us/library/windows/desktop/ms633573(v=vs.85).aspx. Help you? Settles the issue?

  • Hello, I had already tried to do something like this but without success, but thanks anyway for the links. I will put this attempt in question.

  • 1

    In WM_PAINT is not to do Cout... is to put the code (or call the function) drawing!

  • @Bacco I only put Cout to see if WM_PAINT was called and is not being called

  • And did you associate the callback in the window creation? It is not enough to create the function, you need to appoint it responsible for the messages https://msdn.microsoft.com/en-us/library/windows/desktop/ms633570(v=vs.85). aspx

  • @Bacco And how do I do it? I didn’t notice how they do on the link indicated.

  • I recommend using some cross platform graphics API, if you use Windows api, will give problem if you want to port your program to another platform. (I find it even easier to draw in QT)

  • @silas333 But for now I just need it to work for windows, but thanks for the suggestion.

  • Could someone give me an example? I would be very grateful.

Show 4 more comments

1 answer

0

Good evening, I created a recursive function called Redraw, which forces the screen update every 50 milliseconds, see if it solves your problem

#include<windows.h>
#include<iostream>

using namespace std;

void Redraw(HDC mydc);


int main() {
    //Get a console handle
    HWND myconsole = GetConsoleWindow();
    //Get a handle to device context
    HDC mydc = GetDC(myconsole);

    //Choose any color
    COLORREF COLOR = RGB(255, 255, 255);
    HPEN hBluePen = CreatePen(PS_SOLID, 1, COLOR);
    HGDIOBJ hPen = SelectObject(mydc, hBluePen);

    Redraw(mydc);

    ReleaseDC(myconsole, mydc);
    cin.ignore();
    return 1;
}

void Redraw(HDC mydc)
{
    //Lines
    MoveToEx(mydc, 10, 40, NULL);
    LineTo(mydc, 44, 10);
    LineTo(mydc, 78, 40);

    //Rectangles
    Rectangle(mydc, 16, 36, 72, 70);
    Rectangle(mydc, 60, 80, 80, 90);

    //Elipse
    Ellipse(mydc, 40, 55, 48, 65);
    Sleep(50);
    Redraw(mydc);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.