Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to Display some text on to the screen using GDI Textout. Using the following code displays the text on the screen but immediately disappears.Can any one correct me where i am going wrong

What I have tried:

Amount1 = "Rs.*******.**"
m_hdc = ::GetDC(0);
m_nXPos = m_nYPos = 0;
if(m_hdc)
{
m_nXPos = ::GetDeviceCaps(m_hdc,HORZRES)/2;
m_nYPos = ::GetDeviceCaps(m_hdc,VERTRES)/2;
}
m_nXPos = m_nXPos - 100;
DisplayAmount(m_hdc,m_nXPos,m_nYPos,(LPSTR)(LPCTSTR)Amount1);
DisplayAmount(m_hdc,m_nXPos,m_nYPos,(LPSTR)(LPCTSTR)Amount1);

/*Method Defination*/
DisplayAmount(HDC hdc,int XPos, int YPos, char* amount)
{

if(!hdc) return -1;

const COLORREF colorRed = RGB(255,255,255);

HFONT hfont;
LOGFONT logFont;
memset(&logFont, 0, sizeof(logFont));
logFont.lfHeight = -48; // see PS
logFont.lfWeight = FW_NORMAL;

strcpy(logFont.lfFaceName, "Zurich BT");
hfont = CreateFontIndirect(&logFont);

COLORREF oldTextColor = SetTextColor(hdc, colorRed);
SetBkMode(hdc, TRANSPARENT);
HFONT oldHFont = (HFONT)SelectObject(hdc, hfont);

TextOut(hdc, XPos, YPos, amount, strlen(amount));



// restore text color and font
SelectObject(hdc, oldHFont);
SetTextColor(hdc, oldTextColor);


return 0;
}
Posted
Updated 24-May-16 0:02am
Comments
Mehdi Gholam 24-May-16 5:43am    
Try putting your code in the Paint() handler.

1 solution

That is not going to work; as soon as you process a WM_PAINT message, then whatever you have put to the screen will be cleared or overwritten. You should save the information you want to display, and call the InvalidateRect function[^] to force a repaint of your window. Then in your WM_PAINT handler, you do the drawing/painting/writing of all the information that you want displayed.
 
Share this answer
 
Comments
Member 11648410 24-May-16 6:26am    
I am Using this in a DLL not MFC application. i dont have WM_PAINT.
Richard MacCutchan 24-May-16 7:04am    
Then I am afraid that your design is wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900