5

I have been working on an application to set the desktop background basing of another application I found here: http://www.optimumx.com/downloads.html#SetWallpaper. The idea is to set the background to a wallpaper every 10 minutes, so it launches the SetWallpaper.exe with the command 'SetWallpaper.exe /D:S Wallpaper.jpg' but when I launch my application it creates a console window that doesn't automatically close and when I manually close it, it kills the exe.

#include <windows.h>
int main() {
int i = 1;
int j = 3;
// refresh = time until refresh in minutes
int refresh = 10;
// 1000 milliseconds = 1 second
int second = 1000;
int minute = 60;
int time = second * minute * refresh;
while (i < j) {
system("cmd /c start /b SetWallpaper.exe /D:S Wallpaper.jpg");
Sleep(time);
}
return 0;
}

I tried using 'sleep.exe' that comes with MinGW Msys but that creates a new process each team, eventually hogging all the processes.

Thanks in advance!

4

3 回答 3

8

您遇到的第一个问题是您已将程序创建为带有main方法的控制台应用程序。相反,将其创建为Win32 Project带有WinMain入口点的。这将直接调用而不创建控制台窗口。

编辑: Ferruccio 的回答解决了第二个问题,因为您正在调用另一个控制台应用程序,这也将导致创建控制台窗口。

于 2012-10-05T13:19:53.097 回答
6

你正在努力解决它。在程序中更改 Windows 壁纸相当简单:

#include <windows.h>

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) "path/to/wallpaper.jpg", SPIF_UPDATEINIFILE);

无论如何,如果您坚持要启动外部程序来做到这一点。使用CreateProcessdwCreationFlags通过将参数设置为 ,它能够在没有可见窗口的情况下启动控制台模式应用程序CREATE_NO_WINDOW

于 2012-10-05T14:48:18.880 回答
2

设置ShowWindowfalse并且不要忘记 FreeConsole 在最后。

#include <windows.h>


int main(void)
{

   ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);

   // put your code here

   system("cmd /c start /b SetWallpaper.exe /D:S Wallpaper.jpg");

   FreeConsole();

   return 0;
}

正如 Ferruccio 所说,您可以使用SetTimerSystemParametersInfo定期触发更改。

#define STRICT 1 
#include <windows.h>
#include <iostream.h>

VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) 
{

  LPWSTR wallpaper_file = L"C:\\Wallpapers\\wallpaper.png";
  int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, wallpaper_file, SPIF_UPDATEINIFILE);


  cout << "Programmatically change the desktop wallpaper periodically: " << dwTime << '\n';
  cout.flush();
}

int main(int argc, char *argv[], char *envp[]) 
{
    int Counter=0;
    MSG Msg;

    UINT TimerId = SetTimer(NULL, 0, 2000, &TimerProc); //2000 milliseconds = change every 2 seconds

    cout << "TimerId: " << TimerId << '\n';
   if (!TimerId)
    return 16;

   while (GetMessage(&Msg, NULL, 0, 0)) 
   {
        ++Counter;
        if (Msg.message == WM_TIMER)
        cout << "Counter: " << Counter << "; timer message\n";
        else
        cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
        DispatchMessage(&Msg);
    }

   KillTimer(NULL, TimerId);
return 0;
}
于 2012-10-05T18:05:25.983 回答