CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 23

Threaded View

  1. #21
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Learning to use Microsoft Visual C++

    OK. You're getting a bit ahead of yourself.

    The rule number one is that you plan ahead before you plunge into coding. I understand right now you may have a practice run, but in the future always plan ahead how you will implement all of the features in your code.

    Quote Originally Posted by Ion Zone View Post
    Unfortunately, the code I'm using is quite clunky and I'm having some trouble
    I'm not going to write it all for you, but here are some corrections to what you said:

    1. You need to use variables and functions or even better create a class. All that will help you minimize the efforts needed to program it. You see someone has made those things up not to annoy you in different books but to actually make your life simpler.

    I don't seem to be able to maximise the program (Quests got cut off :P )
    2. You need to get used to live with what you currently have. A console window has a certain size, which you can learn by calling GetConsoleScreenBufferInfo and check the CONSOLE_SCREEN_BUFFER_INFO:wMaximumWindowSize that will give you the boundaries of it. In your case you need to format your output in accordance with those boundaries.

    3. Use preprocessor values instead of simple numbers. Otherwise the code you write will not be readable and you may run into more troubles when porting it.

    For instance this:
    Code:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
    should've been this:
    Code:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
    can you change font size and style?
    4. Yes you can, but I wouldn't complicate your life with it at this point. If you still want to do it, check SetCurrentConsoleFontEx

    Also, is there an include I can do to get symbols like £, €, and (especially) ¥ (yen)
    5. First off, you need to build your project as Unicode (which you may be already doing by default if you're using MS VS 2008). Then you can look up your character's Unicode value here and use it like this (for the sample code you'll find below):
    Code:
    //Unicode value for Yen sign is U+00A5
    PrintTextInPos(hCons, _T("The cost is \u00A5100"), 16, 8);
    So here's how one of the versions of your code could've looked like (Note that there are many ways to do it):
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <windows.h>
    using namespace std;
    
    
    bool PrintTextInPos(HANDLE hConsole, LPCTSTR pText, int x, int y, WORD wAttributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	HANDLE hCons = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	//Place "Hello"
    	PrintTextInPos(hCons, _T("Hello"), 6, 10);
    
    	//Place "World"
    	PrintTextInPos(hCons, _T("World"), 16, 10, FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
    
    	//Wait for user input before closing the console
    	getchar();
    
    	return 0;
    }
    
    bool PrintTextInPos(HANDLE hConsole, LPCTSTR pText, int x, int y, WORD wAttributes)
    {
    	//Print text from 'pText' at the 'x' and 'y' position in the 'hConsole' console with 'wAttributes' attributes
    	//For the reference of attributes check:
    	//http://msdn.microsoft.com/en-us/libr...ter_attributes
    	//Returns:
    	//		- true = if success
    	bool bResult = false;
    
    	//Get console information
    	CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
    	if(GetConsoleScreenBufferInfo(hConsole, &csbi))
    	{
    		//Set cursor position
    		COORD coord = {x, y};
    		if(SetConsoleCursorPosition(hConsole, coord))
    		{
    			//Set text & background color
    			if(SetConsoleTextAttribute(hConsole, wAttributes))
    			{
    				//Output text
    				DWORD dwchWritten;
    				WriteConsole(hConsole, pText, lstrlen(pText), &dwchWritten, NULL);
    
    				//Done!
    				bResult = true;
    			}
    		}
    
    		//Restore cursor position & attributes
    		SetConsoleCursorPosition(hConsole, csbi.dwCursorPosition);
    		SetConsoleTextAttribute(hConsole, csbi.wAttributes);
    	}
    
    	return bResult;
    }
    Last edited by ahmd; July 17th, 2010 at 02:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured