控制台下心仪进度条许久,正赶上做编译,花半天把它做出来。不敢藏私,大家一起用,有何bug请指正。
#define  _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
#include <string>
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#using <mscorlib.dll>
#using <System.dll>
using namespace std;
using namespace System;string Scheduelf(int sche,DWORD delay=0,bool fastmode=true){
if(sche<0||sche>100) return "out range";
static int lastsche=sche;
if(lastsche==sche) return "ok";
Console::CursorVisible=false;
int cursorxoffset=2;//进度条离开光标的横向位置
Console::CursorLeft+=cursorxoffset;
int xxx = Console::CursorLeft;
int yyy = Console::CursorTop;
HWND hwnd = GetConsoleWindow();
HDC hdc = GetWindowDC( hwnd );
HPEN hpen = CreatePen( PS_SOLID, 1, RGB(0,0,0) );
HBRUSH hbrush = CreateSolidBrush( RGB(255,255,255) );
HPEN hpen_old = (HPEN)SelectObject( hdc, hpen );
HBRUSH hbrush_old = (HBRUSH)SelectObject( hdc, hbrush );
RECT rect;
rect.left=(xxx+2)*8;//内定光标向右偏移2位置,此值和进度值位置联系,不可改
rect.top=(yyy+1)*16+12;//调整好左上位置
rect.right=rect.left+100;
rect.bottom=rect.top+15;//进度条长100、宽15,宽小于字体高+行距,避免被下一行抹掉
FrameRect(hdc,&rect,hbrush);//边框,宽2像素
GdiFlush();

for(int i=0;i<sche+1;++i){
if(fastmode==true) i=sche;
if(i>=0 && i<=9){
Console::CursorLeft+=7;
Console::Write(i);
Console::Write("%");
Console::CursorLeft-=7+2;//i宽1+1个“%”号
FrameRect(hdc,&rect,hbrush);
GdiFlush();
}
if(i>=10 && i<=99){
Console::CursorLeft+=7;
Console::Write(i);
Console::Write("%");
Console::CursorLeft-=7+3;//i宽2+1个“%”号
FrameRect(hdc,&rect,hbrush);
GdiFlush();
}
if(i==100){
Console::CursorLeft+=6;
Console::Write(i);
Console::Write("%");
Console::CursorLeft-=6+4;//i宽3+1个“%”号
FrameRect(hdc,&rect,hbrush);
GdiFlush();
}
for(int ix=1;ix<i-1;++ix){
for(int iy=0;iy<15-1-1;++iy){
if( GetPixel(hdc,rect.left+ix,rect.top+1+iy)==RGB(0,0,0) ){//黑变蓝
SetPixel(hdc,rect.left+ix,rect.top+1+iy,RGB(0,0,255));
GdiFlush();
}
if( GetPixel(hdc,rect.left+ix,rect.top+1+iy)!=RGB(0,0,255) ){//非蓝变黄
SetPixel(hdc,rect.left+ix,rect.top+1+iy,RGB(252,255,113));
GdiFlush();
}
}
}
lastsche=sche;
Sleep(delay);
}
SelectObject( hdc, hpen_old ); 
SelectObject( hdc, hbrush_old );
ReleaseDC( hwnd, hdc );
Console::CursorVisible=true;
if(sche==100) Console::CursorLeft+=15;
else Console::CursorLeft-=cursorxoffset;
return "ok";
}bool judge(unsigned __int64 num){
if(num==1) return false;
if(num==2) return true;
unsigned __int64 root=sqrt( (unsigned long double)num );
for(unsigned __int64 i=2;i<=root+1;++i){//root用int64避免root+1溢出
if(num%i==0) return false;
Scheduelf(i*100/(root+1));
}
return true;
}void main(){
cout << "this program will calc the next prime number  (0--exit)";
cout << "\nplease input a number ";
for(unsigned __int64 num;cin >> num;cout << "\nplease input a number "){
if(num==0) return;
for(;;){
if( judge(num) ) {cout << num << " is a prime number!!";break;}
num+=1;
}
}
}
//void main(int argc,char* argv[]){//另外一个运用例子
// cout<<" please wait...";
// int sche=0;
// if(argc==2) sche=atoi(argv[1]);
// cout<<Scheduelf(sche,20);
//
//}
补充:本来获取光标位置想用CONSOLE_SCREEN_BUFFER_INFO()来着,看了半天没搞惦,不得已用了Console类,这个东西用/clr,可这样一来就与使用控制台的初衷背离了...
可运行程序见 luobonic.ys168.com war3目录下