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:~# 

转载于:https://www.cnblogs.com/sinpo828/p/10678945.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值