能给我讲讲DoDataExchange(CDataExchange* pDX)函数的用途和用法啊?

bullcat 2003-08-19 11:20:05
想学学DoDataExchange(CDataExchange* pDX)函数的用途和用法
...全文
104 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
思危 2003-08-19
  • 打赏
  • 举报
回复
用于对话框中控件和数据成员交换数据用的。
xtmzl 2003-08-19
  • 打赏
  • 举报
回复
用于 windows控件对象 与 C++对象 之间的数据交换。
fanfyj 2003-08-19
  • 打赏
  • 举报
回复
看MSDN

void CPenWidthsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPenWidthsDlg)

DDX_Text(pDX, IDC_THIN_PEN_WIDTH, m_nThinWidth);

DDV_MinMaxInt(pDX, m_nThinWidth, 1, 20);

DDX_Text(pDX, IDC_THICK_PEN_WIDTH, m_nThickWidth);

DDV_MinMaxInt(pDX, m_nThickWidth, 1, 20);
//}}AFX_DATA_MAP
}

u2m 2003-08-19
  • 打赏
  • 举报
回复
数据控制用的
这是一个用C言编写的聊天程序,很好使哦 #include "stdafx.h" #include "chat.h" #include "chatDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CChatDlg dialog CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/) : CDialog(CChatDlg::IDD, pParent) { //{{AFX_DATA_INIT(CChatDlg) m_strMessage = _T(""); m_strName = _T(""); m_port =5000; m_strShowMessage = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CChatDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CChatDlg) DDX_Text(pDX, IDC_MESSAGE, m_strMessage); DDX_Text(pDX, IDC_NAME, m_strName); DDX_Text(pDX, IDC_PORT, m_port); DDX_Text(pDX, IDC_SHOWMESSAGE, m_strShowMessage); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CChatDlg, CDialog) //{{AFX_MSG_MAP(CChatDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_CONNECT, OnConnect) ON_BN_CLICKED(IDC_SETSERVER, OnSetserver) ON_BN_CLICKED(IDC_SEND, OnSend) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CChatDlg message handlers BOOL CChatDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here //自行添加的程序代码,目的是在初始化对话框的时候就自动显示出本地的ip地址 if(AfxSocketInit(NULL)==0) { AfxMessageBox("CSocket 初始化失败!"); } BYTE f0,f1,f2,f3; CString name; CClientSocket::GetLocalHostName(name); CClientSocket::GetIpAddress(name,f0,f1,f2,f3); ((CIPAddressCtrl *)(GetDlgItem(IDC_IPADDRESS)))->SetAddress(f0,f1,f2,f3); m_bInit=false; m_bClient=false; return TRUE; // return TRUE unless you set the focus to a control } void CChatDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CChatDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(▭); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CChatDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //连接服务器的消息相应函数
vc++利用ADOX创建数据库 // ADOXCreateDatabaseDlg.cpp : implementation file // #include "stdafx.h" #include "ADOXCreateDatabase.h" #include "ADOXCreateDatabaseDlg.h" #include "Shlwapi.h" #pragma comment(lib,"shlwapi.lib") // Download by http://www.codefans.net #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CADOXCreateDatabaseDlg dialog CADOXCreateDatabaseDlg::CADOXCreateDatabaseDlg(CWnd* pParent /*=NULL*/) : CDialog(CADOXCreateDatabaseDlg::IDD, pParent) { //{{AFX_DATA_INIT(CADOXCreateDatabaseDlg) m_dbName = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CADOXCreateDatabaseDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CADOXCreateDatabaseDlg) DDX_Text(pDX, IDC_DBNAME, m_dbName); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CADOXCreateDatabaseDlg, CDialog) //{{AFX_MSG_MAP(CADOXCreateDatabaseDlg) ON_WM_SYSCOMM
词法分析// TranslationDlg.cpp : 实现文件 // #include "stdafx.h" #include "Translation.h" #include "TranslationDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialog { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CTranslationDlg 对话框 CTranslationDlg::CTranslationDlg(CWnd* pParent /*=NULL*/) : CDialog(CTranslationDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CTranslationDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_EDIT2, content); DDX_Control(pDX, IDC_EDIT1, result); } BEGIN_MESSAGE_MAP(CTranslationDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_BUTTON1, &CTranslationDlg::OnBnClickedButton1) END_MESSAGE_MAP() // CTranslationDlg 消息处理程序 BOOL CTranslationDlg::OnInitDialog() { CDialog::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 CAboutDlg dlgAbout; dlgAbout.DoModal(); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CTranslationDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CTranslationDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CTranslationDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void CTranslationDlg::SplideFrontSpc (CString &str) { int i = 0; for(i;str[i]==' ';) {str.Delete (0);}//MessageBox (_T("ok")); } void CTranslationDlg::OnBnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 CString code ,temp ,output; //CString word[8] = ; CString flag[3][10] = {{_T("if"),_T("int"),_T("for"),_T("while"),_T("do"),_T("return"),_T("break"),_T("continue")}, {_T("+"),_T("-"),_T("*"),_T("/"),_T("="),_T(">"),_T("<"),_T("<="),_T(">="),_T("!=")}, { _T(","),_T(";"),_T("{"),_T("}"),_T("("),_T(")")}}; content.GetWindowTextW (code); code.Replace (_T("\r\n"),_T("")); code.Append (_T(" ")); while(!code.IsEmpty ()) { temp.Empty (); int i,j; int isfind = 0;//是否找到,找到了代表其类型 SplideFrontSpc(code); //temp = code.Left (code.Find (_T(","))); //code.Delete (0,2);//temp = code[0]; //截取下一个单词 temp = code.Left (code.Find (_T(" "))); code.Delete (0,code.Find (_T(" "))); //对比单词类型 for(i=0;!isfind&&i<3;i++) for(j=0;j<10;j++) if(temp == flag[i][j]) { if(i==0) isfind = 1; else if(i==1) isfind = 4; else isfind = 5; break; } if(isfind==0) { int isnum = temp[0]-'0'; if(isnum>-1||isnum<10) isfind = 3; else isfind = 2; } CString cnum; cnum.Format (_T("%d"),isfind); if(!temp.IsEmpty ()) temp = _T("(") + cnum + _T(", \"") + temp + _T("\")"); else temp = _T("词法分析完毕!\r\n"); output = output + temp + _T("\r\n"); } //output = _T("asdjfk\r\naksdjfl\nasldkfj\n"); result.SetWindowTextW (output); } BOOL CTranslationDlg::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加专用代码和/或调用基类 return CDialog::PreTranslateMessage(pMsg); } void CTranslationDlg::OnOK() { // TODO: 在此添加专用代码和/或调用基类 CDialog::OnOK(); }
// CmdShellDlg.cpp : implementation file // #include "stdafx.h" #include "darkshell.h" #include "CmdShellDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CCmdShellDlg dialog CCmdShellDlg::CCmdShellDlg(CWnd* pParent /*=NULL*/) : CDialog(CCmdShellDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCmdShellDlg) m_Cmd = _T("net user"); //}}AFX_DATA_INIT } void CCmdShellDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCmdShellDlg) DDX_Control(pDX, IDC_EDIT_SHELL, m_EdtShell); DDX_Text(pDX, IDC_EDIT_CMD, m_Cmd); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCmdShellDlg, CDialog) //{{AFX_MSG_MAP(CCmdShellDlg) ON_BN_CLICKED(IDC_BTN_RUN, OnBtnRun) ON_BN_CLICKED(IDC_BTN_CLEAN, OnBtnClean) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCmdShellDlg message handlers BOOL CCmdShellDlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the small icon for this dialog SetIcon(AfxGetApp()->LoadIcon(IDI_ICOBTN_SHELL), FALSE); CenterWindow(); // TODO: Add extra initialization here TCHAR szIp[100]; DWORD dwPort = m_hServer.GetPeerName(szIp); CString strTitle; strTitle.Format(_T("超级终端@%s:%d"), szIp, dwPort); SetWindowText(strTitle); //设置回调 m_hServer.SetCallBack(this); //开始CmdShell m_hServer.Begin(); return TRUE; } void CCmdShellDlg::OnCancel() { // TODO: Add extra cleanup here m_hServer.SendCmd(_T("exit\r\n")); m_hServer.Close(); //非模式对话框,需要这样销毁对话框 DestroyWindow(); //CDialog::OnCancel(); } void CCmdShellDlg::PostNcDestroy() { // TODO: Add your specialized code here and/or call the base class //delete掉主对话框中new出来的指针 delete this; CDialog::PostNcDestroy(); } BOOL CCmdShellDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if (pMsg->message == WM_KEYDOWN) { int nVirtKey = (int)pMsg->wParam; if (nVirtKey == VK_RETURN) { //如果是回车在这里做你要做的事情,或者什么也不作 return TRUE; } if (nVirtKey == VK_ESCAPE) { //如果是ESC在这里做你要做的事情,或者什么也不作 return TRUE; } } return CDialog::PreTranslateMessage(pMsg); } //回调函数 void CCmdShellDlg::OnRecvMsg(LPCTSTR szMsg) { m_EdtShell.AddText(szMsg); } void CCmdShellDlg::OnBtnRun() { // TODO: Add your control notification handler code here UpdateData(); m_Cmd +=_T("\r\n"); m_hServer.SendCmd(m_Cmd); } void CCmdShellDlg::OnBtnClean() { // TODO: Add your control notification handler code here m_EdtShell.ClearEdit(); }
数字识别之神经网络法(来自网络) /* ============================================= *\ * File: OnrDlg.cpp * * Description: 主对话框头文件 * * 包括所有的函数 * \* ============================================= */ #include "stdafx.h" #include "Onr.h" #include "OnrDlg.h" #include #include #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) virtual BOOL OnInitDialog(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL CAboutDlg::OnInitDialog() { CDialog::OnInitDialog(); return TRUE; } ///////////////////////////////////////////////////////////////////////////// // COnrDlg dialog COnrDlg::COnrDlg(CWnd* pParent) : CDialog(COnrDlg::IDD, pParent) { //{{AFX_DATA_INIT(COnrDlg) //}}AFX_DATA_INIT m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void COnrDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(COnrDlg) DDX_Control(pDX, IDC_TRAINSPIN, m_Trainspin); DDX_Control(pDX, IDC_TESTSPIN, m_Testspin); DDX_Control(pDX, IDC_TRAININFO, m_TrainInfo); DDX_Control(pDX, IDC_INFOBOX, m_cInfoBox); DDX_Control(pDX, IDC_TESTWINDOW, m_TestWindow); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(COnrDlg, CDialog) //{{AFX_MSG_MAP(COnrDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_TRAIN, OnTrain) ON_BN_CLICKED(IDC_CLASSIFY, OnClassify) ON_NOTIFY(UDN_DELTAPOS, IDC_TRAINSPIN, OnDeltaposTrainspin) ON_NOTIFY(UDN_DELTAPOS, IDC_TESTSPIN, OnDeltaposTestspin) //}}AFX_MSG_MAP END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////// // 初始化对话框 BOOL COnrDlg::OnInitDialog() { CDialog::OnInitDialog(); // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } SetIcon(m_hIcon, TRUE); // Set big icon SetIcon((HICON)(LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, 0)), FALSE); // 设置滚动条的范围 m_Testspin.SetRange(0,NN_NUMBERS-1); m_Trainspin.SetRange(0,NN_NUMBERS-1); m_Testspin.SetPos(0); m_Trainspin.SetPos(0); // 打开文件 ifstream data("learn.dat", ios::nocreate); ifstream test("test.dat", ios::nocreate); // 检查文件的有效性 if (!test || !data) { MessageBox("No learning or test data present.", "Cannot run...", MB_OK | MB_ICONERROR); } // 读文件 for(int i=0;i> onoff; m_bTestData[i][j] = onoff; data >> onoff; m_bNumbers[i][j] = onoff; } } // 设置初始要画的数字指针 m_ipDrawNum = &m_bNumbers[0][0]; return true; } void COnrDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } void COnrDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CPaintDC dc(this); CDialog::OnPaint(); // 这种方法画矩形可能有点迂回,不过的确可以工作 CRect rect; m_TestWindow.GetClientRect(&rect); m_TestWindow.ClientToScreen(&rect); ScreenToClient(&rect); dc.FrameRect(&rect, &CBrush(RGB(0,0,0))); rect.DeflateRect(1,1); dc.FillSolidRect(rect, RGB(255,255,255)); // 画数字 DrawNumber(&dc, m_ipDrawNum, &rect); } } HCURSOR COnrDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } /////////////////////////////////////////////////////////// // 由于某些原因,如果采用"Maxinum Speed"优化将引起程序运行的 // 失败,所以先关掉优化,在该段代码结束后再打开优化功能 /////////////////////////////////////////////////////////// #pragma optimize( "", off ) ////////////////////////////////////////////////////////////////////////// // 训练数据 void COnrDlg::OnTrain() { srand((unsigned)time(NULL)); // 设置随机数 m_cInfoBox.ResetContent(); // 所有权值0初始化 memset(&m_fWeights,0,sizeof(m_fWeights)); // 产生一些带有随机噪声的训练数据 m_cInfoBox.InsertString(-1, "Generating noisy data..."); int num = 0; for (int i=0;iext("Complete..."); m_cInfoBox.InsertString(-1,"Training Complete!"); GetDlgItem(IDC_CLASSIFY)->EnableWindow(true); } /////////////////////////////////////////////////////////////////////////// // 画数字 void COnrDlg::DrawNumber(CDC *dc, int *cell, CRect *rect) { // 下面的计算主要是使得数字画在矩形筐的正中央 CPoint tl; tl.x = rect->Width()/2 - (NN_RESX*10)/2; tl.y = rect->Height()/2 - (NN_RESY*10)/2; tl.Offset(rect->TopLeft()); int ix = tl.x; // 产生一些围绕数字的红色虚线 CPen pen; if (pen.CreatePen(PS_DOT, 1, RGB(127,0,0))) { CPen *pOldPen = dc->SelectObject(&pen); CPoint pt1 = rect->TopLeft(); CPoint pt2 = rect->BottomRight(); dc->MoveTo(tl.x-5, tl.y-2); dc->LineTo(tl.x+NN_RESX*10+5, tl.y-2); dc->MoveTo(tl.x-5, tl.y+10*NN_RESY+1); dc->LineTo(tl.x+NN_RESX*10+5, tl.y+10*NN_RESY+1); dc->MoveTo(tl.x-2, tl.y-7); dc->LineTo(tl.x-2, tl.y+NN_RESY*10+7); dc->MoveTo(tl.x+NN_RESX*10+2, tl.y-7); dc->LineTo(tl.x+NN_RESX*10+2, tl.y+NN_RESY*10+7); dc->SelectObject(pOldPen); } // 显示一些文本 CRect format(tl.x+1, tl.y+NN_RESY*10+2, tl.x+NN_RESX*10-1, tl.y+NN_RESY*10+18); if (m_bDisplayString) { CFont fnt; if (fnt.CreatePointFont(80,"Arial")) { CFont* pOldFont = dc->SelectObject(&fnt); dc->SetTextColor(RGB(0,0,127)); dc->DrawText(m_Str,format,DT_CENTER); dc->SelectObject(pOldFont); } } // 画数字 for(int i=0;iFillSolidRect(tl.x,tl.y,10,10,RGB(0,0,0)); } tl.x += 10; if (tl.x == ix + 10*NN_RESX) { tl.x = ix; tl.y += 10; } cell++; } } ////////////////////////////////////////////////////////////////////////////////// // 识别 void COnrDlg::OnClassify() { int pos = (BYTE)m_Testspin.GetPos(); m_ipDrawNum = &m_bTestData[pos][0]; InvalidateNumber(); float d[NN_NUMBERS]; for(int j=0;j d[bestind]) bestind = j; // 计算画文本的区域 CRect size; m_TestWindow.GetClientRect(&size); int x = size.Width()/2 - (NN_RESX*10)/2; int y = size.Height()/2 - (NN_RESY*10)/2; CRect format(x,size.Height()-y+1, size.Width()-x, size.Height()-5); m_bDisplayString = true; GetText(m_Str, bestind+1); } ///////////////////////////////////////////////////////////////////////////// // 运行网络 // 参数:training-为真时表示网络出于训练状态 void COnrDlg::RunNet(bool training) { // 检测带有噪声的数据 float d[NN_NUMBERS]; int cycles = 0;//初始化循环次数为0 bool correct; do { correct = true; for(int i=0;i d[bestind]) bestind = j; int realval = (int)(i/NN_NOISY); if (bestind == realval) continue; if (training) { CString result; result.Format("Guessed %d instead of %d.",bestind, realval); m_TrainInfo.SetWindowText(result); correct = false; for(j=0;j= NN_MAXITER) { MessageBox("Training has timed-out.", "Error in Training", MB_OK | MB_ICONINFORMATION); return; } } //恢复系统优化 #pragma optimize( "", on ) //////////////////////////////////////////////////////////////////////// // 处理训练数据的滚动条 void COnrDlg::OnDeltaposTrainspin(NMHDR* pNMHDR, LRESULT* pResult) { NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR; int iPos = pNMUpDown->iPos; int iDelta = pNMUpDown->iDelta; m_bDisplayString = false; if ((iPos == 0 && iDelta == -1) || (iPos == NN_NUMBERS-1 && iDelta == 1)) { if (iDelta == -1) { m_ipDrawNum = &m_bNumbers[0][0]; InvalidateNumber(); } else { m_ipDrawNum = &m_bNumbers[NN_NUMBERS-1][0]; InvalidateNumber(); } } else { m_ipDrawNum = &m_bNumbers[iPos+iDelta][0]; InvalidateNumber(); } *pResult = 0; } ////////////////////////////////////////////////////////////////////////////////// // 处理测试数据的滚动条 void COnrDlg::OnDeltaposTestspin(NMHDR* pNMHDR, LRESULT* pResult) { NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR; int iPos = pNMUpDown->iPos; int iDelta = pNMUpDown->iDelta; m_bDisplayString = false; if ((iPos == 0 && iDelta == -1) || (iPos == NN_NUMBERS-1 && iDelta == 1)) { if (iDelta == -1) { m_ipDrawNum = &m_bTestData[0][0]; InvalidateNumber(); } else { m_ipDrawNum = &m_bTestData[NN_NUMBERS-1][0]; InvalidateNumber(); } } else { m_ipDrawNum = &m_bTestData[iPos+iDelta][0]; InvalidateNumber(); } *pResult = 0; } ////////////////////////////////////////////////////////////////////////// // 得到数字相对应的文本 void COnrDlg::GetText(CString &str, int num) { switch (num) { case 1: str = "Result:1"; break; case 2: str = "Result:2"; break; case 3: str = "Result:3"; break; case 4: str = "Result:4"; break; case 5: str = "Result:5"; break; case 6: str = "Result:6"; break; case 7: str = "Result:7"; break; case 8: str = "Result:8"; break; case 9: str = "Result:9"; break; default: str = "Undetermined"; } } /////////////////////////////////////////////////////////////////////////////// // 强制重画 void COnrDlg::InvalidateNumber() { CRect rect; m_TestWindow.GetClientRect(&rect); m_TestWindow.ClientToScreen(&rect); ScreenToClient(&rect); InvalidateRect(rect, false); }

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧