修改类中的控制变量 [英] Modifying Control Variables from within a Class

查看:105
本文介绍了修改类中的控制变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用MFC的真正尝试,所以我为我正在制作的任何基本错误或我对该主题的任何知识缺口道歉。



我构建了一个非常基本的程序,可以获取目录中的内容和所有文件,并将内容复制并粘贴到我选择的不同目录中。我有一个名为m_percent的CString类型成员类向导值变量。该变量应该跟踪到目前为止复制的文件的百分比,并在每个文件传输时显示该数字。



我有一个类,它有一个函数,通过引用接受m_percent,这样可以使用这个函数中对m_percent的更改来更新进度百分比。



我遇到的问题是我无法从我构建的类中调用UpdateData(FALSE)以便我可以立即更新百分比和因此,我无法在每次复制递归时直观地更新我的DLG,这使得我的百分比维持毫无价值,因为我无法直观地更新它直到100%标记之后。



有什么比UpdateData更好的方法来直观地更新m_percent实际上是什么,因为我增加了它?我怎样才能正常完成这项工作?



谢谢



Psuedocode:



This is my first real attempt at using MFC so I apologize for any basic mistakes I''m making or any gaps in knowledge that I may have regarding the subject.

I constructed a pretty basic program that can take the contents and all the files in a directory and copy and paste the contents to a different directory of my choice. I have a CString type member class wizard value variable called m_percent. This variable is supposed to keep track of the percentage of the files copied so far and display the number as each file transfers.

I have a class that has a function which accepts m_percent by reference so that the changes to m_percent within this function can be used to update the progress percentage.

The problem i''m having is I''m unable to call UpdateData(FALSE) from within the class I''ve constructed so that I can immediately update the percentage and as a result am unable to update my DLG visually at each recursion of copying effectively making my percentage upkeep worthless as I can''t update it visually until after the 100% mark.

Is there a better way than UpdateData to visually update what m_percent actually is as I increment it? How can I properly make this work?

Thanks

Psuedocode:

Void dir_backup::make_backup(CString &m_percent, unsigned long max_size)
{
 
for(all files that exist)
{
  Copy a file;

  size_copied = copied_file.size();

  double temp = ((double)size_copied/(double)max_size)*100;

  m_percent.Format("%.2f",temp); //Percent is now modified because I passed it by    reference so now I want to visually change it

  This is where I tried to call CWnd::UpdateData(FALSE) but cant so I need an                alternative
}
}

推荐答案

在同一个线程中的长时间进程中不会更新用户界面。



您需要创建一个工作线程你将在其中进行文件复制;从该主题,你可以通知主线程(你的用户界面)更新百分比值。



另一个建议,如果你只想复制文件,会使用 ShFileOperation [ ^ ](使用选项FO_COPY)并使用自己的进度对话框(FOF_SIMPLEPROGRESS)。
The User interface will not be updated during a long process in the same thread.

You need to create a work thread in which you will do the file copy; and from that thread you can notify the main thread (your user interface) to update the percent value.

One other suggestion, if you only want to copy files, would be to use ShFileOperation[^] (with options FO_COPY )and use its own progress dialog (FOF_SIMPLEPROGRESS).


我认为你有两个问题:



I think you have two problems:



  1. 如何更新进度控件
  2. 确保执行更新(未阻止)





更新此类进度控件通常是通过将用户定义的消息从工作人员代码发布到对话框来执行的:





Updating such progress controls is usually performed by posting user defined messages from the worker code to the dialog:

// Your app header file
#define WM_APP_PERCENT_PROGRESS (WM_APP + 0)

// Your dialog header file
class CMyDialog : CDialog
{
    ...
    LRESULT OnUpdateProgress(WPARAM wParam, LPARAM lParam);
    ...
};


// Your dialog source file
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
    ...
    ON_MESSAGE(WM_APP_PERCENT_PROGRESS, OnUpdateProgress)
END_MESSAGE_MAP()

CMyDialog::OnUpdateProgress(WPARAM wParam, LPARAM lParam)
{
    unsigned long size_copied = static_cast<unsigned long>(wParam);
    unsigned long max_size = static_cast<unsigned long>(lParam);
    m_percent.Format(_T("%.2f %%"),
        ((double)size_copied/(double)max_size)*100.);
    UpdateData(FALSE);
    return 0:
}

// From within your dir_backup class post the update message
// You must know the dialogs CWnd / HWND
::PostMessage(hWndDialog, WM_APP_PERCENT_PROGRESS, (WPARAM)size_copied, (LPARAM)max_size);





解决第二个问题更复杂。如果您的复制代码是在与对话框相同的线程中执行的,则对话框的消息处理将被阻止,直到复制完成。要解决此问题,必须从另一个线程中执行复制。这种工作线程的一个例子是简短答案的代码太多。但是,您可以在CodeProject和Web中找到很多示例。



Solving the second problem is more complicated. If your copy code is executed from within the same thread as your dialog, the message processing of your dialog is blocked until copying has been finished. To solve this, the copying must be executed from within another thread. An example for such a worker thread would be too much code for a short answer. However, you may find lot of examples here at CodeProject and in the web.


这篇关于修改类中的控制变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆