没有控制台打开的C ++启动程序

时间:2021-11-24 00:04:13

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.

我一直在研究一个应用程序来设置我在这里找到的另一个应用程序的桌面背景:http://www.optimumx.com/downloads.html#SetWallpaper。我的想法是每10分钟将背景设置为一个壁纸,所以它使用命令'SetWallpaper.exe / D:S Wallpaper.jpg'启动SetWallpaper.exe但是当我启动我的应用程序时,它会创建一个没有的控制台窗口。 t自动关闭,当我手动关闭它时,它会杀死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.

我尝试使用MinGW Msys附带的'sleep.exe'但是每个团队都创建了一个新进程,最终占用了所有进程。

Thanks in advance!

提前致谢!

3 个解决方案

#1


8  

The first problem you're having is that you've created your program as a console application with a main method. Instead, create it as a Win32 Project with a WinMain entry point. This will be invoked directly without creating a console window.

您遇到的第一个问题是您已使用main方法将程序创建为控制台应用程序。而是将其创建为具有WinMain入口点的Win32项目。这将在不创建控制台窗口的情况下直接调用。

EDIT: The second issue is addressed by Ferruccio's answer in that you're invoking another console application from yours which will also result in a console window being created.

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

#2


6  

You're going about it the hard way. It's fairly simple to change the Windows wallpaper in a program:

你正在努力解决这个问题。在程序中更改Windows壁纸非常简单:

#include <windows.h>

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

In any case, if you insist on launching an external program to do it. Use CreateProcess. It has the ability to launch console mode apps without a visible window, by setting the dwCreationFlags parameter to CREATE_NO_WINDOW.

在任何情况下,如果你坚持启动外部程序来做它。使用CreateProcess。通过将dwCreationFlags参数设置为CREATE_NO_WINDOW,它可以在没有可见窗口的情况下启动控制台模式应用程序。

#3


2  

Set ShowWindow to false and don't forget to FreeConsole at the end.

将ShowWindow设置为false,最后不要忘记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;
}

And as Ferruccio mentioned, You can use SetTimer and SystemParametersInfo to trigger a change periodically.

正如Ferruccio所提到的,您可以使用SetTimer和SystemParametersInfo定期触发更改。

#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;
}

#1


8  

The first problem you're having is that you've created your program as a console application with a main method. Instead, create it as a Win32 Project with a WinMain entry point. This will be invoked directly without creating a console window.

您遇到的第一个问题是您已使用main方法将程序创建为控制台应用程序。而是将其创建为具有WinMain入口点的Win32项目。这将在不创建控制台窗口的情况下直接调用。

EDIT: The second issue is addressed by Ferruccio's answer in that you're invoking another console application from yours which will also result in a console window being created.

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

#2


6  

You're going about it the hard way. It's fairly simple to change the Windows wallpaper in a program:

你正在努力解决这个问题。在程序中更改Windows壁纸非常简单:

#include <windows.h>

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

In any case, if you insist on launching an external program to do it. Use CreateProcess. It has the ability to launch console mode apps without a visible window, by setting the dwCreationFlags parameter to CREATE_NO_WINDOW.

在任何情况下,如果你坚持启动外部程序来做它。使用CreateProcess。通过将dwCreationFlags参数设置为CREATE_NO_WINDOW,它可以在没有可见窗口的情况下启动控制台模式应用程序。

#3


2  

Set ShowWindow to false and don't forget to FreeConsole at the end.

将ShowWindow设置为false,最后不要忘记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;
}

And as Ferruccio mentioned, You can use SetTimer and SystemParametersInfo to trigger a change periodically.

正如Ferruccio所提到的,您可以使用SetTimer和SystemParametersInfo定期触发更改。

#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;
}