ChangeDisplaySettings problems

This is a discussion on ChangeDisplaySettings problems within the Windows Programming forums, part of the Platform Specific Boards category; Consider this piece of code: Code: #include<windows.h> #include<stdio.h> #include<conio.h> DWORD w[] = {640,800,1024,1280}; DWORD h[] = {480,600, 768,1024}; int main(){ ...

  1. #1
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722

    ChangeDisplaySettings problems

    Consider this piece of code:
    Code:
    #include<windows.h>
    #include<stdio.h>
    #include<conio.h>
    
    DWORD w[] = {640,800,1024,1280};
    DWORD h[] = {480,600, 768,1024};
    
    int main(){
    	DEVMODE d;
    	int i;
    
    	ZeroMemory(&d, sizeof(DEVMODE));
    
    	d.dmSize = sizeof(DEVMODE);
    	d.dmDisplayFlags = DM_PELSWIDTH	| DM_PELSHEIGHT | DM_DISPLAYFLAGS;
    
    	for(i=0;i<4;i++){
    		printf("mode: %d x %d -> ",w[i],h[i]);
    		d.dmPelsHeight = h[i];
    		d.dmPelsWidth = w[i];
    		switch(ChangeDisplaySettings(&d,0)){
    		case DISP_CHANGE_SUCCESSFUL:
    			puts("SUCCESS");break;
    		case DISP_CHANGE_RESTART:
    			puts("RESTART");break;
    		case DISP_CHANGE_BADFLAGS:
    			puts("BADFLAGS");break;
    		case DISP_CHANGE_FAILED:
    			puts("FAILED");break;
    		case DISP_CHANGE_BADMODE:
    			puts("BADMODE");break;
    		case DISP_CHANGE_NOTUPDATED:
    			puts("NOTUPDATED");break;
    		default:
    			puts("ERROR");break;
    		}
    		getch();
    	}
    }
    My current desktop resolution is 1280x1024. I can't change to a lower one. If change the resolution in my monitor properties, and then run the program, when the resolution should change to 800x600, it changes to 1280x1024 directly.
    But worst of all, the ChangeDisplaySetting function always returns DISP_CHANGE_SUCCESSFUL.
    1# What's wrong with this??
    2# Is there another good way to change my desktop resolution??

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,668
    When a Win32 API isn't behaving as you expect, go back and [re]read the documentation.
    From MSDN: ChangeDisplaySettings Remarks
    To ensure that the DEVMODE structure passed to ChangeDisplaySettings is valid and contains only values supported by the display driver, use the DEVMODE returned by the EnumDisplaySettings function.
    You can use this (C++) code to find the best display mode for a particular width, height, and bit depth:
    Code:
    bool FindBestDisplayMode(DEVMODE &best, DWORD width, DWORD height, DWORD bpp)
    {
        DEVMODE dm = {0};
        dm.dmSize = sizeof(DEVMODE);
    
        DWORD n, maxFreq = 0;
        bool found = false;
    
        for (n = 0; EnumDisplaySettings(NULL, n, &dm); n++)
        {
            if ((dm.dmPelsWidth == width) && 
                (dm.dmPelsHeight == height) &&
                (dm.dmBitsPerPel == bpp))
            {
                found = true;
    
                if (dm.dmDisplayFrequency > maxFreq)
                {
                    maxFreq = dm.dmDisplayFrequency;
                    best = dm;
                }//if
            }//if
        }//for
    
        return found;
    }//FindBestDisplayMode
    gg

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    I read that, but the flags field should limit the DEVMODE structure use only to the width and height...
    But now it worked fine: thanks !

    [edit]
    If I'm passing a invalid DEVMODE structure to the function, why does it succeed??
    Last edited by xErath; 09-15-2004 at 04:16 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,668
    >> If I'm passing a invalid DEVMODE structure to the function, why does it succeed??
    Buggy display driver perhaps? Who knows.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21