65,206
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <string.h>
#include <stdio.h>
class Label
{
public:
Label(int left, int top, int width, int height, char *caption, WORD _attrib);
void left(int left);
void top(int top);
void width(int width);
void height(int height);
void refresh(void);
private:
CHAR_INFO *buffer;
char *caption;
COORD bufCoord;
COORD bufSize;
SMALL_RECT site;
WORD attrib;
};
Label::Label(int left, int top, int width, int height, char *_caption, WORD _attrib)
{
bufCoord.X = 0;
bufCoord.Y = 0;
bufSize.X = width;
bufSize.Y = height;
site.Left = left;
site.Top = top;
site.Right = left + width - 1;
site.Bottom = top + height - 1;
buffer = new CHAR_INFO [width * height];
caption = new char [strlen(_caption)];
strcpy(caption, _caption);
attrib = _attrib;
refresh();
return;
}
void Label::left(int left)
{
site.Right += left - site.Left;
site.Left = left;
refresh();
return;
}
void Label::top(int top)
{
site.Bottom += top - site.Top;
site.Top = top;
refresh();
return;
}
void Label::width(int width)
{
bufSize.X = width;
site.Right = site.Left + width - 1;
refresh();
return;
}
void Label::height(int height)
{
bufSize.Y = height;
site.Bottom = site.Top + height - 1;
refresh();
return;
}
void Label::refresh(void)
{
CHAR_INFO *pb = buffer;
char *pc = caption;
int i;
for (; pb->Char.AsciiChar = *pc; pb++,pc++);
i = strlen(caption);
i = bufSize.X * bufSize.Y - i;
for (; i>0; pb++,i--)
pb->Char.AsciiChar = ' ';
for (i = bufSize.X * bufSize.Y, pb = buffer; i>0; pb++,i--)
pb->Attributes = attrib;
WriteConsoleOutput(
GetStdHandle(STD_OUTPUT_HANDLE),
buffer,
bufSize,
bufCoord,
&site);
return;
}
int main()
{
Label a(0,0,8,2,"abcdefghijklmnop",0x47);
for (int i=1; i<20; i++)
{
Sleep(100);
system("cls");
a.top (i);
Sleep(100);
system("cls");
a.left (i);
}
return (0);
}