It's a long story, but here we go:
So, I make this really cool field class, and then back it up to my CD-RW. Then, a few days later, I decide to go back and inprove on it and finish it up. When I run it, one of the Win32 GDI functions is acting strange. I thought, "It must have been something I'd done." So, I ran the .exe from my CD-RW backup. It did the same thing, even though I KNEW it had worked a few days ago. And now I'm really stuck, and I think my computer may be possesed by evil demons out to thwart my programming efforts. Exactly what happens is, any call to a GDI function that gets information about text size, wtc. such as GetTextMetrics() and GetTextExtentPoint32() fails, yet it did not a couple days ago. And it doesn't fail in the sense that it returns a zero, it just does not fill the structures with information!
Here's the class definition:
class CField
{
public:
char *GetContents(void)
{
return contents;
}
int Init(int f_maxchars, USHORT f_bgcolor, USHORT f_bordercolor, COLORREF f_textcolor, int f_bordersize, LPDIRECTDRAWSURFACE7 f_lpdds, HFONT f_font, int f_x, int f_y);
void PutStr(char *f_char);
void Clear();
int Get_Cursor_Pos()
{
return cursorpos;
}
void GiveFocus()
{
cursor_on = true;
focus = true;
}
void Backspace();
bool IsFull()
{
if(strlen(contents) == maxchars)
return true;
else
return false;
}
void Change_Pos(int f_x, int f_y)
{
x = f_x;
y = f_y;
}
void Release();
void Update_Cursor_Pos(int f_pos);
void Render()
{
if(bordersize != 0)
Render_Border();
Render_Bg();
// don't even ask about the next section of code!
if(GetTickCount() - cursor_count > 700 && focus)
{
if(first)
{
cursor_count_on = GetTickCount();
first = false;
}
if(cursor_on)
{
if(GetTickCount() - cursor_count_on > 700)
{
cursor_on = false;
first = true;
cursor_count = GetTickCount();
}
Render_Cursor();
}
else
{
if(GetTickCount() - cursor_count_on > 700)
{
cursor_on = true;
first = true;
cursor_count = GetTickCount();
}
Render_Cursor();
}
}
Render_Text();
}
private:
void Render_Border();
void Render_Cursor();
void Render_Bg();
void Render_Text();
void Update_Offset();
DWORD cursor_count;
DWORD cursor_count_on;
int x;
int y;
int text_offset; // for the Render_Text() func
int cursorpos;
int maxchars;
int width;
int height;
bool focus;
bool cursor_on;
bool first;
COLORREF textcolor;
char *contents;
USHORT bgcolor;
USHORT bordercolor;
int bordersize;
LPDIRECTDRAWSURFACE7 lpdds;
HFONT font;
};
and the code for it:
int CField :: Init(int f_maxchars, USHORT f_bgcolor, USHORT f_bordercolor, COLORREF f_textcolor, int f_bordersize, LPDIRECTDRAWSURFACE7 f_lpdds, HFONT f_font, int f_x, int f_y)
{
LPTEXTMETRIC lptm;
HDC hdc;
f_lpdds->GetDC(&hdc);
// get the max width of character
HFONT prev = (HFONT)SelectObject(hdc, f_font);
GetTextMetrics(hdc, lptm);
width = lptm->tmAveCharWidth * f_maxchars;
height = lptm->tmHeight;
SelectObject(hdc, prev);
// release the dc
f_lpdds->ReleaseDC(hdc);
contents = new char[f_maxchars+1]; // allocate memory for chars
if(!contents) // opps! not enought memory, return fail
return FAIL;
memset(contents, NULL, sizeof(contents)); // init array of chars
bgcolor = f_bgcolor; // setup other fields
bordercolor = f_bordercolor;
bordersize = f_bordersize;
maxchars = f_maxchars;
textcolor = f_textcolor;
x = f_x;
y = f_y;
lpdds = f_lpdds;
font = f_font;
text_offset = 0;
cursor_count = GetTickCount();
first = true;
GiveFocus();
return SUCCESS; // return success
}
void CField :: Release()
{
delete []contents; // dealloc memory allocated by the Init func
}
void CField :: PutStr(char *f_char)
{
if(strlen(contents)+strlen(f_char) <= maxchars)
{
strcpy(contents+cursorpos, strcat(f_char, contents+cursorpos));
Update_Offset();
cursorpos++;
}
}
void CField :: Clear()
{
memset(contents, NULL, sizeof(contents));
cursorpos = 0;
}
void CField :: Render_Border()
{
DDBLTFX ddbltfx;
RECT border = {x, y, x+width+(2*bordersize), y+height+(2*bordersize)};
ddbltfx.dwSize = sizeof(ddbltfx);
ddbltfx.dwFillColor = bordercolor;
// ready to blt to surface
lpddswork->Blt(&border, // ptr to dest rectangle
NULL, // ptr to source surface, NA
NULL, // ptr to source rectangle, NA
DDBLT_COLORFILL / DDBLT_WAIT, // fill and wait
&ddbltfx); // ptr to DDBLTFX structure
}
void CField :: Render_Bg()
{
DDBLTFX ddbltfx;
RECT border = {x+bordersize, y+bordersize, x+width+bordersize, y+height+bordersize};
ddbltfx.dwSize = sizeof(ddbltfx);
ddbltfx.dwFillColor = bgcolor;
// ready to blt to surface
lpddswork->Blt(&border, // ptr to dest rectangle
NULL, // ptr to source surface, NA
NULL, // ptr to source rectangle, NA
DDBLT_COLORFILL / DDBLT_WAIT, // fill and wait
&ddbltfx); // ptr to DDBLTFX structure
}
void CField :: Render_Text()
{
Draw_Text(contents+text_offset, textcolor, x+bordersize+2, y+bordersize, lpdds, font);
}
void CField :: Backspace()
{
if(cursorpos != 0)
{
for(int y = cursorpos; y < maxchars+1; y++)
contents[y-1] = contents[y];
cursorpos--;
}
}
void CField :: Render_Cursor()
{
SIZE size;
HDC hdc;
lpdds->GetDC(&hdc);
HFONT prev = (HFONT)SelectObject(hdc, font);
GetTextExtentPoint32(hdc, contents, cursorpos, &size);
SelectObject(hdc, prev);
// release the dc
lpdds->ReleaseDC(hdc);
DDBLTFX ddbltfx;
RECT border = {x+size.cx+bordersize+2, y+2+bordersize, x+size.cx+2+bordersize+2, y+height-2+bordersize};
ddbltfx.dwSize = sizeof(ddbltfx);
ddbltfx.dwFillColor = RGB16(0,0,0);
// ready to blt to surface
lpddswork->Blt(&border, // ptr to dest rectangle
NULL, // ptr to source surface, NA
NULL, // ptr to source rectangle, NA
DDBLT_COLORFILL / DDBLT_WAIT, // fill and wait
&ddbltfx); // ptr to DDBLTFX structure
}
void CField :: Update_Cursor_Pos(int f_pos)
{
if(f_pos <= strlen(contents) && abs(f_pos) == f_pos ) // abs to test if negative or not
{
cursorpos = f_pos;
}
}
void CField :: Update_Offset()
{
SIZE size;
HDC hdc;
lpdds->GetDC(&hdc);
HFONT prev = (HFONT)SelectObject(hdc, font);
GetTextExtentPoint32(hdc, contents, cursorpos, &size);
SelectObject(hdc, prev);
// release the dc
lpdds->ReleaseDC(hdc);
if(size.cx > width)
{
//text_offset++;
}
}
and also my call to the Init function:
testfield.Init(20, RGB16(200,200,200), RGB16(0,0,0), RGB(0,0,0), 2, lpddswork, CreateFont(18,0,0,0,FW_BOLD,0,0,0,0,0,0,0,0, "Arial"), 100,100);
Someone must be able to help me, I must have made some mistake.
Visit our web site:
Asylum Entertainment
Edited by - ziplux on 5/26/00 10:54:17 PM