c++實現用戶密碼輸入界面

#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <bits/stdc++.h>
using namespace std;
void gotoxy(int x,int y)
{
    COORD c;
    c.X=x-1;
    c.Y=y-1;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}

int getCursorLine(){ // 獲取光標所在的行號
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (GetConsoleScreenBufferInfo(hConsole, &csbi))
    {
        return csbi.dwCursorPosition.Y;
    }
}

int getCursorColumn(){ // 獲取光標所在的列號
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (GetConsoleScreenBufferInfo(hConsole, &csbi))
    {
        return csbi.dwCursorPosition.X;
    }
}

void inputNotDisplayed(string &t){
    char c ;
    int i=0 ;
    int line = getCursorLine();
    while((c=getch() ) != '\r') {
        if(c=='\b' ) {
            if(i > 0){
                printf("\b") ;
                putchar(' ') ;
                gotoxy(i , line+1) ;  // 這裏求得的光標所在位置的行數要加一
                i-- ;
            }
            continue ;
        }
        t += c;
        i++ ;
        putchar('*') ;
    }
}

int main()
{
    string passWd;
    cout << "  請輸入您的密碼" << endl;
    inputNotDisplayed(passWd);
    cout << "  您的密碼是 : " << passWd << endl;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章