Windows工具包

· · 科技·工程

所需头文件:windows.h

1.修改字体:

void Font(int Size){
    CONSOLE_FONT_INFOEX cfi;
    cfi.cbSize=sizeof(cfi);
    cfi.nFont=0;
    cfi.dwFontSize.X=0;
    cfi.dwFontSize.Y=Size;
    cfi.FontFamily=FF_DONTCARE;
    cfi.FontWeight=FW_NORMAL;
    wcscpy(cfi.FaceName,L"Consolas");
    SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE),FALSE,&cfi);
}

2.修改字体颜色

void Color(int color){
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY|color);
}

3.清屏

注意:该功能本质为将字符替换,要求后续输出的字符数量不少于原有字符数量,若不满足以上要求,请使用 system("cls")

void Clean(){
    COORD pos={(short)0,(short)0};
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut,pos);
    return;
}

4.隐藏光标

void hidecur(){
    CONSOLE_CURSOR_INFO cur={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
}

5.键盘监测

注意:该功能只有观察功能,不能屏蔽字符,如需避免字符的影响,建议使用 conio.h 中的 getch() 屏蔽字符

可结合功能6使用

bool IsPressed(WORD word){
    return GetAsyncKeyState(word)<0;
}

6.字符转键位

WORD CharToKey(char ch){
    return VkKeyScan(ch);
}

7.设置窗口参数

注意:5个参数分别为窗口宽度、窗口宽度、窗口左上角X坐标、窗口左上角Y坐标、是否禁止用户调整窗口宽高(对坐标无影响,1为禁止)

void SetWindows(int width,int height,int LeftPos,int TopPos,bool lock){
    HWND hwnd=GetConsoleWindow();
    MoveWindow(hwnd,LeftPos,TopPos,width,height,SWP_NOMOVE|SWP_NOZORDER);
    if(lock){
        LONG style=GetWindowLong(hwnd,GWL_STYLE);
        style&=~(WS_SIZEBOX|WS_MAXIMIZEBOX);
        SetWindowLong(hwnd,GWL_STYLE,style);
    }
}

8.获取窗口参数

注意:

rect.top//窗口左上角X坐标
rect.bottom//窗口左上角Y坐标
rect.left//窗口右下角X坐标
rect.right//窗口右下角Y坐标
RECT GetWindows(){
    RECT rect;
    HWND hwnd=GetConsoleWindow();
    GetWindowRect(hwnd,&rect);
    return rect;
}

9.获取鼠标位置

注意:

point.x//鼠标X坐标
poiny.y//鼠标Y坐标
POINT GetMousePos(){
    POINT pos;
    GetCursorPos(&pos);
    return pos;
}

10.移动鼠标

注意:该功能表示在 t 秒内将鼠标移动到坐标 (x,y),需要配合功能9使用

void MouseMove(int x,int y,int t){
    POINT pos=GetMousePos();
    int xx=pos.x,yy=pos.y;
    for(int i=0;i<=t;i++) {
        double s=static_cast<double>(i)/t;
        int x_=xx+(x-xx)*s;
        int y_=yy+(y-yy)*s;
        SetCursorPos(x_,y_);
        Sleep(1);
    }return;
}

11.按下键盘

注意:可结合功能6使用

void KeyDown(WORD word){
    keybd_event(word,0,0,0);
}

12.松开键盘

注意:可结合功能6使用

void KeyUp(WORD word){
    keybd_event(word,0,KEYEVENTF_KEYUP,0);
}

13.限制鼠标运动

void SetMouseClip(int x1,int y1,int x2,int y2){
    RECT clipRect;
    clipRect.left=x1;
    clipRect.top=y1;
    clipRect.right=x2;
    clipRect.bottom=y2;
    ClipCursor(&clipRect);
}

14.判断某文件是否存在

注意:此程序无需<windows.h>,使用前确保包含<fstream>

bool FileExists(const char* name){
    std::ifstream file(name);
    return !file.fail();
}

15.调整窗口透明度

注意:Transparency \in [0,255] ,传值越小,透明度越大

void SetTransparency(int Transparency){
    HWND hwnd=GetConsoleWindow();
    SetWindowLong(hwnd,GWL_EXSTYLE,GetWindowLong(hwnd,GWL_EXSTYLE)|WS_EX_LAYERED);
    SetLayeredWindowAttributes(hwnd,0,Transparency,LWA_ALPHA);
}

16.从任务栏隐藏

void HideFromTaskbar(){
    HWND hwnd=GetConsoleWindow();
    LONG exStyle=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
    SetWindowLongPtr(hwnd,GWL_EXSTYLE,(exStyle|WS_EX_TOOLWINDOW)&~WS_EX_APPWINDOW);
}