在VC中创建一个基于MFC的空的工程,取名叫MFCTest,
==================================================================
在非客户区创建按钮:
在CMainFrame类中添加一个私有(private)的成员变量,如图:
在创建框架窗口的函数中CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)函数中添加:
m_btn.Create("按钮",WS_CHILD|BS_DEFPUSHBUTTON,CRect(0,0,100,50),this,123); //创建按钮
m_btn.ShowWindow(SW_SHOWNORMAL); //显示按钮
创建,button按钮;利用CBtton::Create(……)函数的用法,下面是详细说明:
BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
Nonzero if successful; otherwise 0.
Specifies the button control's text.
Specifies the button control's style. Apply any combination of button styles to the button.
rect
Specifies the button control's size and position. It can be either a CRect object or a RECT structure.
pParentWnd
Specifies the button control's parent window, usually a CDialog. It must not be NULL.
nID
Specifies the button control's ID.
================================================================================================
非客户区创建按钮:
同样,在CMFCTestView类中添加一个私有(private)的成员变量,
private:
CButton m_btn;
右键点击类CMFCTestView -> Add Windows Message Handle… -> WM_CREATE ->Add And Edit
在// TODO: Add your specialized creation code here 下添加:
m_btn.Create("按钮",WS_CHILD|BS_DEFPUSHBUTTON,CRect(0,0,100,50),this,123);
m_btn.ShowWindow(SW_SHOWNORMAL);
……………………^_^ OK !
如果想在创建完这个button就让它立即显示出来,可以在Create函数中增加一窗口类型WS_VISIBLE,不要这句m_btn.ShowWindow(SW_SHOWNORMAL);
m_btn.Create("按钮",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,CRect(0,0,100,50),this,123);
效果如下: