Share via


如何建立按鈕

若要動態建立按鈕,您可以使用 CreateWindow 或 CreateWindowEx 函式。 本主題示範如何使用 CreateWindow 函式來建立預設的按鈕。

您需要知道的事項

技術

必要條件

  • C/C++
  • Windows 使用者介面程序設計

指示

使用 CreateWindow 函式來建立按鈕控件。

在下列 C++ 範例中 ,m_hwnd 參數是父視窗的句柄。 BS_DEFPUSHBUTTON樣式會指定應該建立預設的按鈕。 請注意,必須指定大小和位置值,因為針對按鈕使用 CW_USEDEFAULT 會將值設定為零。

HWND hwndButton = CreateWindow( 
    L"BUTTON",  // Predefined class; Unicode assumed 
    L"OK",      // Button text 
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
    10,         // x position 
    10,         // y position 
    100,        // Button width
    100,        // Button height
    m_hwnd,     // Parent window
    NULL,       // No menu.
    (HINSTANCE)GetWindowLongPtr(m_hwnd, GWLP_HINSTANCE), 
    NULL);      // Pointer not needed.

關於按鈕

按鈕控制件參考

使用按鈕

按鈕