MFC应用程序中的Fork对话框和控件子窗口问题 我在C++中在Visual Studio 2017上编写了一个MFC应用程序。该应用程序是与USB设备通信的用户界面。在我的应用程序中有Start按钮。当用户按下Start按钮时,USB设备开始传输数据,也就是说,它开始发送数据包,直到我告诉它停止(我知道每个数据包的最大大小,但不知道我将收到多少数据包)。我正在将流数据写入.CSV文件
为此,我使用了一个循环板(我仍然没有原始板)。 我试图模拟一个真实的场景,所以我编写了一个while循环来发送和读取数据 在我的应用程序中,我还添加了一个MFC应用程序中的Fork对话框和控件子窗口问题 我在C++中在Visual Studio 2017上编写了一个MFC应用程序。该应用程序是与USB设备通信的用户界面。在我的应用程序中有Start按钮。当用户按下Start按钮时,USB设备开始传输数据,也就是说,它开始发送数据包,直到我告诉它停止(我知道每个数据包的最大大小,但不知道我将收到多少数据包)。我正在将流数据写入.CSV文件,c++,visual-studio,mfc,libusb,C++,Visual Studio,Mfc,Libusb,为此,我使用了一个循环板(我仍然没有原始板)。 我试图模拟一个真实的场景,所以我编写了一个while循环来发送和读取数据 在我的应用程序中,我还添加了一个Stop按钮,该按钮应该通过退出while循环来停止数据传输 由于我无法从主对话框执行此操作,因此我希望创建一个子对话框来执行通信(它将执行while循环,从loopboard发送和接收包),并从主对话框控制此子窗口 根据Barrnet Chou的建议,我创建了另一个类,如下所示: /* Start Handler */ void CEdita
Stop
按钮,该按钮应该通过退出while循环来停止数据传输
由于我无法从主对话框执行此操作,因此我希望创建一个子对话框来执行通信(它将执行while循环,从loopboard发送和接收包),并从主对话框控制此子窗口
根据Barrnet Chou的建议,我创建了另一个类,如下所示:
/* Start Handler */
void CEditableListControlDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
dlg->SendDlgItemMessageA(IDC_BUTTON1, BM_CLICK);
return;
}
/* Stop Handler */
void CEditableListControlDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
dlg->SendDlgItemMessageA(IDC_BUTTON2, BM_CLICK);
}
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON1': undeclared identifier
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON2': undeclared identifier
标题
文件:
#pragma once
#include <string>
#include "libusb.h"
class ChildDlg : public CDialog
{
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnButton1Clicked();
afx_msg void OnButton2Clicked();
};
#pragma once
#include "stdafx.h"
#include "ChildDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// constants definition for packet transfers
constexpr auto VID = 0x04B4;
constexpr auto PID = 0x00F0;
constexpr auto OUT_ENDPOINT_ID = 1;
constexpr auto IN_ENDPOINT_ID = 1;
constexpr auto MAX_PACKET_SIZE = 512;
constexpr int INIT_PACKET_SIZE = 4;
bool flag;
struct libusb_device_descriptor DeviceDescriptor;
libusb_context* context = NULL; //a libusb session
libusb_device_handle* DeviceHandle = NULL; //a device handle
BEGIN_MESSAGE_MAP(ChildDlg, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &ChildDlg::OnButton1Clicked)
ON_BN_CLICKED(IDC_BUTTON2, &ChildDlg::OnButton2Clicked)
END_MESSAGE_MAP()
void startCommunication() {
/* Prepare device for communication */
int RetVal = libusb_init(&context); //initialize a library session
if (RetVal < 0) {
// MessageBox(hwnd, "Error in libusb_init", "Error:", MB_OK);
return;
}
else {
libusb_set_debug(context, 3); //set verbosity level to 3, as suggested in the documentation
DeviceHandle = libusb_open_device_with_vid_pid(context, VID, PID); //these are vendorID and productID for Cypress FX3 specific device
}
if (DeviceHandle == NULL) {
// MessageBox(hwnd, "Cannot open device", "Notice:", MB_OK);
return;
}
if (libusb_kernel_driver_active(DeviceHandle, 0) == 1) { //find out if kernel driver is attached
//detach it
if (libusb_detach_kernel_driver(DeviceHandle, 0) != 0) {
// MessageBox(hwnd, "Failed To Detach Kernel Driver!", "Error:", MB_OK);
return;
}
}
RetVal = libusb_claim_interface(DeviceHandle, 0); //claim interface 0 (the first) of device (desired device FX3 has only 1)
if (RetVal < 0) {
// MessageBox(hwnd, "Cannot Claim Interface", "Error:", MB_OK);
return;
}
}
void endCommunication() {
libusb_close(DeviceHandle); //close the device we opened
libusb_exit(context); //needs to be called at the end
}
void ChildDlg::OnButton1Clicked() {
// Insert Start button handler here
flag = true;
startCommunication();
unsigned char* DataOut = new unsigned char[INIT_PACKET_SIZE]; //data to write
unsigned char* DataIn = new unsigned char[MAX_PACKET_SIZE]; //data to read
int BytesWritten; //used to find out how many bytes were written
int BytesRead; //used to find out how many bytes were read
DataOut[0] = 'a'; DataOut[1] = 'b'; DataOut[2] = 'c'; DataOut[3] = 'd'; //some dummy values
int RetVal = libusb_bulk_transfer(DeviceHandle, (OUT_ENDPOINT_ID | LIBUSB_ENDPOINT_OUT), DataOut, sizeof(DataOut), &BytesWritten, 0);
if (RetVal == 0 && BytesWritten == sizeof(DataOut)) //we wrote the 4 bytes successfully
MessageBox("Writing Successful!", "Notice:");
else
MessageBox("Write Error", "Notice:");
int counter = 0;
while ((libusb_bulk_transfer(DeviceHandle, (IN_ENDPOINT_ID | LIBUSB_ENDPOINT_IN), DataIn, sizeof(DataIn), &BytesRead, 200) == 0) && flag == true) {
std::string cleanData = std::to_string(DataIn[0]) + std::to_string(DataIn[1]) + std::to_string(DataIn[2]) + std::to_string(DataIn[3]);
MessageBox(cleanData.c_str(), "DATA IN ");
int RetVal = libusb_bulk_transfer(DeviceHandle, (OUT_ENDPOINT_ID | LIBUSB_ENDPOINT_OUT), DataOut, sizeof(DataOut), &BytesWritten, 0); //the out endpoint of current device is 1
if (RetVal != 0 || BytesWritten != sizeof(DataOut)) MessageBox("Write Error", "Error:");
}
while (libusb_bulk_transfer(DeviceHandle, (IN_ENDPOINT_ID | LIBUSB_ENDPOINT_IN), DataIn, sizeof(DataIn), &BytesRead, 200)) {
MessageBox("Garbage", "Notice:");
}
delete[] DataOut; //delete the allocated memory for data
delete[] DataIn; //delete the allocated memory for data
endCommunication();
}
void ChildDlg::OnButton2Clicked() {
flag == false;
}
当按下主对话框中的Start
按钮时,我会向子对话框发送一条消息,如下所示:
/* Start Handler */
void CEditableListControlDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
dlg->SendDlgItemMessageA(IDC_BUTTON1, BM_CLICK);
return;
}
/* Stop Handler */
void CEditableListControlDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
dlg->SendDlgItemMessageA(IDC_BUTTON2, BM_CLICK);
}
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON1': undeclared identifier
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON2': undeclared identifier
当按下主对话框中的停止
按钮时,我会向子对话框发送一条消息,如下所示:
/* Start Handler */
void CEditableListControlDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
dlg->SendDlgItemMessageA(IDC_BUTTON1, BM_CLICK);
return;
}
/* Stop Handler */
void CEditableListControlDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
dlg->SendDlgItemMessageA(IDC_BUTTON2, BM_CLICK);
}
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON1': undeclared identifier
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON2': undeclared identifier
我有两个问题,第一个是我不知道如何将消息从子对话框发送回主对话框
第二个问题是,我得到一个编译错误,如下所示:
/* Start Handler */
void CEditableListControlDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
dlg->SendDlgItemMessageA(IDC_BUTTON1, BM_CLICK);
return;
}
/* Stop Handler */
void CEditableListControlDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
dlg->SendDlgItemMessageA(IDC_BUTTON2, BM_CLICK);
}
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON1': undeclared identifier
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON2': undeclared identifier
但我不知道如何申报这些按钮?在我的主对话框中,我创建了这些按钮,方法是将它们添加到资源视图中
并单击它们两次(这会自动创建适当的处理程序)
是否有方法将资源视图添加到子对话框中?如果没有,我该如何创建这些标识符IDC_按钮1
和ICD_按钮2
谢谢。如果您想单击按钮并弹出窗口,我建议您参考以下示例
创建一个新对话框并添加一个新类(如“Test”)
添加以下代码
如果control
表示要通过父窗口的控件操作子窗口中的控件,则可以通过包含子窗口头文件来查看子窗口的所有控件。例如,如果子窗口是Test.h
,则将#include Test.h
添加到父窗口。“没有弹出窗口”-子窗口和弹出窗口实际上是互斥的。如果您希望您的孩子窗口“弹出”,您可能需要阅读。1。我不确定您是否在父窗口中添加了消息映射。如果没有,您可以添加它,例如在_MESSGAE上。2.您可以检查Button1和Button2
的ID是否已被修改,或者是否已添加相应的头文件。@谢谢您的评论。我检查了一下,不幸的是我仍然找不到问题,消息映射在标题中声明,并在cpp
文件中实现,我在主对话框中包含了ChildDlg.h
,我认为您可以在需要发送消息的地方使用。此解决方案不完整,因为pDlg
永远不会被删除pDlg
t我应该是CMFCApplication7Dlg
的成员。但无论如何,我不确定你是否回答了这里的问题。@BarrnetChou非常感谢你的评论,我已经编辑了我的问题,我想现在我的意图更清楚了。我建议你可以使用SendMessage()
发送消息。@BarrnetChou比你更感谢你的帮助。我正试图实施你的建议,但做起来有困难。你能看看我编辑的帖子吗?也许你会对我如何解决这些新问题有一个新的想法?再次非常感谢