如何在c(操作系统QNX或Linux)中获得屏幕分辨率

如何在c(操作系统QNX或Linux)中获得屏幕分辨率,c,C,我正在使用QNX(屏幕分辨率设计)进行GUI开发 如何在c中获得屏幕分辨率。 我使用的是QNX操作系统。 可能吗? 此解决方案是否有操作系统功能 谢谢假设您使用的设备具有帧缓冲区(并且具有根访问权限): (摘自此答案:) 此外,如上所述,您正在使用的图形库会产生很大的不同,因为此代码只会告诉您帧缓冲区设置为什么,而不会告诉您GUI代码正在使用什么。所以可能根本没用。如果您没有使用X或任何其他图形库,那么您可能需要使用帧缓冲区,您可以看到关于如何做到这一点的其余答案。(我强烈建议您使用Direct

我正在使用QNX(屏幕分辨率设计)进行GUI开发 如何在c中获得屏幕分辨率。 我使用的是QNX操作系统。 可能吗? 此解决方案是否有操作系统功能


谢谢

假设您使用的设备具有帧缓冲区(并且具有根访问权限): (摘自此答案:)

此外,如上所述,您正在使用的图形库会产生很大的不同,因为此代码只会告诉您帧缓冲区设置为什么,而不会告诉您GUI代码正在使用什么。所以可能根本没用。如果您没有使用X或任何其他图形库,那么您可能需要使用帧缓冲区,您可以看到关于如何做到这一点的其余答案。(我强烈建议您使用DirectFB,这将节省您实现大量代码的时间)

此外,您还可以使用在大多数设备(包括嵌入式设备)上出现的gl驱动程序,因此这也会影响您执行所需操作的方式

你在使用SOC吗?制造商是否有自己的驱动层?这可能会完全不同,并且可能会有自己的API来处理这个问题

但无论如何,我希望这能有所帮助

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

int main()
{
    int fbfd = 0;
    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    long int screensize = 0;
    char *fbp = 0;
    int x = 0, y = 0;
    long int location = 0;

    // Open the file for reading and writing
    fbfd = open("/dev/fb0", O_RDWR);
    if (fbfd == -1) {
        perror("Error: cannot open framebuffer device");
        exit(1);
    }
    printf("The framebuffer device was opened successfully.\n");

    // Get fixed screen information
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
        perror("Error reading fixed information");
        exit(2);
    }

    // Get variable screen information
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
        perror("Error reading variable information");
        exit(3);
    }

    printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);

    // Figure out the size of the screen in bytes
    //
    close(fbfd);
}   
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
int fbfd=0;
结构fb_var_screenfo vinfo;
结构fb\u fix\u屏幕信息finfo;
长整型屏幕尺寸=0;
char*fbp=0;
int x=0,y=0;
长整数位置=0;
//打开文件进行读写
fbfd=打开(“/dev/fb0”,O_RDWR);
如果(fbfd==-1){
perror(“错误:无法打开帧缓冲区设备”);
出口(1);
}
printf(“帧缓冲区设备已成功打开。\n”);
//获取固定屏幕信息
如果(ioctl(fbfd、FBIOGET_FSCREENINFO和finfo)=-1){
perror(“读取固定信息时出错”);
出口(2);
}
//获取可变屏幕信息
如果(ioctl(fbfd、FBIOGET\U VSCREENINFO和vinfo)=-1){
perror(“错误读取变量信息”);
出口(3);
}
printf(“%dx%d,%dbpp\n”,vinfo.xres,vinfo.yres,vinfo.bits每像素);
//计算屏幕的大小(以字节为单位)
//
关闭(fbfd);
}   

对于类unix操作系统,您可以使用库X11,但如果需要跨平台解决方案,请尝试GTK+

完整代码

// The C standart library
#include <stdlib.h>

// GTK+
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>

// X11
#include <X11/Xlib.h>


/*
    Printing a current screen resoltion by using the GTK+3
    https://en.wikipedia.org/wiki/GTK%2B
 */
int
print_screen_resolution_by_GTK(int argc, char *argv[])
{
    GdkScreen *screen;
    gint width, height;

    gtk_init(&argc, &argv);

    if ((screen = gdk_screen_get_default()) != NULL) {
        width = gdk_screen_get_width(screen);
        height = gdk_screen_get_height(screen);
        g_printf("Current screen resolution: %dx%d (by used GTK+)\n", width, height);
    }
    return 0;
}


/*
    Printing a current screen resoltion by using the libX11 (worked only for Unix-like OS)
    https://en.wikipedia.org/wiki/X_Window_System

    Based on:
        https://www.x.org/releases/X11R7.6/doc/libX11/specs/libX11/libX11.html
        http://surfingtroves.blogspot.com/2011/01/how-to-get-screen-resolution-in-linux-c.html
 */
int
print_display_resolution_by_X11()
{
    Display *display;
    Window window;
    XWindowAttributes xw_attrs;

    if ((display = XOpenDisplay(NULL)) == NULL) {
        fprintf(stderr, "Failed to open default display\n");
        return -1;
    }

    window = DefaultRootWindow(display);

    XGetWindowAttributes(display, window, &xw_attrs);

    printf("Current window resolution: %dx%d (by used X11)\n", xw_attrs.width, xw_attrs.height);

    XCloseDisplay(display);

    return 0;
}


int main(int argc, char *argv[])
{
    print_screen_resolution_by_GTK(argc, argv);
    print_display_resolution_by_X11();
    return EXIT_SUCCESS;
}
结果(实际适用于我的计算机)


您正在使用什么图形库?不确定,但可能对您的情况有所帮助
gcc -o main main.c `pkg-config --libs --cflags gtk+-3.0 x11`
Current screen resolution: 1366x768 (by used GTK+)
Current window resolution: 1366x768 (by used X11)