[RESOLVED] error on a code - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: [RESOLVED] error on a code

  1. #16
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,985

    Re: error on a code

    Will set EmptyText to all spaces for the required length. As EmptyText is to use with TextOut you don't need a terminating NULL as you specify with TextOut the number of chars to display.
    Code:
    size_t tlen = p->Text.length();
    char *EmptyText = new char[tlen];
    memset(EmptyText, ' ', tlen);
    However, this is not your problem. The problem you have is that you are writing onto a DC and therefore using the font selected into that DC. On the Console this DC font is NOT the font that the console uses to display console characters (change ' ' in your code to '@' and see what happens!). So if you persist in using gui functions for the console window then you have a whole lot of more work to do. Use the console supplied functions. They are far easier to use.

    The other problem you have is that you assume that each subsequent use of TextOut clears from the screen that which was there before. It doesn't. It writes on top of what was there. So outputing a space over a letter does nothing. Because you haven't selected a correct font to use you only think that TextOut is working properly and the problem is with your EmptyText string. The real problem is with trying to use gui TextOut in the same way as you would output to a console. It doesn't work that way I'm afraid. Once you have your font selected, try outputting a string of A with O as the space character.
    Last edited by 2kaud; October 31st, 2013 at 06:18 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  2. #17
    Join Date
    Apr 2009
    Posts
    596

    Re: error on a code

    Quote Originally Posted by 2kaud View Post
    Will set EmptyText to all spaces for the required length. As EmptyText is to use with TextOut you don't need a terminating NULL as you specify with TextOut the number of chars to display.
    Code:
    size_t tlen = p->Text.length();
    char *EmptyText = new char[tlen];
    memset(EmptyText, ' ', tlen);
    However, this is not your problem. The problem you have is that you are writing onto a DC and therefore using the font selected into that DC. On the Console this DC font is NOT the font that the console uses to display console characters (change ' ' in your code to '@' and see what happens!). So if you persist in using gui functions for the console window then you have a whole lot of more work to do. Use the console supplied functions. They are far easier to use.
    thanks
    i can use the cout with get and set caret functions, but on delay i can lose the caret position when i need ask for name or something... that's why i use TextOut()

  3. #18
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,985

    Re: error on a code

    Try this as a starter for further work. The problem with doing blinking like this is how do you stop it? You start a new thread for each text element you want to blink which will stay blinking until its thread is finished. So to blink say 10 pieces of text on the screen you have 10 threads running with no way of stopping them to turn the blinking off.

    Code:
    #define WINVER 0x0501
    #define _WIN32_WINNT WINVER
    #include <windows.h>
    #include <process.h>
    
    #include <string>
    #include <iostream>
    using namespace std;
    
    struct Blink
    {
        string Text;
        int x;
        int y;
        COLORREF TextColor;
        COLORREF BackColor;
    };
    
    unsigned __stdcall BlinkLoop(void *params)
    {
    Blink *p = static_cast<Blink *>(params);
    
    int tlen = (int)p->Text.length();
    
    HDC hDC = GetDC(GetConsoleWindow());
    
    HBRUSH hb = CreateSolidBrush(p->BackColor);
    
    LOGFONT	lf;
    
    	GetObject(GetStockObject(OEM_FIXED_FONT), sizeof(LOGFONT), &lf);
    	lf.lfWeight = FW_REGULAR; 
      
    HFONT hf = CreateFont(lf.lfHeight, lf.lfWidth, 
    			lf.lfEscapement, lf.lfOrientation, lf.lfWeight, 
    			lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet, 
    			lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality, 
    			lf.lfPitchAndFamily, lf.lfFaceName);
    
    	SelectObject(hDC, (HGDIOBJ)(HFONT)hf);
    	SetTextColor(hDC, p->TextColor);
            SetBkMode(hDC, TRANSPARENT);
    
    TEXTMETRIC tm;
    
    	GetTextMetrics(hDC, &tm);
    
    RECT rt;
    
    	rt.left = p->x * tm.tmAveCharWidth;
    	rt.top = p->y * tm.tmHeight;
    	rt.right = rt.left + tlen * tm.tmAveCharWidth;
    	rt.bottom = rt.top + tm.tmHeight;
    
        while (true)
        {
    		TextOut(hDC, rt.left, rt.top, p->Text.c_str(), tlen);
                    Sleep(30);
    		FillRect(hDC, &rt, hb);
                    Sleep(30);
        }
        return 0;
    }
    
    void TextBlink(const string& Text, int x, int y, COLORREF TextColor, COLORREF BackColor)
    {
        Blink *b = new Blink;
        b->Text = Text;
        b->BackColor = BackColor;
        b->TextColor = TextColor;
        b->x=x;
        b->y =y;
    
        _beginthreadex(NULL, 0, BlinkLoop, b, 0, NULL);
    }
    
    int main()
    {
    	TextBlink("This text blinks ok", 0, 0, RGB(255, 255, 255), RGB(0, 0, 0));
    	TextBlink("and so does this text", 4, 5, RGB(255, 255, 255), RGB(0, 0, 0));
    	TextBlink("this text also blinks ok", 6, 8, RGB(255, 255, 255), RGB(0, 0, 0));
    	getchar();
    	return 0;
    }
    Last edited by 2kaud; October 31st, 2013 at 08:26 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #19
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,985

    Re: error on a code

    i can use the cout with get and set caret functions, but on delay i can lose the caret position when i need ask for name or something... that's why i use TextOut()
    Don't use cout/cin if you want to do clever things with the console. Use the special console input/output funtions
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    WriteConsole(), ReadConsole() etc.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #20
    Join Date
    Apr 2009
    Posts
    596

    Re: error on a code

    Quote Originally Posted by 2kaud View Post
    Don't use cout/cin if you want to do clever things with the console. Use the special console input/output funtions
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    WriteConsole(), ReadConsole() etc.
    thanks for all thanks
    you have right... see these loop:

    Code:
    while (true)
        {
    		TextOut(hDC, rt.left, rt.top, p->Text.c_str(), tlen+1);
                    Sleep(500);
    		FillRect(hDC, &rt, hb);
                    Sleep(500);
            if (IsWindow(GetConsoleWindow())==false) break;
        }
    these 'if' tests if the window continues valid, if not then close the loop.
    Last edited by Cambalinho; November 1st, 2013 at 06:32 AM.

Page 2 of 2 FirstFirst 12

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center