2012年3月13日 星期二

[VC(Visual C++) ]- 啟動和關閉外部程式

[VC(Visual C++) ]- 啟動和關閉外部程式



本篇分享啟動和關閉外部程式範例有興趣的(C/P)同好歡迎來http://filemarkets.com/file/jashliao/c857dfe9/索取因為我不會上傳檔案分享哈哈 ^ ^












主要程式碼:



001    // OPenCloseDlg.cpp : implementation file                

002    //                

003                    

004    #include "stdafx.h"                

005    #include "OPenClose.h"                

006    #include "OPenCloseDlg.h"                

007                    

008    #ifdef _DEBUG                

009    #definenew DEBUG_NEW                

010    #undef THIS_FILE                

011    staticchar THIS_FILE[] = __FILE__;                

012    #endif                

013                    

014    /////////////////////////////////////////////////////////////////////////////                

015    // CAboutDlg dialog used for App About                

016                    

017    class CAboutDlg : public CDialog                

018    {                

019    public:                

020        CAboutDlg();            

021                    

022    // Dialog Data                

023        //{{AFX_DATA(CAboutDlg)            

024        enum { IDD = IDD_ABOUTBOX };            

025        //}}AFX_DATA            

026                    

027        // ClassWizard generated virtual function overrides            

028        //{{AFX_VIRTUAL(CAboutDlg)            

029        protected:            

030        virtualvoid DoDataExchange(CDataExchange* pDX);    // DDX/DDV support            

031        //}}AFX_VIRTUAL            

032                    

033    // Implementation                

034    protected:                

035        //{{AFX_MSG(CAboutDlg)            

036        //}}AFX_MSG            

037        DECLARE_MESSAGE_MAP()            

038    };                

039                    

040    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)                

041    {                

042        //{{AFX_DATA_INIT(CAboutDlg)            

043        //}}AFX_DATA_INIT            

044    }                

045                    

046    void CAboutDlg::DoDataExchange(CDataExchange* pDX)                

047    {                

048        CDialog::DoDataExchange(pDX);            

049        //{{AFX_DATA_MAP(CAboutDlg)            

050        //}}AFX_DATA_MAP            

051    }                

052                    

053    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)                

054        //{{AFX_MSG_MAP(CAboutDlg)            

055            // No message handlers        

056        //}}AFX_MSG_MAP            

057    END_MESSAGE_MAP()                

058                    

059    /////////////////////////////////////////////////////////////////////////////                

060    // COPenCloseDlg dialog                

061                    

062    COPenCloseDlg::COPenCloseDlg(CWnd* pParent /*=NULL*/)                

063        : CDialog(COPenCloseDlg::IDD, pParent)            

064    {                

065        //{{AFX_DATA_INIT(COPenCloseDlg)            

066            // NOTE: the ClassWizard will add member initialization here        

067        //}}AFX_DATA_INIT            

068        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32            

069        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);            

070    }                

071                    

072    void COPenCloseDlg::DoDataExchange(CDataExchange* pDX)                

073    {                

074        CDialog::DoDataExchange(pDX);            

075        //{{AFX_DATA_MAP(COPenCloseDlg)            

076            // NOTE: the ClassWizard will add DDX and DDV calls here        

077        //}}AFX_DATA_MAP            

078    }                

079                    

080    BEGIN_MESSAGE_MAP(COPenCloseDlg, CDialog)                

081        //{{AFX_MSG_MAP(COPenCloseDlg)            

082        ON_WM_SYSCOMMAND()            

083        ON_WM_PAINT()            

084        ON_WM_QUERYDRAGICON()            

085        ON_BN_CLICKED(IDC_BUTTON1, OnButton1)            

086        ON_BN_CLICKED(IDC_BUTTON2, OnButton2)            

087        //}}AFX_MSG_MAP            

088    END_MESSAGE_MAP()                

089                    

090    /////////////////////////////////////////////////////////////////////////////                

091    // COPenCloseDlg message handlers                

092                    

093    BOOL COPenCloseDlg::OnInitDialog()                

094    {                

095        CDialog::OnInitDialog();            

096                    

097        // Add "About..." menu item to system menu.            

098                    

099        // IDM_ABOUTBOX must be in the system command range.            

100        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);            

101        ASSERT(IDM_ABOUTBOX < 0xF000);            

102                    

103        CMenu* pSysMenu = GetSystemMenu(FALSE);            

104        if (pSysMenu != NULL)            

105        {            

106            CString strAboutMenu;        

107            strAboutMenu.LoadString(IDS_ABOUTBOX);        

108            if (!strAboutMenu.IsEmpty())        

109            {        

110                pSysMenu->AppendMenu(MF_SEPARATOR);    

111                pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);    

112            }        

113        }            

114                    

115        // Set the icon for this dialog.  The framework does this automatically            

116        //  when the application's main window is not a dialog            

117        SetIcon(m_hIcon, TRUE);            // Set big icon

118        SetIcon(m_hIcon, FALSE);        // Set small icon    

119                    

120        // TODO: Add extra initialization here            

121        SetWindowPos(&CWnd::wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); //jash_liao write            

122        return TRUE;  // return TRUE  unless you set the focus to a control            

123    }                

124                    

125    void COPenCloseDlg::OnSysCommand(UINT nID, LPARAM lParam)                

126    {                

127        if ((nID & 0xFFF0) == IDM_ABOUTBOX)            

128        {            

129            CAboutDlg dlgAbout;        

130            dlgAbout.DoModal();        

131        }            

132        else            

133        {            

134            CDialog::OnSysCommand(nID, lParam);        

135        }            

136    }                

137                    

138    // If you add a minimize button to your dialog, you will need the code below                

139    //  to draw the icon.  For MFC applications using the document/view model,                

140    //  this is automatically done for you by the framework.                

141                    

142    void COPenCloseDlg::OnPaint()                 

143    {                

144        if (IsIconic())            

145        {            

146            CPaintDC dc(this); // device context for painting        

147                    

148            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);        

149                    

150            // Center icon in client rectangle        

151            int cxIcon = GetSystemMetrics(SM_CXICON);        

152            int cyIcon = GetSystemMetrics(SM_CYICON);        

153            CRect rect;        

154            GetClientRect(&rect);        

155            int x = (rect.Width() - cxIcon + 1) / 2;        

156            int y = (rect.Height() - cyIcon + 1) / 2;        

157                    

158            // Draw the icon        

159            dc.DrawIcon(x, y, m_hIcon);        

160        }            

161        else            

162        {            

163            CDialog::OnPaint();        

164        }            

165    }                

166                    

167    // The system calls this to obtain the cursor to display while the user drags                

168    //  the minimized window.                

169    HCURSOR COPenCloseDlg::OnQueryDragIcon()                

170    {                

171        return (HCURSOR) m_hIcon;            

172    }                

173                    

174    void COPenCloseDlg::OnButton1()                 

175    {                

176        // TODO: Add your control notification handler code here            

177        ShellExecute(NULL,"open","C:\\WINDOWS\\CBLOCKS.EXE",NULL,NULL,SW_SHOWNORMAL);             

178        ShellExecute(NULL,"open","C:\\WINDOWS\\CALC.EXE",NULL,NULL,SW_SHOWNORMAL);            

179        ShellExecute(NULL,"open","C:\\Program Files\\Accessories\\MSPAINT.EXE",NULL,NULL,SW_SHOWNORMAL);            

180        ShellExecute(NULL,"open","C:\\WINDOWS\\NOTEPAD.EXE",NULL,NULL,SW_SHOWNORMAL);            

181    }                

182                    

183    void COPenCloseDlg::OnButton2()                 

184    {                

185        // TODO: Add your control notification handler code here            

186        CWnd* hWnd3 = FindWindow(NULL, "未命名 - 記事本");            

187        hWnd3->SendMessage(WM_CLOSE,0,0);            

188        CWnd* hWnd2 = FindWindow(NULL, "未命名 - 小畫家");            

189        hWnd2->SendMessage(WM_CLOSE,0,0);            

190        CWnd* hWnd1 = FindWindow(NULL, "小算盤");            

191        hWnd1->SendMessage(WM_CLOSE,0,0);            

192        CWnd* hWnd0 = FindWindow(NULL, "中文輸入法練習 - 輸入法");            

193        hWnd0->SendMessage(WM_CLOSE,0,0);            

194                    

195    }                

 



 


沒有留言:

張貼留言