MFC应用程序中的Fork Dialog和控制子窗口问题

如何解决MFC应用程序中的Fork Dialog和控制子窗口问题

我正在用C ++在Visual Studio 2017上编写MFC应用程序。该应用程序是用于与USB设备通信的用户界面。在我的应用程序中,有一个Start按钮。当用户按下Start按钮时,USB设备开始传输数据,即开始发送数据包,直到我告诉它停止为止(我知道每个数据包的最大大小,但是我不知道有多少个数据包包我会得到)。我正在将流数据写入.CSV文件。

出于这个目的,我使用的是环路板(我仍然没有原始板)。 我正在尝试模拟一个真实的场景,所以我编写了一个while循环来发送和读取数据。

在我的应用程序中,我还添加了一个Stop按钮,该按钮应该通过退出while循环来停止数据传输。

由于我无法在主对话框中执行此操作,因此我想创建一个子对话框来执行通信(它将执行while循环,以从循环板上发送和接收程序包),我将对此进行控制我的主对话框中的子窗口。

为此,我根据Barrnet Chou的建议创建了另一个类,如下所示:

header文件:

#pragma once

#include <string>
#include "libusb.h"

class ChildDlg : public CDialog
{
public:

    DECLARE_MESSAGE_MAP()
    afx_msg void OnButton1Clicked();
    afx_msg void OnButton2Clicked();
};

cpp文件:

#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!",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",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,0); //the out endpoint of current device is 1
        if (RetVal != 0 || BytesWritten != sizeof(DataOut)) MessageBox("Write Error","Error:");
    }

    while (libusb_bulk_transfer(DeviceHandle,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按钮时,我向子对话框发送了一条消息,如下所示:

/* 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    

但是我不知道如何声明这些按钮?在主对话框中,我通过将这些按钮添加到Resource view并单击两次(自动创建了适当的处理程序)来创建了这些按钮

是否可以将Resource view添加到子对话框?如果没有,我应该如何创建这些标识符IDC_BUTTON1ICD_BUTTON2

谢谢。

解决方法

如果您想单击按钮并弹出窗口,建议您参考以下示例。

  1. 创建一个新对话框并添加一个新类(例如“ Test”)

  2. 添加以下代码

#include "Test.h"
void CMFCApplication7Dlg::OnBnClickedButton1()
{
  // TODO: Add your control notification handler code here
  Test *pDlg = new Test;
  pDlg->Create(IDD_DIALOG1,this); //IDD_DIALOG1
  pDlg->ShowWindow(SW_SHOW);
  
}

如果control表示您想通过父窗口的控件来操作子窗口中的控件,则可以通过包含子窗口头文件来查看子窗口的所有控件。例如,如果子窗口是Test.h,则将#include Test.h添加到父窗口。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res