vs2019 MFC 如何在框架類中實現添加一個按鈕button

首先, 在框架類CMainFrame中添加一個CButton m_btn的成員

然後,在框架類CMainFrame中OnCreate 函數最後添加創建button的函數並顯示button,

(注意:在創建button函數create中如果使用了WS_VISIBLE,那麼ShowWindow可以不用)

最後運行,可以看到顯示添加的button在框架顯示欄範圍內(根據需要調整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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章