在MFC向导生成的基于Dialog的程序中,程序把主对话框 CTest1Dlg dlg; 定义到BOOL CTest1App::InitInstance()中,见下面函数,那dlg岂不是私有成员,我想把dlg定义为全局,当把CTest1Dlg dlg;拿到函数外部定义时,编译OK,但运行就出错。不知何故?谢谢!// CTest1App initialization
BOOL CTest1App::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
} AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CTest1Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
BOOL CTest1App::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
} AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CTest1Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
在CTest1Dlg dlg; 定义了一个串口 CMSComm m_ctrlComm 成员,一个(CDialog子窗口) Cpage0 page0;
在Cpage0 page0中有一个按键button1,void Cpage1::OnButton1() 函数中需要调用dlg中的串口发送数据,函数如下
void Cpage1::OnButton1()
{
// TODO: Add your control notification handler code here
unsigned char send_data[50];
send_data[0]=0x7f;
send_data[1]=ReceiveID;
send_data[2]=0x02;
send_data[3]=0x02;
send_data[4]=0x4d;
send_data[5]=cal_xor(&send_data[1],4);
send_data[6]=0xef;
// dlg.UartSend(send_data,7); //注该行不知怎么表示 UartSend()属于窗口类 CTest1Dlg的函数,而CTest1Dlg dlg;的dlg却定义在BOOL CTest1App::InitInstance()中,是一个私有变量无法成为全局变量。我该如何写这一句呢?就像文件路径一样,CPage1中的函数如何访问它的上一级dlg中的成员函数。
MessageBox("¶ÁRAM²Ù×÷³É¹¦");
}不胜感激!
CTest1Dlg* pDlg = (CTest1Dlg*)GetParent();
pDlg->UartSend(...)
int nResponse = page0.DoModal();
if (nResponse == IDOK)
{
CString data = page0.send_data;
}
其中send_data设置为public并且在你的在Cpage0 page0中有一个按键button1 对应的函数最后加上
CDialog::OnOk();
参考你们的办法,我用指针的方法解决了,哈哈!CTest1Dlg * hwnd_dlg; //定义一个全局变量
BOOL CTest1App::InitInstance()
{
...
CTest1Dlg dlg;
m_pMainWnd = &dlg;
hwnd_dlg=&dlg; //将指针指向dlg
...
}-------------------------------extern unsigned char BuffUart0[64];
extern CTest1Dlg * hwnd_dlg;
void Cpage1::OnButton1()
{
// TODO: Add your control notification handler code here
unsigned char send_data[50];
send_data[0]=0x7f;
send_data[1]=ReceiveID;
send_data[2]=0x02;
send_data[3]=0x02;
send_data[4]=0x4d;
send_data[5]=cal_xor(&send_data[1],4);
send_data[6]=0xef;
memset(BuffUart0,0,64);
(*hwnd_dlg).UartSend(send_data,7); //用指针就能成功访问UartSend();
Sleep(100);
MessageBox("¶ÁRAM²Ù×÷³É¹¦");
}
最后编译OK,程序调试时发现也成功的调用了UartSend(),哈哈!再次感谢 Mackz wzaen erac glunoy 的关注!