Linux 獲取屏幕分辨率與窗口行列數(c/c++)

獲取當前分辨率

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<fcntl.h>
#include<linux/fb.h>

int main(int argc,char *argv[]){
    int fd;                                                                              
    struct fb_var_screeninfo screen_info;
    fd = open("/dev/fb0",O_RDWR);
    ioctl(fd,FBIOGET_VSCREENINFO,&screen_info);
    printf("%d*%d\n",screen_info.xres,screen_info.yres);
    close(fd);
    return 0;
}

結果:

root@cocktail:~# vim screen_xy.c 
root@cocktail:~# ./screen_xy 
1152*864
root@cocktail:~# 

獲取當前窗口大小

這裏大小指的是一個滿屏幕的窗口,按照當前字體大小所能顯示的字體的行數與列數。
如果調整字體大小,結果會根據字體大小變化。這一結論在ssh工具界面也適用。

#include<stdio.h>
#include<stdlib.h>
#include<sys/ioctl.h>
#include<termios.h>
#include<signal.h>
#include<unistd.h>

int main(int argc,char *argv[]){                                                         
    struct winsize info;
    ioctl(STDIN_FILENO,TIOCGWINSZ,&info);
    printf("當前終端爲%d行%d列\n",info.ws_row,info.ws_col);
    return 0;
}

結論:

root@cocktail:~# vim screen_wc.c 
root@cocktail:~# ./screen_wc
當前終端爲23行89列
root@cocktail:~# 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章