How to detect screen resolution and set wallpaper

This is a discussion on How to detect screen resolution and set wallpaper within the Windows Programming forums, part of the Platform Specific Boards category; I've been playing around with C++, and decided on a simple app to change wallpaper depending on screen resolution, since ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    How to detect screen resolution and set wallpaper

    I've been playing around with C++, and decided on a simple app to change wallpaper depending on screen resolution, since at college(sixth form) some of our computers are 16:9 and others are 4:3.

    What I want to know is what Windows API calls do I need to make in order to achieve this? And also what syntax? (e.g. Resolution(width, height)), I've had a look on google and here, but I'm still a little mystified. If there is anything compiler specific, I'm using GCC.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    > I've had a look on google

    Really ?!
    What did you type ?
    Found in 0.25 seconds in Google Groups
    ... and MSDN of course with complete application in SDK which does that.
    (SystemParametersInfo, EnumDisplaySettingsEx, and so on)
    Last edited by Alex31; 10-14-2009 at 07:49 AM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Yes, I found info, and I think I have an idea, and I'm about to try something, which I found on Google and if it doesn't work I will post source code to find out what's wrong, happy?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Ok, several compiling errors, I assume I have something fundamentally wrong in my code, any ideas? I'm not asking to be spoon fed, just a pointer or two.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
    	int cx = GetSystemMetrics(SM_CXSCREEN);
    	/*int cy = GetSystemMetrics(SM_CYSCREEN)*/
    
    	if (cx = 1440)
    	{
    		SystemParametersInfo(SPI_SETDESKWALLPAPER,TRUE,(LPVOID)"E:\\my_docs\\test.bmp",SPIF_SENDWININICHANGE| SPIF_UPDATEINIFILE);
    	}
    	else
    	{
    		SystemParametersInfo(SPI_SETDESKWALLPAPER,TRUE,(LPVOID)"E:\\my_docs\\test2.bmp",SPIF_SENDWININICHANGE| SPIF_UPDATEINIFILE);
    	}
    }
    Compile Errors:
    H:\Projects\C++\wallpaper.cpp|6|error: `main' must return `int'|
    H:\Projects\C++\wallpaper.cpp||In function `int main(...)':|
    H:\Projects\C++\wallpaper.cpp|7|error: `SM_CXSCREEN' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|7|error: `GetSystemMetrics' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|12|error: `SPI_SETDESKWALLPAPER' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|12|error: `TRUE' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|12|error: `LPVOID' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|12|error: `SPIF_SENDWININICHANGE' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|12|error: `SPIF_UPDATEINIFILE' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|12|error: `SystemParametersInfo' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|16|error: `SPI_SETDESKWALLPAPER' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|16|error: `TRUE' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|16|error: `LPVOID' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|16|error: `SPIF_SENDWININICHANGE' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|16|error: `SPIF_UPDATEINIFILE' was not declared in this scope|
    H:\Projects\C++\wallpaper.cpp|16|error: `SystemParametersInfo' was not declared in this scope|
    ||=== Build finished: 15 errors, 0 warnings ===|

    It appears that it is trying to read the various declarations as variables, but they're not.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Code:
    #pragma comment(lib, "user32.lib")
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main(void)
    {
    	int cx = GetSystemMetrics(SM_CXSCREEN);
    	/*int cy = GetSystemMetrics(SM_CYSCREEN)*/
    
    	if (cx = 1440)
    	{
    		SystemParametersInfo(SPI_SETDESKWALLPAPER,TRUE,(LPVOID)"E:\\my_docs\\test.bmp",SPIF_SENDWININICHANGE| SPIF_UPDATEINIFILE);
    	}
    	else
    	{
    		SystemParametersInfo(SPI_SETDESKWALLPAPER,TRUE,(LPVOID)"E:\\my_docs\\test2.bmp",SPIF_SENDWININICHANGE| SPIF_UPDATEINIFILE);
    	}
    }

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Of course, I hadn't realised I needed Windows specific libraries having not used them before. Thanks. Forgive my complete and utter ignorance, still learning, having come from a mostly VB.NET environment, and I'm a novice there as well. :P

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Change screen resolution and set deskop wallpaper

    Check the following solutins:
    Change screen resolution: C++ Builder Tips and Tricks: How to change screen settings?
    Change the desktop background: C++ Builder Tips and Tricks: How to change the desktop background?

  8. #8

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    5
    hi! please help! the above code doesn't work (do anything) for me

    I've searched on the internets and have tried both

    Code:
    HKEY hKey;
    DWORD dwDisposition;
    char szData[50]="c:\\kovtunets2.bmp";
    
    RegCreateKeyEx(HKEY_CURRENT_USER, _T("Control Panel\\Desktop"), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
    RegSetValueEx(hKey, _T("Wallpaper"), 0, REG_SZ, (LPBYTE)szData, sizeof(szData));
    RegCloseKey(hKey);



    and

    Code:
    int main()
    {
    SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, "c:\\kovtunets2.bmp", SPIF_UPDATEINIFILE );
    }

    each file complies however neither does anything

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    This was astoundingly easy to find using Google. I just searched for SPI_SETDESKWALLPAPER.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by yuriythebest View Post
    I've searched on the internets
    All three of them?
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screen Resolution
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-25-2003, 05:43 PM
  2. Console Screen Resolution!!!!
    By Perica in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2002, 07:45 AM
  3. How to set screen resulution and...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 11:44 AM
  4. MSVC++ allegro tutorial
    By bobish in forum Game Programming
    Replies: 5
    Last Post: 02-13-2002, 03:11 PM
  5. FAQ: MSVC++ allegro tutorial
    By bobish in forum FAQ Board
    Replies: 3
    Last Post: 02-09-2002, 06:48 PM

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