AllExperts > C++ 
Search      
C++
Volunteer
Answers to thousands of questions
 Home · More C++ Questions · Answer Library  · Encyclopedia ·
More C++ Answers
Question Library

Ask a question about C++
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Eddie
Expertise
I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience
I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

 
   

You are here:  Experts > Computing/Technology > C/C++ > C++ > How can i print graphics in c++?

C++ - How can i print graphics in c++?


Expert: Eddie - 10/7/2009

Question
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?


-Steven Rogers

(By the way, i am using MCVisual Studio
to program/compile my work. and i am
using a Windows XP)

Answer
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:

LineTo()
Rectangle()
SetPixel()
InvalidateRect()
GetClientRect()

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,
                    HINSTANCE hPrevInstance,
                    LPTSTR    lpCmdLine,
                    int       nCmdShow)
{
  UNREFERENCED_PARAMETER(hPrevInstance);
  UNREFERENCED_PARAMETER(lpCmdLine);

   // TODO: Place code here.
  MSG msg;
  HACCEL hAccelTable;

  // Initialize global strings
  LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  LoadString(hInstance, IDC_ALLEXPERTSWINDOWS, szWindowClass, MAX_LOADSTRING);
  MyRegisterClass(hInstance);

  // Perform application initialization:
  if (!InitInstance (hInstance, nCmdShow))
  {
     return FALSE;
  }

  hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ALLEXPERTSWINDOWS));

  // Main message loop:
  while (GetMessage(&msg, NULL, 0, 0))
  {
     if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
     {
        TranslateMessage(&msg);
        DispatchMessage(&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.

- Eddie

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.