使用GDI的textout在屏幕上显示文本并消失 [英] Using textout of GDI shows text on screen and disapperas
问题描述
我需要使用GDI Textout在屏幕上显示一些文本。使用下面的代码显示屏幕上的文字,但立即消失。任何人都可以纠正我的错误
我尝试了什么:
金额1 =卢比。*******。**
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);
/ *方法定义* /
DisplayAmount(HDC hdc,int XPos,int YPos,char * amount)
{
如果(!hdc)返回-1;
const COLORREF colorRed = RGB (255,255,255);
HFONT hfont;
LOGFONT logFont;
memset(& logFont,0, sizeof(logFont));
logFont.lfHeight = -48; //见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));
//恢复文字颜色和字体
SelectObject(hdc,oldHFont);
SetTextColor(hdc,oldTextColor);
返回0;
}
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;
}
推荐答案
这不会起作用;只要您处理WM_PAINT
消息,就会清除或覆盖您放入屏幕的任何内容。您应保存要显示的信息,并致电InvalidateRect
功能 [ ^ ]强制重新绘制窗口。然后在WM_PAINT
处理程序中,绘制/绘制/写入要显示的所有信息。
That is not going to work; as soon as you process aWM_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 theInvalidateRect
function[^] to force a repaint of your window. Then in yourWM_PAINT
handler, you do the drawing/painting/writing of all the information that you want displayed.
这篇关于使用GDI的textout在屏幕上显示文本并消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!