-
October 31st, 2013, 05:42 PM
#16
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 31st, 2013, 05:54 PM
#17
Re: error on a code
Originally Posted by 2kaud
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()
-
October 31st, 2013, 08:19 PM
#18
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 31st, 2013, 08:25 PM
#19
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 1st, 2013, 05:50 AM
#20
Re: error on a code
Originally Posted by 2kaud
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|