// CommunicationTest1Dlg.cpp: 实现文件
//
#include "pch.h"
#include "framework.h"
#include "CommunicationTest1.h"
#include "CommunicationTest1Dlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CCommunicationTest1Dlg 对话框
CCommunicationTest1Dlg::CCommunicationTest1Dlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_COMMUNICATIONTEST1_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCommunicationTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CCommunicationTest1Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_OPEN, &CCommunicationTest1Dlg::OnBnClickedBtnOpen)
ON_BN_CLICKED(IDC_BTN_CLOSE, &CCommunicationTest1Dlg::OnBnClickedBtnClose)
END_MESSAGE_MAP()
// CCommunicationTest1Dlg 消息处理程序
BOOL CCommunicationTest1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CCommunicationTest1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CCommunicationTest1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCommunicationTest1Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCommunicationTest1Dlg::OnBnClickedBtnOpen()
{
// 打开外部程序
HINSTANCE handle = ShellExecute(NULL, _T("open"), _T("CommunicationTest2.exe"), NULL, NULL, SW_SHOW);
if (int(handle) < 32)
{
MessageBox(L"执行应用程序失败!");
return;
}
else
{
// 注册窗口类
HWND hwnd = NULL;
WNDCLASS m_wndClassVisicosity; //窗口类
m_wndClassVisicosity.lpszClassName = _T("CommunicationTest2");
::RegisterClass(&m_wndClassVisicosity);
// 通过窗体名称,获取其他进程窗口句柄
Sleep(500);
hwnd = ::FindWindow(_T("#32770"), _T("CommunicationTest2")); //(LPCTSTR)"#32770",代表对话框窗口类
if (hwnd == NULL)
{
MessageBox(L"程序未运行!");
return;
}
// 发送开始检测消息
::SendMessage(hwnd, WM_OPEN_VISICOSITY, NULL, 0);
}
}
void CCommunicationTest1Dlg::OnBnClickedBtnClose()
{
// 通过窗体名称,获取其他进程窗口句柄
HWND hwnd = ::FindWindow(_T("#32770"), _T("CommunicationTest2")); //(LPCTSTR)"#32770",代表对话框窗口类
if (hwnd == NULL)
{
MessageBox(L"程序未运行!");
return;
}
// 发送结束检测消息
::SendMessage(hwnd, WM_CLOSE_VISICOSITY, NULL, 0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
MFC进程间通信,通过一个进程安全打开和关闭另一个进程
共85个文件
tlog:18个
h:12个
cpp:6个
需积分: 9 1 下载量 30 浏览量
2023-03-23
09:47:11
上传
评论 1
收藏 124.11MB RAR 举报
温馨提示
本实例展示了如何利用windows的消息通信机制,安全打开和关闭另一个进程,用vs2019编译通过,实测可用。
资源推荐
资源详情
资源评论
收起资源包目录
Communication.rar (85个子文件)
Communication
CommunicationTest1
CommunicationTest1Dlg.cpp 5KB
CommunicationTest1.aps 90KB
resource.h 789B
CommunicationTest1.rc 11KB
CommunicationTest1.h 550B
framework.h 2KB
CommunicationTest1.sln 1KB
res
CommunicationTest1.ico 66KB
CommunicationTest1.rc2 692B
.vs
CommunicationTest1
v16
Browse.VC.db 74.04MB
.suo 55KB
ipch
2e2100d0b273e5e2.ipch 120MB
b606b462ffa270b8.ipch 120MB
pch.h 629B
CommunicationTest1.vcxproj.user 244B
CommunicationTest1.cpp 3KB
CommunicationTest1.vcxproj.filters 2KB
pch.cpp 158B
CommunicationTest1Dlg.h 775B
CommunicationTest1.vcxproj 10KB
Debug
CommunicationTest1.log 142B
CommunicationTest1.res 68KB
Communic.6F70FEDC.tlog
CL.write.1.tlog 2KB
CommunicationTest1.lastbuildstate 215B
rc.command.1.tlog 998B
rc.write.1.tlog 302B
rc.read.1.tlog 4KB
CL.command.1.tlog 5KB
link.command.1.tlog 4KB
link.read.1.tlog 7KB
link.write.1.tlog 1KB
CL.read.1.tlog 107KB
vc142.idb 1.34MB
vc142.pdb 6.62MB
CommunicationTest1.ilk 1.96MB
CommunicationTest1.pch 54.94MB
pch.obj 985KB
CommunicationTest1Dlg.obj 59KB
CommunicationTest1.obj 73KB
CommunicationTest1.exe 217KB
CommunicationTest1.pdb 3.69MB
CommunicationTest2.exe 217KB
CommunicationTest2.exe 217KB
targetver.h 299B
CommunicationTest2
CommunicationTest2.vcxproj.user 244B
CommunicationTest2Dlg.h 859B
CommunicationTest2.sln 1KB
resource.h 651B
framework.h 2KB
CommunicationTest2Dlg.cpp 4KB
CommunicationTest2.aps 107KB
res
CommunicationTest2.rc2 692B
CommunicationTest2.ico 66KB
.vs
CommunicationTest2
v16
Browse.VC.db 74.13MB
.suo 47KB
ipch
7ee65dae52205399.ipch 120MB
CommunicationTest2.vcxproj 10KB
pch.h 629B
CommunicationTest2.rc 11KB
CommunicationTest2.vcxproj.filters 2KB
CommunicationTest2.cpp 3KB
CommunicationTest2.h 550B
pch.cpp 158B
Debug
vc142.idb 1.32MB
vc142.pdb 6.62MB
Communic.A01179DE.tlog
CL.write.1.tlog 2KB
rc.command.1.tlog 504B
rc.write.1.tlog 302B
rc.read.1.tlog 4KB
CL.command.1.tlog 3KB
CommunicationTest2.lastbuildstate 215B
link.command.1.tlog 2KB
link.read.1.tlog 7KB
link.write.1.tlog 1KB
CL.read.1.tlog 107KB
CommunicationTest2.obj 73KB
CommunicationTest2Dlg.obj 57KB
pch.obj 985KB
CommunicationTest2.res 68KB
CommunicationTest2.pdb 3.64MB
CommunicationTest2.log 142B
CommunicationTest2.ilk 1.67MB
CommunicationTest2.exe 217KB
CommunicationTest2.pch 54.94MB
targetver.h 299B
共 85 条
- 1
资源评论
toandylee
- 粉丝: 23
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功