根据ID获取CEdit的句柄实例

MyApp.h和MyApp.cpp

#ifndef MY_APP_H_

#define MY_APP_H_



#include <afxwin.h>



class CMyApp:public CWinApp

{

public:

    virtual BOOL InitInstance();

};



#endif



=====================================





#include "stdafx.h"

#include "MyApp.h"



#include "resource.h"

#include "MyCustomCDialog.h"



BOOL CMyApp::InitInstance()

{

    /*

    CDialog dialog;

    dialog.Create(IDD_DIALOG_FIRST,NULL);

    dialog.ShowWindow(m_nCmdShow);

    */



    CMyCustomCDialog myDialog;

    myDialog.DoModal();

    //myDialog.ShowWindow(m_nCmdShow);



    /*myDialog.UpdateData(true);



    std::string result(myDialog.m_cstrEditFirst.GetBuffer());

    myDialog.m_cstrEditFirst.ReleaseBuffer();

    */

    ::MessageBox(NULL,"Message","Title",MB_OK);

    myDialog.DestroyWindow();



    return true;

}



CMyApp myApp;

 

CMyCustomCDialog.h和 CMyCustomCDialog.cpp

#ifndef MY_CUSTOM_CDIALOG_H_

#define MY_CUSTOM_CDIALOG_H_



#include <afxwin.h>

#include "resource.h"

#include <string>



class CMyCustomCDialog:public CDialog

{

public:

    CMyCustomCDialog(CWnd* pParent=NULL);



    enum{ IDD=IDD_DIALOG_FIRST };



    //Overrides

protected:

    virtual void DoDataExchange(CDataExchange* pDX);



    //Implement

protected:

    afx_msg void OnDoAction();



    DECLARE_MESSAGE_MAP()



public:

    CString m_cstrEditFirst;

};



#endif





======================================





#include "stdafx.h"

#include "MyCustomCDialog.h"



CMyCustomCDialog::CMyCustomCDialog(CWnd* pParent)

    :CDialog(CMyCustomCDialog::IDD,pParent)

{



}



void CMyCustomCDialog::DoDataExchange(CDataExchange* pDX)

{

    CDialog::DoDataExchange(pDX);

    ::MessageBox(NULL,"Message_DoDataExchange","Title",MB_OK);



    DDX_Text(pDX,IDC_EDIT_FIRST,m_cstrEditFirst);

}



BEGIN_MESSAGE_MAP(CMyCustomCDialog,CDialog)

    ON_BN_CLICKED(IDC_BUTTON_ACTION,OnDoAction)

END_MESSAGE_MAP()



void CMyCustomCDialog::OnDoAction()

{

    HWND hWnd=::GetDlgItem(this->m_hWnd,IDC_EDIT_FIRST);

    char* pChar=new char[100];

    memset(pChar,'\0',100);

    ::GetWindowText(hWnd,pChar,99);

    m_cstrEditFirst.Format("%s",pChar);

}

Resource很简单:

sample edit box(CEdit)

  Action(CButton)

 

关键代码是:

void CMyCustomCDialog::OnDoAction()
{
  HWND hWnd=::GetDlgItem(this->m_hWnd,IDC_EDIT_FIRST);
  char* pChar=new char[100];
  memset(pChar,'\0',100);
  ::GetWindowText(hWnd,pChar,99);
  m_cstrEditFirst.Format("%s",pChar);
}

你可能感兴趣的:(it)