C++/How can i print graphics in c++?
I have been programming for a while now,
i have used Blitz, Basic, and some C++.
In C++ i only know how to output text
to a terminal window. I was wondering
if there is a way to output to an
actual window. If there is a way,
then can i print out images or shapes
to the window?
If there is a way to open a window and
print images/shapes/pixels to the window,
can you PLEASE tell me how to do it or
link me to a page that can?
.... if you can, can you type up a small
bit of example code please?
(By the way, i am using MCVisual Studio
to program/compile my work. and i am
using a Windows XP)
Hello Steven, thank you for the question.
I'm sorry for the delay, I haven't had access to the internet.
Graphics can be done using the Windows Graphics Device Interface. You need a handle to a device context for starters. You can get one by called GetDC(), and release it with ReleaseDC().
Here are a list of helpful drawing functions:
In order to create a project that spawns a Window instead of a terminal, you simply create a new Win32 project in Visual Studio. Don't change any of the default settings.
In a Windows project, instead of main you get WinMain. This is your main entry point, and contains your message loop. A windows application loops until it gets the exit message, whereas a console project simply executes code top to bottom, looping only if you tell it.
int APIENTRY _tWinMain(HINSTANCE hInstance,
// TODO: Place code here.
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_ALLEXPERTSWINDOWS, szWindowClass, MAX_LOADSTRING);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ALLEXPERTSWINDOWS));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
return (int) msg.wParam;
Inside the while loop under the Main message loop comment is where you would do your drawing. Every loop you have to call InvalidateRect in order to refresh your window. If you don't, then it will simply draw the same shape all over itself if you are moving it.
Here is a good starting tutorial: http://windows-programming.suite101.com/article.cfm/win32_createdc_and_gdi_funct
I hope this information was helpful.