【发布时间】:2012-05-28 09:11:10
【问题描述】:
我有一个用 Visual C++ 6 编写的旧应用程序。这个应用程序的一部分是将文本绘制到位图中。这在 Windows XP 上运行良好,但是当在 Windows 7 上运行相同的代码时,所有文本都会在 ASCII 表中移动一个位置。
例如,"Category" 变为 "B'sdfnqx"。
任何想法是什么导致了这个问题以及如何解决它?
编辑:抱歉,以上内容略有不正确。当我在代码中看到 DrawText 函数时,我认为它是 GDI 函数。当我踏入其中时,发现作者创建了自己的DrawText 函数,该函数使用OpenGL。我不知道任何OpenGL,所以现在这已经失控了。它调用glCallLists 将文本(存储在CString 中)传递给这个函数。
完整的课程代码如下。注意:是 DrawText 函数中的 glCallLists 函数导致问题。
OGLFontClass::OGLFontClass()
{
m_id = -1;
}
OGLFontClass::~OGLFontClass()
{
Clear();
}
void OGLFontClass::Clear()
{
if( m_id != -1 )
{
glDeleteLists(m_id,255);
m_id = -1;
}
}
void OGLFontClass::Initialise(CString fontname, int size, HDC hDC)
{
m_HDC = hDC;
m_id = glGenLists(255);
::DeleteObject( m_FONT );
m_FONT = CreateFont( -size, // Height Of Font ( NEW )
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_NORMAL, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
fontname); // Font Name
HFONT oldfont = (HFONT)SelectObject(hDC, m_FONT); // Selects The Font We Want
wglUseFontBitmaps(hDC, 0, 255, m_id ); // Builds 96 Characters Starting At Character 32
::SelectObject( hDC, oldfont );
}
void OGLFontClass::DrawText( float x, float y, CString str )
{
glRasterPos3f(x,y, 0);
glPushAttrib(GL_LIST_BIT);
glListBase(m_id);
glCallLists(str.GetLength(), GL_UNSIGNED_BYTE, str.GetBuffer(0));
glPopAttrib();
}
void OGLFontClass::DrawText(int x, int y, int r, int g, int b, CString text)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();// Reset The View
HWND hWnd = ::WindowFromDC(wglGetCurrentDC() );
RECT rc;
::GetClientRect( hWnd, &rc );
int CX = rc.right;//::GetSystemMetrics( SM_CXSCREEN );
int CY = rc.bottom;//::GetSystemMetrics( SM_CYSCREEN );
gluOrtho2D (0,::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN), 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3ub(r,g,b);
glRasterPos2d( x, y ); // had to do this to get the text to line up where i want it
//glDisable(GL_TEXTURE_2D);
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits ( NEW )
glListBase(m_id); // Sets The Base Character to 32 ( NEW )
unsigned char* szTemp = new unsigned char[text.GetLength()+1];
strcpy((char*)szTemp, text);
glCallLists(strlen((char*)szTemp), GL_UNSIGNED_BYTE, szTemp); // Draws The Display List Text ( NEW )
delete[] szTemp;
glPopAttrib(); // Pops The Display List Bits ( NEW )
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void OGLFontClass::DrawRightText( int x, int y, int r, int g, int b, CString text )
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();// Reset The View
HWND hWnd = ::WindowFromDC(wglGetCurrentDC() );
RECT rc;
::GetClientRect( hWnd, &rc );
float CX = (float)::GetSystemMetrics( SM_CXSCREEN );
float CY = (float)::GetSystemMetrics( SM_CYSCREEN );
float fMultiplier = CX / CY;
gluOrtho2D (0,::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN), 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
int nPos = x;
glColor3ub(r,g,b);
//glDisable(GL_TEXTURE_2D);
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits ( NEW )
glListBase(m_id); // Sets The Base Character to 32 ( NEW )
for( int i = text.GetLength() - 1; i >= 0; i-- )
{
CString sChar = text.GetAt(i);
glRasterPos2d(nPos,y); // had to do this to get the text to line up where i want it
glCallLists(1, GL_UNSIGNED_BYTE, sChar); // Draws The Display List Text ( NEW )
if ( i > 0 )
{
CString sNextChar = text.GetAt(i-1);
SIZE szWidth = GetTextExtent(sNextChar);
szWidth.cx += 1;
szWidth.cx *= fMultiplier;
szWidth.cx += 1;
nPos -= szWidth.cx;
}
}
//glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text ( NEW )
glPopAttrib(); // Pops The Display List Bits ( NEW )
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
CSize OGLFontClass::GetTextExtent(CString text, float fFactor)
{
SIZE sz;
HFONT oldfont = (HFONT) SelectObject(m_HDC, m_FONT);
GetTextExtentPoint32(m_HDC,text,strlen(text),&sz);
SelectObject(m_HDC, oldfont);
sz.cx *= 0.2;
sz.cy *= 0.2;
return sz;
}
现在我根本不知道 openGL,但我假设 glCallLists 只是将字符串重新解释为字节数组,在 Windows XP 和 Windows 7 之间出现了问题。也许是 unicode 问题或什么的?可能是 32 位 Windows 操作系统与 64 位操作系统?
现在这是否显示出更明显的问题?
【问题讨论】:
-
真是个大问题。我想自己知道答案,即使它不会影响到我:-)。
-
谢谢。希望我能得到一个很好的答案;)
-
当然,这个问题与使用的字体无关。
-
@Sachin:不,最低级别的问题似乎是对字体字形的错误索引(减一),因此字体技术很可能是相关的。一个完整的例子比知道哪种字体更好。
-
我猜想传递给 glCallLists 的基本列表编号是硬编码的,而不是使用从 glGenLists 返回的值。并且 glGenLists 在您的不同 PC 上返回不同的值。你可能想检查一下。