<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 控件 III > 原文: [http://zetcode.com/gui/winapi/controlsIII/](http://zetcode.com/gui/winapi/controlsIII/) 在本章中,我們將結束有關 Windows API 控件的討論。 我們將提到單選按鈕,單選框,組合框和進度條。 ## `RadioButton`和分組框 在這里,我們介紹兩個控件。分組框是圍繞一組控件的矩形。 這些通常是單選按鈕。 分組框具有描述控件的標簽。 此控件的目的是對某種程度上相關的控件進行分組。單選按鈕是一種特殊的按鈕,用戶可以選擇但不能清除。 它允許用戶從一組選項中選擇一個獨占選項。 `radio_buttons.c` ```c #include <windows.h> #define ID_BLUE 1 #define ID_YELLOW 2 #define ID_ORANGE 3 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HINSTANCE g_hinst; COLORREF g_color; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg ; WNDCLASSW wc = {0}; wc.lpszClassName = L"GroupBox"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); g_hinst = hInstance; RegisterClassW(&wc); hwnd = CreateWindowW(wc.lpszClassName, L"GroupBox", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 300, 170, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; HBRUSH hBrush, holdBrush; HPEN hPen, holdPen; switch(msg) { case WM_CREATE: CreateWindowW(L"Button", L"Choose colour", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 10, 10, 120, 110, hwnd, (HMENU) 0, g_hinst, NULL); CreateWindowW(L"Button", L"Blue", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 20, 30, 100, 30, hwnd, (HMENU) ID_BLUE , g_hinst, NULL); CreateWindowW(L"Button", L"Yellow", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 20, 55, 100, 30, hwnd, (HMENU) ID_YELLOW , g_hinst, NULL); CreateWindowW(L"Button", L"Orange", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 20, 80, 100, 30, hwnd, (HMENU) ID_ORANGE , g_hinst, NULL); break; case WM_COMMAND: if (HIWORD(wParam) == BN_CLICKED) { switch (LOWORD(wParam)) { case ID_BLUE: g_color = RGB(0, 76, 255); break; case ID_YELLOW: g_color = RGB(255, 255, 0); break; case ID_ORANGE: g_color = RGB(255, 123, 0); break; } InvalidateRect(hwnd, NULL, TRUE); } break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); hBrush = CreateSolidBrush(g_color); hPen = CreatePen(PS_NULL, 1, RGB(0, 0, 0)); holdPen = SelectObject(hdc, hPen); holdBrush = (HBRUSH) SelectObject(hdc, hBrush); Rectangle(hdc, 160, 20, 260, 120); SelectObject(hdc, holdBrush); SelectObject(hdc, holdPen); DeleteObject(hPen); DeleteObject(hBrush); EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } ``` 在我們的示例中,我們有一個帶有三個單選按鈕的分組框。 通過單擊單選按鈕,我們為右側的矩形選擇背景色。 ```c CreateWindowW(L"Button", L"Choose colour", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 10, 10, 120, 110, hwnd, (HMENU) 0, g_hinst, NULL); ``` 分組框是一種特殊的按`BS_GROUPBOX`樣式創建的按鈕。 ```c CreateWindowW(L"Button", L"Blue", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 20, 30, 100, 30, hwnd, (HMENU) ID_BLUE , g_hinst, NULL); ``` 單選按鈕也是`BS_AUTORADIOBUTTON`樣式的特殊按鈕。 ```c case ID_BLUE: g_color = RGB(0, 76, 255); break; ``` 如果單擊單選按鈕,則將使用選定的顏色填充全局變量。 此變量用于創建填充矩形的畫筆。 ```c InvalidateRect(hwnd, NULL, TRUE); ``` 我們使矩形(在本例中為整個窗口)無效,這將導致重繪客戶區。 這將啟動`WM_PAINT`消息。 在`WM_PAINT`消息期間,我們繪制矩形。 在 [GDI](../gdi) 一章中更詳細地說明了繪圖。 ![Radio buttons in a GroupBox](https://img.kancloud.cn/9f/cf/9fcfa2bf07db3613a62e1021be6e48b2_300x170.jpg) 圖:`GroupBox`中的單選按鈕 ## `ComboBox` 組合框是編輯框或靜態文本與列表的組合。 當我們需要從可用選項列表中選擇一個項目時,將使用一個組合框。 `combobox.c` ```c #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HINSTANCE g_hinst; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg ; WNDCLASSW wc = {0}; wc.lpszClassName = L"Application"; wc.hInstance = hInstance ; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc ; wc.hCursor = LoadCursor(0,IDC_ARROW); g_hinst = hInstance; RegisterClassW(&wc); hwnd = CreateWindowW(wc.lpszClassName, L"Combo box", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 270, 170, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hwndCombo, hwndStatic; const wchar_t *items[] = { L"FreeBSD", L"OpenBSD", L"NetBSD", L"Solaris", L"Arch" }; switch(msg) { case WM_CREATE: hwndCombo = CreateWindowW(L"Combobox", NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWN, 10, 10, 120, 110, hwnd, NULL, g_hinst, NULL); CreateWindowW(L"Button", L"Drop down", WS_CHILD | WS_VISIBLE, 150, 10, 90, 25, hwnd, (HMENU) 1, g_hinst, NULL); hwndStatic = CreateWindowW(L"Static", L"", WS_CHILD | WS_VISIBLE, 150, 80, 90, 25, hwnd, NULL, g_hinst, NULL); for (int i = 0; i < 4; i++ ) { SendMessageW(hwndCombo, CB_ADDSTRING, 0, (LPARAM) items[i]); } break; case WM_COMMAND: if (HIWORD(wParam) == BN_CLICKED) { SendMessage(hwndCombo, CB_SHOWDROPDOWN, (WPARAM) TRUE, 0); } if (HIWORD(wParam) == CBN_SELCHANGE) { LRESULT sel = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0); SetWindowTextW(hwndStatic, items[sel]); } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } ``` 在我們的示例中,我們在窗口上放置了三個控件:一個組合框,一個按鈕和一個靜態文本。 靜態文本顯示組合框中當前選擇的項目。 用于演示`CBN_SELCHANGE`組合框消息。 該按鈕以編程方式打開組合框。 ```c hwndCombo = CreateWindowW(L"Combobox", NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWN, 10, 10, 120, 110, hwnd, NULL, g_hinst, NULL); ``` 要創建一個組合框,我們使用`L"Combobox"`窗口類。 `CBS_DROPDOWN`標志創建一個下拉列表。 ```c for (int i = 0; i < 4; i++ ) { SendMessageW(hwndCombo, CB_ADDSTRING, 0, (LPARAM) items[i]); } ``` 我們用項目填充組合框。 要向組合框添加字符串,我們發送`CB_ADDSTRING`消息。 ```c if (HIWORD(wParam) == BN_CLICKED) { SendMessage(hwndCombo, CB_SHOWDROPDOWN, (WPARAM) TRUE, 0); } ``` 單擊該按鈕將導致發送`CB_SHOWDROPDOWN`消息,該消息將以編程方式調用下拉框。 如果從組合框中選擇一個項目,則窗口過程將接收`WM_COMMAND`消息,并在`wParam`參數的高位字中帶有通知消息`CBN_SELCHANGE`。 ```c if (HIWORD(wParam) == CBN_SELCHANGE) { LRESULT sel = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0); SetWindowTextW(hwndStatic, items[sel]); } ``` 我們找出當前選擇的項目。 我們向組合框發送`CB_GETCURSEL`消息。 該函數返回當前所選項目的索引。 我們將靜態文本設置為當前選擇的字符串。 ![Combo box](https://img.kancloud.cn/70/93/70937684dd7372ab89944a33b93eaf9c_270x170.jpg) 圖:組合框 ## 進度條 進度條是當我們處理冗長的任務時使用的控件。 它具有動畫效果,以便用戶知道我們的任務正在進行中。 `progressbar.c` ```c #include <windows.h> #include <commctrl.h> #define ID_BUTTON 1 #define ID_TIMER 2 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void CreateControls(HWND); HWND hwndPrgBar; HWND hbtn; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg ; WNDCLASSW wc = {0}; wc.lpszClassName = L"Application"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); hwnd = CreateWindowW(wc.lpszClassName, L"Progress bar", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 260, 170, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static int i = 0; switch(msg) { case WM_CREATE: CreateControls(hwnd); break; case WM_TIMER: SendMessage(hwndPrgBar, PBM_STEPIT, 0, 0); i++; if (i == 150) { KillTimer(hwnd, ID_TIMER); SendMessageW(hbtn, WM_SETTEXT, (WPARAM) NULL, (LPARAM) L"Start"); i = 0; } break; case WM_COMMAND: if (i == 0) { i = 1; SendMessage(hwndPrgBar, PBM_SETPOS, 0, 0); SetTimer(hwnd, ID_TIMER, 5, NULL); SendMessageW(hbtn, WM_SETTEXT, (WPARAM) NULL, (LPARAM) L"In progress"); } break; case WM_DESTROY: KillTimer(hwnd, ID_TIMER); PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } void CreateControls(HWND hwnd) { INITCOMMONCONTROLSEX icex; icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwICC = ICC_PROGRESS_CLASS; InitCommonControlsEx(&icex); hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 30, 20, 190, 25, hwnd, NULL, NULL, NULL); hbtn = CreateWindowW(L"Button", L"Start", WS_CHILD | WS_VISIBLE, 85, 90, 85, 25, hwnd, (HMENU) 1, NULL, NULL); SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150)); SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0); } ``` 在我們的示例中,我們有一個進度條和一個按鈕。 該按鈕啟動一個計時器,該計時器更新進度條。 ```c hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 30, 20, 190, 25, hwnd, NULL, NULL, NULL); ``` 我們使用`PROGRESS_CLASS`類名稱和`PBS_SMOOTH`樣式創建進度條控件。 ```c SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150)); SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0); ``` 我們設置進度條的范圍及其步驟。 ```c i = 1; SendMessage(hwndPrgBar, PBM_SETPOS, 0, 0); SetTimer(hwnd, ID_TIMER, 5, NULL); ``` 當按下開始按鈕時,我們將`i`值設置為 1,設置進度條的初始位置,然后啟動計時器。 計時器將定期向窗口過程發送`WM_TIMER`消息,直到被殺死。 ```c SendMessageW(hbtn, WM_SETTEXT, (WPARAM) NULL, (LPARAM) L"In progress"); ``` 當計時器進行時,我們更改按鈕的標簽。 ```c case WM_TIMER: SendMessage(hwndPrgBar, PBM_STEPIT, 0, 0); i++; if (i == 150) { KillTimer(hwnd, ID_TIMER); SendMessageW(hbtn, WM_SETTEXT, (WPARAM) NULL, (LPARAM) L"Start"); i = 0; } break; ``` 當我們收到`WM_TIMER`消息時,我們通過發送`PBM_STEPIT`消息的一步來更新進度條。 當`i`變量達到進度條的上限時,計時器將被終止。 ![Progress bar](https://img.kancloud.cn/32/ad/32add02fadd8337ec371c01fb7f97137_260x170.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>

                              哎呀哎呀视频在线观看