1.编译条件:
在Project options-->Directories/Conditionals-->Conditionals:Conditional dfines中添加_X86_;_WINDOWS;_MSC_VER=1100;_AFX_PORTABLE;_AFX_NOFORCE_LIBS;_MT;_CRT_PORTABLE;
_AFX_NO_DEBUG_CRT
2.包含路径:
include path 加入 $(BCB)\include\MFC
Library path 加入 $(BCB)\Lib\PSDK
3.Advanced Compiler 里将 MFC Compatibility 勾选起来
4.CPP文件中包含相关的lib文件
#ifdef _DEBUG
# pragma link "nafxcwd.lib"
#else
# pragma link "nafxcw.lib"
#endif
5.CPP文件中包含头文件,在vcl.h之前
#include <afxwin.h>
#include <Afxdlgs.h>
在编译的过程中,有两个常量定义冲突,可注释掉。
//static const Word CN_COMMAND = 0xbd11;
//static const Shortint ID_HELP = 0x9;
如果还有其他冲突可在CPP开始处加入#define NO_WIN32_LEAN_AND_MEAN
在c++builder窗体上画线,调用MFC中的CClientDC类,在窗体上画图,代码如下:
在Project options-->Directories/Conditionals-->Conditionals:Conditional dfines中添加_X86_;_WINDOWS;_MSC_VER=1100;_AFX_PORTABLE;_AFX_NOFORCE_LIBS;_MT;_CRT_PORTABLE;
_AFX_NO_DEBUG_CRT
2.包含路径:
include path 加入 $(BCB)\include\MFC
Library path 加入 $(BCB)\Lib\PSDK
3.Advanced Compiler 里将 MFC Compatibility 勾选起来
4.CPP文件中包含相关的lib文件
#ifdef _DEBUG
# pragma link "nafxcwd.lib"
#else
# pragma link "nafxcw.lib"
#endif
5.CPP文件中包含头文件,在vcl.h之前
#include <afxwin.h>
#include <Afxdlgs.h>
在编译的过程中,有两个常量定义冲突,可注释掉。
//static const Word CN_COMMAND = 0xbd11;
//static const Shortint ID_HELP = 0x9;
如果还有其他冲突可在CPP开始处加入#define NO_WIN32_LEAN_AND_MEAN
void __fastcall TForm1::FormPaint(TObject *Sender)
{
//HWND Handle转CWnd;
CWnd *pCWnd1=CWnd::FromHandle(this->Handle);
//CPaintDC dc(pCWnd1);//这个类要在WM_PAINT消息中调用
CClientDC dc1(pCWnd1);
dc1.DrawText("大家好",CRect(10,10,200,100),DT_LEFT);
CBrush brush(RGB(120,21,90));
CBrush *pOldBrush = dc1.SelectObject(&brush);
RECT rect;
rect.bottom =100;
rect.left = 200;
rect.right =400;
rect.top =10;
dc1.Ellipse(&rect);
}
MFC中的类基本都能在C++builder6中调用,控件除外,因为MFC控件的消息映射方式和C++builder中的不同,需要重写子类过程。以下代码演示VC的控件消息处理:
或者重载TForm的虚函数WndProc,处理VC控件的消息响应,如下:
WNDPROC lpfnOldProc = 0;
LRESULT CALLBACK SCWndProc(HWND hWnd, UINT wMsg, WPARAM wParam,LPARAM lParam)
{ //CComboBox控件的子类过程函数
/*/
LOWORD(wParam) 子窗口ID
HIWORD(wParam) 通知码
lParam 子窗口句柄
*/
switch (wMsg)
{
case WM_COMMAND://控件操作消息
{
if(HIWORD(wParam) == CBN_SELCHANGE)
{
//由控件的资源ID转换成控件句柄
HWND cbHwnd = ::GetDlgItem(Form1->Handle,2);
//由句柄转换成对象
CComboBox * pCB = (CComboBox*)CWnd::FromHandle(cbHwnd);
//获取选中的索引
int iPos = pCB->GetCurSel();
CString szText;
//根据索引获根选择的内容
pCB->GetLBText(iPos,szText);
//显示选中的内容
Form1->Label1->Caption = szText.GetBuffer(szText.GetLength());
}
break;
}
}
return CallWindowProc (lpfnOldProc, hWnd, wMsg, wParam, lParam);
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0); //控制台程序中初始化MFC
CreateCommBoBox();
}
void TForm1::CreateCommBoBox()
{
m_cb1 = new CComboBox;
RECT rect;
rect.left = 200;
rect.top = 200;
rect.right = 400;
rect.bottom = 330;
CWnd *pCWnd1 = CWnd::FromHandle(this->Handle);
m_cb1->Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWN|CBS_AUTOHSCROLL,rect,pCWnd1,2);
//插入内容
m_cb1->InitStorage(2,20);
m_cb1->AddString("中国人");
m_cb1->AddString("美国人");
//改写控件子类过程处理函数
HWND cbHwnd = ::GetDlgItem(this->Handle,2);
lpfnOldProc = (WNDPROC)SetWindowLong(cbHwnd, GWL_WNDPROC, (DWORD)SCWndProc);
}
void __fastcall TForm1::Button4Click(TObject *Sender)
{
//获取CComboBox当前文本内容
HWND cbHwnd = ::GetDlgItem(Form1->Handle,2);
CComboBox * pCB = (CComboBox*)CWnd::FromHandle(cbHwnd);
CString szText;
pCB->GetWindowTextA(szText);
char * pText = szText.GetBuffer(szText.GetLength());
ShowMessage(pText);
}
或者重载TForm的虚函数WndProc,处理VC控件的消息响应,如下:
void __fastcall TForm1::WndProc(TMessage &msg)
{
int id = LOWORD(msg.WParam);//获取控件ID
if(id == 0x0100)//资源ID
{
if(msg.Msg == WM_COMMAND)
{
ShowMessage("确定");
}
OutputDebugStringA( AnsiString(msg.Msg).c_str() );
}
TForm::WndProc(msg);
}
CButton btn;//声明MFC按钮类对象
void __fastcall TForm1::Button2Click(TObject *Sender)
{
CRect rect(100,100,170,123);
CWnd *pCWnd1 = CWnd::FromHandle(this->Handle);
btn.Create("确定",WS_CHILD|WS_VISIBLE,rect,pCWnd1,0x100);//最后一个参数为控件ID
}
如果要修改控件字体,如按钮表面的文字字体,可如下处理:
CButton* pBtn = (CButton*)pCWnd1->GetDlgItem(0x100);
CFont *pFont = new CFont; //创建一个指向新字体类型的指针pFont
LOGFONT LogFont;
lstrcpy((LPSTR)LogFont.lfFaceName,(LPSTR)"宋体");
LogFont.lfWidth=5;
LogFont.lfWeight = FW_NORMAL;
LogFont.lfHeight=14;
LogFont.lfEscapement=0;
LogFont.lfUnderline=FALSE;
LogFont.lfItalic=FALSE;
LogFont.lfStrikeOut=FALSE;
LogFont.lfCharSet=GB2312_CHARSET; //以上参数好像一个都不能少
pFont->CreateFontIndirect(&LogFont);
//pFont->CreatePointFont(96,"宋体",NULL);
pBtn->SetFont(pFont);