C++ Graphics Help - Drawing a line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Firecore
    New Member
    • Jul 2007
    • 114

    C++ Graphics Help - Drawing a line

    Hello there.
    This a part of a program i am trying to make. I am trying to learn how to use graphics in a console C++ App.
    I am using the Visual C++ 6 and whenever I try to run this code, I can never see any lines.

    Code :
    Code:
    //This is my graphics experiment
    
    #include <cstdio>
    #include <cmath>
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    
    int main(void)
    {
    	//Colour Declarations
    
    	COLORREF white = RGB(255,255,255);// White background
    	COLORREF black = RGB(0,0,0);
    	COLORREF lightblue = RGB(173,216,230);
    	int x = 0; // X coordinate
    	int y = 0; // Y coordinate
    	
    	int r, g, b; // RGB values
    	
    	SetConsoleTitle("Title Goes Here");
    	HWND hWnd = FindWindow(NULL, "Title Goes Here");
    
    	HDC hDC = GetDC(hWnd);
    
    	SetBkColor(hDC, white);
    	SetTextColor(hDC, black);
    
    	while (x != 50)
    	{
    		SetPixel(hDC, x, y, lightblue);
    		x = x + 1;
    
    	}
    
    	ReleaseDC(hWnd, hDC);
            DeleteDC(hDC);
    
    	return 0;
    }
    Basically, I am trying to change the console background to white and i am trying to draw a black line on it.

    Can people please help me by helping me make this work?

    When I run it, I cant see any graphics.

    Please Help.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by Firecore
    Hello there.
    This a part of a program i am trying to make. I am trying to learn how to use graphics in a console C++ App.
    I am using the Visual C++ 6 and whenever I try to run this code, I can never see any lines.

    Code :
    Code:
    //This is my graphics experiment
    
    #include <cstdio>
    #include <cmath>
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    
    int main(void)
    {
    	//Colour Declarations
    
    	COLORREF white = RGB(255,255,255);// White background
    	COLORREF black = RGB(0,0,0);
    	COLORREF lightblue = RGB(173,216,230);
    	int x = 0; // X coordinate
    	int y = 0; // Y coordinate
    	
    	int r, g, b; // RGB values
    	
    	SetConsoleTitle("Title Goes Here");
    	HWND hWnd = FindWindow(NULL, "Title Goes Here");
    
    	HDC hDC = GetDC(hWnd);
    
    	SetBkColor(hDC, white);
    	SetTextColor(hDC, black);
    
    	while (x != 50)
    	{
    		SetPixel(hDC, x, y, lightblue);
    		x = x + 1;
    
    	}
    
    	ReleaseDC(hWnd, hDC);
            DeleteDC(hDC);
    
    	return 0;
    }
    Basically, I am trying to change the console background to white and i am trying to draw a black line on it.

    Can people please help me by helping me make this work?

    When I run it, I cant see any graphics.

    Please Help.
    Trying to draw grpahics on the console is not going to work, the console does not support raster graphics because it is a text interface. If you called GetDeviceCaps on you DC I suspect you would find that it supports very little.

    You can set the text foreground and background colours of the console by calling cmd.exe with the /T switch when you start your console.

    From a command prompt run these 2 commands for more information

    cmd /?
    color /?

    If you need to draw raster graphics (pixels and lines) you are going to need a window.

    Comment

    • Firecore
      New Member
      • Jul 2007
      • 114

      #3
      Originally posted by Banfa
      If you need to draw raster graphics (pixels and lines) you are going to need a window.
      How do i do that? Can you please tell me?

      Comment

      • arunmib
        New Member
        • May 2007
        • 104

        #4
        If you need to draw something in windows you need a bitmap area (as far as my understanding goes, correct me if I am wrong). Since console is only for text, you need to create a Bitmap and associate it with the console and then you can do the 'SetPixel' kind of thing.
        Take a look at the following APIs in the MSDN website, they might be of use to
        you,

        CreateCompatibl eDC, CreateCompatibl eBitmap, SelectObject and then your SetPixel.

        The first two APIs mentioned will mostly help you avoid much details about the bitmap area. But if you wish to get into the details, refer to their counterparts in the "see also" section below.

        I hope my post is of some help. If I am wrong anywhere, correct me.

        Comment

        • Firecore
          New Member
          • Jul 2007
          • 114

          #5
          Thank You for your help.
          I will check the stuff tonight.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by Firecore
            How do i do that? Can you please tell me?
            Start by creating a WIN32 application not a console application has they have different entry points.

            Comment

            Working...