C++ 中打开 exe 文件

使用 system 函数

#include <iostream>
#include<Windows.h>

int main()
{
    std::cout << "Hello World!\n";
	system("WPFDocumentPrint.exe");
	ShellExecuteA(NULL, "open", "WPFDocumentPrint.exe", NULL, NULL, SW_HIDE);
	system("pause");
	return 0;
}

该函数以命令行方式执行输入的命令。
注:该种方式在非命令行应用程序中会弹出命令行窗口。

ShellExecute 方法

HINSTANCE ShellExecuteA(
HWND hwnd,
LPCSTR lpOperation,
LPCSTR lpFile,
LPCSTR lpParameters,
LPCSTR lpDirectory,
INT nShowCmd
);

参数说明

hwnd 用于显示UI或错误信息的父窗口,可以为NULL
lpOperation 需要执行的操作类型,如下:

  • edit
    Launches an editor and opens the document for editing. If lpFile is not a document file, the function will fail.

  • explore
    Explores a folder specified by lpFile.

  • find
    Initiates a search beginning in the directory specified by lpDirectory.

  • open
    Opens the item specified by the lpFile parameter. The item can be a file or folder.

  • print
    Prints the file specified by lpFile. If lpFile is not a document file, the function fails.

  • runas
    Launches an application as Administrator. User Account Control (UAC) will prompt the user for consent to run the application elevated or enter the credentials of an administrator account used to run the application.

  • NULL
    The default verb is used, if available. If not, the “open” verb is used. If neither verb is available, the system uses the first verb listed in the registry.

常用的为 open ,对于以 exe 文件选择 open.

lpFile 文件对象,lpOperation 的操作目标对象。这里为 exe 文件路径。
lpDirectory 父路径。
nShowCmd 应用程序被打开的方式。
SW_HIDE (0)
Hides the window and activates another window.

SW_MAXIMIZE (3)
Maximizes the specified window.

SW_MINIMIZE (6)
Minimizes the specified window and activates the next top-level window in the z-order.

SW_RESTORE (9)
Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.

SW_SHOW (5)
Activates the window and displays it in its current size and position.

SW_SHOWDEFAULT (10)
Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.

SW_SHOWMAXIMIZED (3)
Activates the window and displays it as a maximized window.

SW_SHOWMINIMIZED (2)
Activates the window and displays it as a minimized window.

SW_SHOWMINNOACTIVE (7)
Displays the window as a minimized window. The active window remains active.

SW_SHOWNA (8)
Displays the window in its current state. The active window remains active.

SW_SHOWNOACTIVATE (4)
Displays a window in its most recent size and position. The active window remains active.

SW_SHOWNORMAL (1)
Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.

以上这些选项可以调整被打开的应用程序的窗体展开方式,主要用到SW_MAXIMIZE 以最大化方式打开,SW_MINIMIZE 最小化方式打开 ,SW_HIDE 以隐藏方式打开主窗体(对WPF应用程序无效) 。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值