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);
}
}
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?– Maniero
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.
– Simple coder
In WM_PAINT is not to do Cout... is to put the code (or call the function) drawing!
– Bacco
@Bacco I only put Cout to see if WM_PAINT was called and is not being called
– Simple coder
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
@Bacco And how do I do it? I didn’t notice how they do on the link indicated.
– Simple coder
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)
– silash35
@silas333 But for now I just need it to work for windows, but thanks for the suggestion.
– Simple coder
Could someone give me an example? I would be very grateful.
– Simple coder