jeg skrev det med debian for at i kunne få et exempel på hvad jeg skulle lave, altså en loader bar...
******************
*Fishing is a lifestyle*
*Like Computer is *
******************
OK.
Hvis du kan holde det til en enkelt linje så virker "\\r" udemærket. "\\r" hopper tilbage til starten af den nuværende linje og så kan du skrive linjen igen. Hvis du skal skrive på andre linjer så er Windows API'et det rigtige valg (under Windows).
Du har bl.a.:
BOOL SetConsoleCursorPosition(
HANDLE hConsoleOutput,
COORD dwCursorPosition
);
typedef struct _COORD {
SHORT X;
SHORT Y;
} COORD;
BOOL GetConsoleScreenBufferInfo(
HANDLE hConsoleOutput,
PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
);
typedef struct _CONSOLE_SCREEN_BUFFER_INFO {
COORD dwSize;
COORD dwCursorPosition;
WORD wAttributes;
SMALL_RECT srWindow;
COORD dwMaximumWindowSize;
} CONSOLE_SCREEN_BUFFER_INFO;
Jeg hakkede lige hurtigt følgende sammen:
#include <windows.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void clear(HANDLE out) {
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
GetConsoleScreenBufferInfo( out, &csbi );
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter( out, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten );
GetConsoleScreenBufferInfo( out, &csbi );
FillConsoleOutputAttribute( out, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten );
}
void writeBox(HANDLE out) {
SYSTEMTIME tm;
CONSOLE_SCREEN_BUFFER_INFO screenBuffer;
SHORT midX, midY;
COORD coord;
int i;
clear(out);
GetConsoleScreenBufferInfo(out,&screenBuffer);
midX = ((screenBuffer.srWindow.Right - screenBuffer.srWindow.Left) / 2) + screenBuffer.srWindow.Left;
midY = ((screenBuffer.srWindow.Bottom - screenBuffer.srWindow.Top) / 2) + screenBuffer.srWindow.Top;
//Write top line
coord.X = midX - 11;
coord.Y = midY - 1;
SetConsoleCursorPosition(out,coord);
cout << ((char)201);
for(i = 0; i < 20; i++)
cout << ((char)205);
cout << ((char)187);
//Write middle lines
coord.X = midX - 11;
coord.Y = midY;
SetConsoleCursorPosition(out,coord);
cout << ((char)186);
coord.X = midX + 10;
SetConsoleCursorPosition(out,coord);
cout << ((char)186);
//Write bottom line
coord.X = midX - 11;
coord.Y = midY + 1;
SetConsoleCursorPosition(out,coord);
cout << ((char)200);
for(i = 0; i < 20; i++)
cout << ((char)205);
cout << ((char)188);
GetLocalTime(&tm);
ostringstream oss;
oss << tm.wDay << "-" << tm.wMonth << "-" << tm.wYear << " ";
oss << tm.wHour << ":" << tm.wMinute << ":" << tm.wSecond;
string str = oss.str();
coord.X = midX - (str.length() / 2);
coord.Y = midY;
SetConsoleCursorPosition(out,coord);
cout << str;
}
bool again = true;
BOOL WINAPI ctrlHandler(DWORD ctrlType) {
switch(ctrlType) {
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
again = false;
break;
}
return TRUE;
}
int main(int, char **) {
HANDLE out;
CONSOLE_CURSOR_INFO cursor = {1,FALSE};
CONSOLE_CURSOR_INFO cursorInfo;
SetConsoleCtrlHandler(ctrlHandler,TRUE);
out = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleCursorInfo(out,&cursorInfo);
SetConsoleCursorInfo(out,&cursor);
while(again) {
writeBox(out);
Sleep(1000);
}
SetConsoleCursorInfo(out,&cursorInfo);
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
return 0;
}
Det viser Dato og tidspunkt og bliver opdateret hvert sekund.
[Redigeret d. 24/08-04 16:48:46 af Robert Larsen][Redigeret d. 25/08-04 17:22:16 af Robert Larsen]