您的位置:首页 > 其它

基于MFC,在非客户区与客户区利用CButon类创建button

2011-09-07 18:21 169 查看
 

 

在VC中创建一个基于MFC的空的工程,取名叫MFCTest,

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

在非客户区创建按钮:


在CMainFrame类中添加一个私有(private)的成员变量,如图:



private:


    CButton m_btn;


在创建框架窗口的函数中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
);

Return Value

Nonzero if successful; otherwise 0.

Parameters

lpszCaption

Specifies the button control's text.

dwStyle

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);


 

效果如下:



 

其中CRect函数是指定按钮窗口的左上角与右下角。

 

如果要在非客户区中显示则可改成:m_btn.Create("按钮",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,CRect(0,0,100,50),GetParent(),123);


即利用GetParent(),获得一个父窗口的指针;

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: