欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vs2019 MFC 如何在框架类中实现添加一个按钮button

程序员文章站 2022-05-23 12:13:54
...

首先, 在框架类CMainFrame中添加一个CButton m_btn的成员

vs2019 MFC 如何在框架类中实现添加一个按钮button

然后,在框架类CMainFrame中OnCreate 函数最后添加创建button的函数并显示button,

(注意:在创建button函数create中如果使用了WS_VISIBLE,那么ShowWindow可以不用)

vs2019 MFC 如何在框架类中实现添加一个按钮button

最后运行,可以看到显示添加的button在框架显示栏范围内(根据需要调整button大小位置让其显示在合理位置即可)

vs2019 MFC 如何在框架类中实现添加一个按钮button

重点:关于CButton类提供的成员函数Create 和CButton的父类CWnd提供的ShowWindow说明如下:

Create

Creates the Windows button control and attaches it to the CButton object.

 

 

CButton::Create
Example  See Also  Send Feedback
 

 

Creates the Windows button control and attaches it to the CButton object.

 
virtual BOOL Create(
   LPCTSTR lpszCaption,
   DWORD dwStyle,
   const RECT& rect,
   CWnd* pParentWnd,
   UINT nID 
);

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.

Return Value

Nonzero if successful; otherwise 0.

Remarks

You construct a CButton object in two steps. First, call the constructor and then call Create, which creates the Windows button control and attaches it to the CButton object.

If the WS_VISIBLE style is given, Windows sends the button control all the messages required to activate and show the button.

Apply the following window styles to a button control:

  • WS_CHILD   Always

  • WS_VISIBLE   Usually

  • WS_DISABLED   Rarely

  • WS_GROUP   To group controls

  • WS_TABSTOP   To include the button in the tabbing order

Example

Visual C++  Copy Code
CButton myButton1, myButton2, myButton3, myButton4;

// Create a push button.
myButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 
   CRect(10,10,100,30), pParentWnd, 1);

// Create a radio button.
myButton2.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON, 
   CRect(10,40,100,70), pParentWnd, 2);

// Create an auto 3-state button.
myButton3.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTO3STATE, 
   CRect(10,70,100,100), pParentWnd, 3);

// Create an auto check box.
myButton4.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX, 
   CRect(10,100,100,130), pParentWnd, 4);
MFC Library Reference
CWnd::ShowWindow
Example  See Also  Send Feedback
 

 

Sets the visibility state of the window.

 
BOOL ShowWindow(
   int nCmdShow 
);

Parameters

nCmdShow

Specifies how the CWnd is to be shown. It must be one of the following values:

  • SW_HIDE   Hides this window and passes activation to another window.

  • SW_MINIMIZE   Minimizes the window and activates the top-level window in the system's list.

  • SW_RESTORE   Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.

  • SW_SHOW   Activates the window and displays it in its current size and position.

  • SW_SHOWMAXIMIZED   Activates the window and displays it as a maximized window.

  • SW_SHOWMINIMIZED   Activates the window and displays it as an icon.

  • SW_SHOWMINNOACTIVE   Displays the window as an icon. The window that is currently active remains active.

  • SW_SHOWNA   Displays the window in its current state. The window that is currently active remains active.

  • SW_SHOWNOACTIVATE   Displays the window in its most recent size and position. The window that is currently active remains active.

  • SW_SHOWNORMAL   Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.

Return Value

Nonzero if the window was previously visible; 0 if the CWnd was previously hidden.

Remarks

ShowWindow must be called only once per application for the main window with CWinApp::m_nCmdShow. Subsequent calls to ShowWindow must use one of the values listed above instead of the one specified by CWinApp::m_nCmdShow.

Example

Visual C++  Copy Code
// Uses CalcWindowRect to determine size for new CFrameWnd
// based on the size of the current view. The end result is a
// top level frame window of the same size as CMdiView's frame.
void CMdiView::OnMyCreateFrame() 
{
   CFrameWnd* pFrameWnd = new CFrameWnd;
   CRect myRect;
   GetClientRect(myRect);
   pFrameWnd->Create(NULL, _T("My Frame"));
   pFrameWnd->CalcWindowRect(&myRect, CWnd::adjustBorder);
   pFrameWnd->MoveWindow(0, 0, myRect.Width(), myRect.Height());
   pFrameWnd->ShowWindow(SW_SHOW);
}
相关标签: MFC visual studio