純 C 語言實現圖形化歡迎界面較為罕見,通常採用字符界面 (TUI)或調用平台圖形庫。以下提供控制台版實現思路。
| 模塊 | 實現要點 |
|---|---|
| 歡迎動畫 | 逐字符輸出 + `Sleep()` 延時、ANSI 轉義序列控制顏色/光標 |
| 密碼驗證 | `getch()` 回顯 `*`、定次數重試、鎖定延時 |
| 界面佈局 | `gotoxy()` 封裝 (Windows API / ANSI)、邊框繪製 |
#include <windows.h>
#include <conio.h>
void gotoxy(int x, int y) {
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void typewriter(const char* str, int x, int y, int delay_ms) {
gotoxy(x, y);
for (int i = 0; str[i]; i++) {
putchar(str[i]);
fflush(stdout);
Sleep(delay_ms);
}
}
bool verifyPassword(const char* correct, int max_try) {
char input[64];
for (int attempt = 1; attempt <= max_try; attempt++) {
printf("密碼 (%d/%d): ", attempt, max_try);
int i = 0;
while (i < 63) {
int ch = _getch();
if (ch == '\r' || ch == '\n') break;
if (ch == 8) { if (i) { i--; printf("\b \b"); } continue; }
input[i++] = ch;
putchar('*');
}
input[i] = '\0';
putchar('\n');
if (strcmp(input, correct) == 0) return true;
printf("錯誤!剩餘 %d 次機會\n", max_try - attempt);
}
return false;
}
int main() {
system("cls"); // 清屏
setColor(14); // 黃色
typewriter("歡迎使用學生信息管理系統", 20, 5, 80);
Sleep(500);
if (verifyPassword("admin123", 3)) {
setColor(10); printf("\n驗證成功,進入系統...\n");
// 進入主菜單
} else {
setColor(12); printf("\n嘗試次數耗盡,程序退出\n");
}
return 0;
}