<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Windows API 控件 I > 原文: [http://zetcode.com/gui/winapi/controls/](http://zetcode.com/gui/winapi/controls/) 控件是 Windows 應用的基本構建塊。 (控件在 UNIX 中稱為小部件。)Windows API 教程的這一部分涵蓋了靜態控件,按鈕,復選框和編輯框。 控件也是窗口。 它們是使用`CreateWindowW()`或`CreateWindowExW()`函數創建的。 這些函數分別將窗口類名稱作為其第一個和第二個參數。 控件具有其特定的預定義窗口類名稱; 因此,在創建控件時,我們不會調用`RegisterClassW()`或`RegisterClassExW()`。 ## 靜態控件 靜態控件顯示文本和圖形。 無法選擇靜態控件。 它還不能具有鍵盤焦點。 ### 靜態文字 在第一個示例中,我們創建一個靜態文本控件。 `static_text.c` ```c #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Static Control"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Criminal", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 330, 270, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static wchar_t *lyrics = L"I know you told me I should stay away\n\ I know you said he's just a dog astray\n\ He is a bad boy with a tainted heart\n\ And even I know this ain't smart\n\ \n\ But mama, I'm in love with a criminal\n\ And this type of love isn't rational, it's physical\n\ Mama, please don't cry, I will be alright\n\ All reason aside, I just can't deny, love the guy\n\ "; switch(msg) { case WM_CREATE: CreateWindowW(L"Static", lyrics, WS_CHILD | WS_VISIBLE | SS_LEFT, 20, 20, 300, 230, hwnd, (HMENU) 1, NULL, NULL); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } ``` 該示例在窗口上顯示歌曲的歌詞。 ```c CreateWindowW(L"Static", lyrics, WS_CHILD | WS_VISIBLE | SS_LEFT, 20, 20, 300, 230, hwnd, (HMENU) 1, NULL, NULL); break; ``` 靜態控件是使用`L"Static"`類創建的。 文本以`SS_LEFT`樣式向左對齊。 ![Static text control](https://img.kancloud.cn/19/0c/190c22b4511ec4ce66cba7e733afffdb_330x270.jpg) Static text control ### 靜態圖像 第二個示例創建一個靜態圖像控件。 `static_image.c` ```c #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void LoadMyImage(void); HBITMAP hBitmap; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Static image"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0,IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Static image", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 330, 270, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HWND hsti; switch(msg) { case WM_CREATE: LoadMyImage(); hsti = CreateWindowW(L"Static", L"", WS_CHILD | WS_VISIBLE | SS_BITMAP, 5, 5, 300, 300, hwnd, (HMENU) 1, NULL, NULL); SendMessage(hsti, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hBitmap); break; case WM_DESTROY: DeleteObject(hBitmap); PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } void LoadMyImage(void) { hBitmap = LoadImageW(NULL, L"C:\\prog\\slovakia.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); } ``` 該示例在窗口上顯示了 BMP 圖像。 ```c hsti = CreateWindowW(L"Static", L"", WS_CHILD | WS_VISIBLE | SS_BITMAP, 5, 5, 300, 300, hwnd, (HMENU) 1, NULL, NULL); ``` `SS_BITMAP`常量使靜態控件顯示位圖。 ```c SendMessage(hsti, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hBitmap); ``` 發送`STM_SETIMAGE`消息以將新圖像與靜態控件關聯。 ```c void LoadMyImage(void) { hBitmap = LoadImageW(NULL, L"C:\\prog\\slovakia.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); } ``` `LoadImageW()`函數從文件系統加載位圖。 如果函數成功,則返回值是新加載的圖像的句柄。 ![Static image control](https://img.kancloud.cn/b8/dd/b8dd5f7ca36740bc17f6280da9a9300b_330x270.jpg) Static image control ## 按鈕 按鈕是帶有文本標簽的簡單控件。 用于觸發動作。 當我們單擊一個按鈕時,它會向其父窗口發送`WM_COMMAND`消息。 `wParam`參數的低位字包含控件標識符。 `button.c` ```c #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); #define ID_BEEP 1 #define ID_QUIT 2 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Buttons"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Buttons", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 150, 150, 300, 200, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: CreateWindowW(L"Button", L"Beep", WS_VISIBLE | WS_CHILD , 20, 50, 80, 25, hwnd, (HMENU) ID_BEEP, NULL, NULL); CreateWindowW(L"Button", L"Quit", WS_VISIBLE | WS_CHILD , 120, 50, 80, 25, hwnd, (HMENU) ID_QUIT, NULL, NULL); break; case WM_COMMAND: if (LOWORD(wParam) == ID_BEEP) { MessageBeep(MB_OK); } if (LOWORD(wParam) == ID_QUIT) { PostQuitMessage(0); } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } ``` 在我們的示例中,我們創建了兩個按鈕。 一鍵鳴音。 另一個將關閉窗口。 ```c CreateWindowW(L"Button", L"Beep", WS_VISIBLE | WS_CHILD , 20, 50, 80, 25, hwnd, (HMENU) ID_BEEP, NULL, NULL); ``` 按鈕控件是使用`L"Button"`類創建的。 ```c case WM_COMMAND: if (LOWORD(wParam) == ID_BEEP) { MessageBeep(MB_OK); } if (LOWORD(wParam) == ID_QUIT) { PostQuitMessage(0); } break; ``` 控件的 ID 在`wParam`的`LOWORD`中。 根據 ID,我們稱為`MessageBeep()`函數或`PostQuitMessage()`函數。 ![Button controls](https://img.kancloud.cn/19/40/19405946079caa60a7ac42069e90f8d5_300x200.jpg) Button controls ## `CheckBox` 復選框控件是可以單擊以打開或關閉選項的框。 `checkbox.c` ```c #include <windows.h> #include <stdbool.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Check Box"; wc.hInstance = hInstance ; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Check Box", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 150, 150, 230, 150, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { bool checked = true; switch(msg) { case WM_CREATE: CreateWindowW(L"button", L"Show Title", WS_VISIBLE | WS_CHILD | BS_CHECKBOX, 20, 20, 185, 35, hwnd, (HMENU) 1, NULL, NULL); CheckDlgButton(hwnd, 1, BST_CHECKED); break; case WM_COMMAND: checked = IsDlgButtonChecked(hwnd, 1); if (checked) { CheckDlgButton(hwnd, 1, BST_UNCHECKED); SetWindowTextW(hwnd, L""); } else { CheckDlgButton(hwnd, 1, BST_CHECKED); SetWindowTextW(hwnd, L"Check Box"); } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } ``` 在我們的示例中,我們根據復選框的狀態顯示或隱藏窗口標題。 ```c CreateWindowW(L"button", L"Show Title", WS_VISIBLE | WS_CHILD | BS_CHECKBOX, 20, 20, 185, 35, hwnd, (HMENU) 1, NULL, NULL); ``` 復選框是一種特殊的按鈕。 它是用`BS_CHECKBOX`標志創建的。 ```c checked = IsDlgButtonChecked(hwnd, 1); ``` 我們使用`IsDlgButtonChecked()`函數確定復選框的狀態。 ```c CheckDlgButton(hwnd, 1, BST_UNCHECKED); ``` 我們使用`CheckDlgButton()`函數選中并取消選中該復選框。 ```c SetWindowTextW(hwnd, L""); ``` `SetWindowTextW()`函數設置窗口的標題。 ![Checkbox control](https://img.kancloud.cn/f8/2d/f82defc3f5265fa9a61ab2502f3b047e_230x150.jpg) Checkbox control ## 編輯控件 編輯控件是一個矩形子窗口,用于輸入和編輯文本。 它可以是單行或多行。 `edit.c` ```c #include <windows.h> #define ID_EDIT 1 #define ID_BUTTON 2 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Edit control"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Edit control", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 220, 220, 280, 200, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hwndEdit; HWND hwndButton; switch(msg) { case WM_CREATE: hwndEdit = CreateWindowW(L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 50, 50, 150, 20, hwnd, (HMENU) ID_EDIT, NULL, NULL); hwndButton = CreateWindowW(L"button", L"Set title", WS_VISIBLE | WS_CHILD, 50, 100, 80, 25, hwnd, (HMENU) ID_BUTTON, NULL, NULL); break; case WM_COMMAND: if (HIWORD(wParam) == BN_CLICKED) { int len = GetWindowTextLengthW(hwndEdit) + 1; wchar_t text[len]; GetWindowTextW(hwndEdit, text, len); SetWindowTextW(hwnd, text); } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } ``` 在我們的示例中,我們有一個編輯控件和一個按鈕。 我們可以將一些文本放入編輯控件中。 如果單擊按鈕,則輸入的文本將顯示在主窗口的標題欄中。 ```c hwndEdit = CreateWindowW(L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 50, 50, 150, 20, hwnd, (HMENU) ID_EDIT, NULL, NULL); ``` 編輯控件是使用`L"Edit"`窗口類創建的。 `WS_BORDER`窗口樣式在控件周圍創建細線邊框。 ```c if (HIWORD(wParam) == BN_CLICKED) { int len = GetWindowTextLengthW(hwndEdit) + 1; wchar_t text[len]; GetWindowTextW(hwndEdit, text, len); SetWindowTextW(hwnd, text); } ``` `GetWindowTextLengthW()`返回輸入文本的長度。 注意,我們在長度上加 1。 這將包括零終止符。 嘗試忽略它,看看會發生什么。 `GetWindowTextW()`從編輯控件接收文本。 該函數的第一個參數是包含文本的窗口或控件的句柄。 `SetWindowTextW()`設置窗口的文本。 在這種情況下,它是主窗口的標題。 ![Edit control](https://img.kancloud.cn/28/6c/286c517a91c1476259709955e458e9f8_280x200.jpg) 圖:編輯控件 在 Windows API 教程的這一部分中,我們介紹了四個基本的 Windows 控件。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看