簡體   English   中英

如何驗證有效的屏幕分辨率(在C / C ++中)

[英]How to validate valid screen resolution (in C/C++)

這是我要在用戶輸入時成對驗證的屏幕分辨率集。

Aspect ratio - 4:3
800  x 600
1024 x 768
1152 x 864
1280 x 960
etc..

目前這是我在想的:

int arrWidth[] = {800, 1024, 1152, 1280};
int arrHeight[] = {600, 768, 864, 960};
for ( int i = 0; i < sizeof(arrWidth)/sizeof(arrWidth[0]); ++i) {
    if ( pInputWidth == arrWidth[i] ) {
        if ( pInputHeight == arrHeight[i] ) {
            // both width and height is good
            return true;
        }
        // width good, but height doesn't match
        return false;
    }
}
// width doesn't match at all
return false;

有什么更好的方法嗎?

如果您只是想驗證哪些分辨率與給定的比率匹配,則可以將這些分辨率相乘並測試它們是否相等。 這是一個簡短的示例:

#include <stdio.h>

int main (void) {

    int ar[] = {4, 3},
        aw[] = {800, 1024, 1152, 1280, 1280},
        ah[] = {600, 768, 864, 960, 1024},
        n = sizeof aw/sizeof *aw;

    for (int i = 0; i < n; i++)
        if (ar[0] * ah[i] == ar[1] * aw[i])   /* simple cross-comparison */
            printf (" resolution OK : %4d x %d\n", aw[i], ah[i]);

    return 0;
}

使用/輸出示例

$ ./bin/resolution
 resolution OK :  800 x 600
 resolution OK : 1024 x 768
 resolution OK : 1152 x 864
 resolution OK : 1280 x 960

未顯示1280 x 1024分辨率,因為它不是4:3 如果我錯過了您提出問題的意圖,請告訴我。 如果可能,請保持整數比較以避免某些浮點相等的陷阱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM