Non-class member functions in MFC call class member function methods (describe the problem and solution in detail, and the personal test is feasible)

Describe the problems and solutions in plain language in detail, which is suitable for novices like me, and the personal test is effective.

 

The workaround comes from:

https://blog.csdn.net/jiayanhui2877/article/details/14128111

 

Problem Description:

The functions automatically generated in MFC are all class member functions. For example, the name of my mfc project is 1553B_PCI_Software, and the function corresponding to the list control named m_table looks like this:

void CMy1553B_PCI_SoftwareDlg::Onstep() 

Take the "CMy1553B_PCI_SoftwareDlg" part (depending on the name of the project you have created), hereinafter referred to as CxxxDlg.

At this point I created an ordinary function myself:

void task()

If you want to operate on the list control here, what should you do?

 

Solution:

1. First define a global pointer variable CxxxDlg *MyDlg;

2. Write this sentence in the initialization function BOOL CxxxDlg::OnInitDialog() of this MFC project:

MyDlg=this;

Note that there is such a comment in this initialization function:

// TODO: Add extra initialization here

So like MyDlg= this; the statement we added later is best added after this comment.

3. It is enough to operate the list control in the ordinary function task(), for example, fill in the string "abc" in the first row and first column of the list:

MyDlg->m_table.InsertItem(0,0);
MyDlg->m_table.SetItemText(0,0,"abc");

Guess you like

Origin blog.csdn.net/u012824853/article/details/85019302