65.9K
CodeProject 正在变化。 阅读更多。
Home

AutoStart - 在启动 Visual Studio 时自动启动应用程序

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (2投票s)

2007年5月15日

CPOL

1分钟阅读

viewsIcon

26980

downloadIcon

226

这是一个适用于 Visual Studio 2003 和 2005 的插件,它会在启动 Visual Studio 时自动启动您定义的应用程序。

引言

当我打开 Visual Studio 进行编码工作时,我经常需要运行几个实用工具来帮助我轻松愉快地完成工作。我编写了这个非常简单的插件来自动执行这些操作,在启动 Visual Studio 时,它会从 ini 文件加载信息,并启动我需要的实用工具,并在退出 Visual Studio 时关闭它们。

使用代码

代码非常简单,只有一个类和几个函数。

首先是类 CAppInfo,它描述了我们想要运行的应用程序

class CAppInfo
{
public:
 string strWindowTitle;    // the main window title
 string strAppPath;    // the application's exe file path
 BOOL bLoaded;    // indicate if the application is launch by this addin
 CAppInfo & operator =(CAppInfo & ai)
 {
  strWindowTitle = ai.strWindowTitle;
  strAppPath = ai.strAppPath;
  bLoaded = ai.bLoaded;
  return * this;
 }
};

CConect 类中有一个变量和四个函数

protected:
    list<cappinfo> AppList;    // store the applications info loaded from an ini file

    void LoadAppInfo(LPCTSTR strIniFile);    // load applications info from ini file
    void LaunchApp(CAppInfo & ai);    // launch an application
    void StartApp(LPCTSTR strIniFile);    // load applications info from ini file and launch then all
    void EndApp(void);    // close all launched applications 

它们的功能在注释中描述。

最后,在 CConnect 类中,在适当的时间调用我们的函数

STDMETHODIMP CConnect::OnStartupComplete (SAFEARRAY ** /*custom*/ )
{
    StartApp(STRINIFILE);
    return S_OK;
}

STDMETHODIMP CConnect::OnBeginShutdown (SAFEARRAY ** /*custom*/ )
{
    EndApp();
    return S_OK;
}

就是这样。

ini 文件

它使用名为 AutoStart.ini 的 ini 文件来描述应用程序信息,必须将其放在您的系统文件夹中(C:\\windows 或类似位置)。

如果您想启动“n”个应用程序,它必须有 n 个节。这些节命名为 [Application 1],[Application 2],...,[Application n],每个节描述一个应用程序。下面是一个示例

[Application 1]
ClassName=AfxFrameOrView42s
WindowTitle="Perfect Keyboard - cpp.4pk"
AppPath="E:\Program Files\Perfect Keyboard AS\pk32.exe"
[Application 2]
ClassName=HamsinClipboardClassName
AppPath="E:\Program Files\Hamsin Clipboard\HamsinClipboard.exe"

我们使用“ClassName”和“WindowTitle”来查找应用程序的主窗口,以便在退出 Visual Studio 时通过发送 WM_CLOSE 消息来关闭它,因此它们是可选的。

我们需要“AppPath”来启动应用程序,所以它是必要的。

历史

2007-05-16 发布原始版本。

© . All rights reserved.