列表框控制元簡介

列表框控制元常用于集中顯示同種型别的一系列內容以供用戶選擇。列表框一般具有如下特點:

可提供大量的可選項(需要時自動顯示捲軸)

可設置單選(單個選項)或多選(多項選擇)功能

單選時,單擊列表框,被選的項以反色顯示表示被選中;再次單擊該選項,恢復為非選中

狀態

程式:

//================================================================

//程式說明:

//           創建一個主視窗,並在主視窗上放置兩個列表框控制元,然後添加兩個按鈕

//            ADDDEL,如圖 2.18所示。左側列表框中初始有一些字串,當點擊ADD

//            鈕時,實現將左//側列表框中被選中的項添加至右側的列表框中;當點擊DEL

//            按鈕時,實現將右側列表框中被選中的項刪除。

//================================================================


#include "windows.h"

#define ID_ADD      100

#define ID_DEL       101

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain( HINSTANCE hInstance,

                                        HINSTANCE hPrevInstance,

                                        LPSTR     lpCmdLine,

                                        int       nCmdShow)

{

       TCHAR szAppName[]  = "MyListBox";                 // 自定義的類名不能和系統已有的類名相同,

       TCHAR szAppTitle[] = "ListBox_Test";            // 否則在調用與此類同名的系統類的回調函數時會調用此類的視窗的回調函數而不是系統類的回調函數

        HWND hwnd;

        MSG msg;

        WNDCLASS wndclass;


        wndclass.style         = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;

        wndclass.lpfnWndProc   = (WNDPROC)WndProc;

        wndclass.cbClsExtra    = 0;

        wndclass.cbWndExtra    = 0;

        wndclass.hInstance     = hInstance;

        wndclass.hIcon         = 0;

        wndclass.hCursor       = 0;

        wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);

        wndclass.lpszMenuName  = NULL;

        wndclass.lpszClassName = szAppName;


        RegisterClass(&wndclass);

        MwRegisterButtonControl(NULL);

        MwRegisterListboxControl(NULL);

        hwnd = CreateWindowEx(  0L,

                                                        szAppName,                   // 視窗類名

                                                        szAppTitle,                     // 視窗標題

                                                        WS_OVERLAPPEDWINDOW | WS_VISIBLE,       // 視窗樣式

                                                        CW_USEDEFAULT,        // 視窗左上角坐標

                                                        CW_USEDEFAULT,

                                                        260,                              // 視窗大小

                                                        200,

                                                        NULL,                            // 父窗體執行緒

                                                        NULL,                            // 主選單執行緒

                                                        NULL,                            // 實例執行緒

                                                        NULL);                           // 視窗參數


        ShowWindow(hwnd, nCmdShow);             // 設定視窗顯示模式

        UpdateWindow(hwnd);                              // 立即繪製視窗


        while (GetMessage(&msg, NULL, 0, 0))      // 消息循環

        {

                TranslateMessage(&msg);

                DispatchMessage(&msg);

        }

        return msg.wParam;

}


//====================================================

//列表框控制元常用樣式及說明

//   LBS_NOTIFY 列表框可向父視窗發出消息

//   LBS_SORT 按字母順序排列表項

//   LBS_MULTIPLESEL 允許選擇多項

//   LBS_STANDARD 標準樣式, 即同時具有LBS_NOTIFY

//   LBS_SORTWS_VSCROLLWS_BORDER 樣式

//====================================================

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

        static HWND hListSrc, hListDst;


        switch(message)

        {

                case WM_CREATE:

                        hListSrc = CreateWindowEx(0L, "LISTBOX", NULL,

                                                                WS_VISIBLE | WS_CHILD | WS_VSCROLL | LBS_STANDARD,

                                                                20, 20, 100, 100,

                                                                hWnd, NULL, NULL, NULL);

                        hListDst = CreateWindowEx(0L, "LISTBOX", NULL,

                                                                WS_VISIBLE | WS_CHILD | WS_VSCROLL | LBS_STANDARD,

                                                                130, 20, 100, 100,

                                                                hWnd, NULL, NULL, NULL);

                        CreateWindowEx(0L, "Button",

                                                  "ADD",

                                                   WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,

                                                   20, 150, 50, 20,

                                                    hWnd, (HMENU)ID_ADD, NULL, NULL);

                        CreateWindowEx(0L, "Button",

                                                  "DEL",

                                                   WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,

                                                   160, 150, 50, 20,

                                                   hWnd, (HMENU)ID_DEL, NULL, NULL);

                        SendMessage(hListSrc, LB_ADDSTRING, 0, (LPARAM)"ListItem1");

                        SendMessage(hListSrc, LB_ADDSTRING, 0, (LPARAM)"ListItem2");

                        SendMessage(hListSrc, LB_ADDSTRING, 0, (LPARAM)"ListItem3");

                        SendMessage(hListSrc, LB_ADDSTRING, 0, (LPARAM)"ListItem4");

                        SendMessage(hListSrc, LB_ADDSTRING, 0, (LPARAM)"ListItem5");

                        break;

                case WM_COMMAND:

                        switch(LOWORD(wParam))

                        {

                                case ID_ADD:

                                {

                                        int CurSel = SendMessage(hListSrc, LB_GETCURSEL, 0, 0);

                                        char CurBuf[10] = "";

                                        SendMessage(hListSrc, LB_GETTEXT, CurSel, (LPARAM)CurBuf);

                                        SendMessage(hListDst, LB_ADDSTRING, 0, (LPARAM)CurBuf);

                                }

                                break;

                                case ID_DEL:

                                {

                                        int CurSel = SendMessage(hListDst, LB_GETCURSEL, 0, 0);

                                        SendMessage(hListDst, LB_DELETESTRING, CurSel, 0);

                                }

                                break;

                        }

                        break;

                case WM_DESTROY:

                        PostQuitMessage(0);

                        break;

                default:

                        return DefWindowProc(hWnd, message, wParam, lParam);

        }

        return 0;

}

運行結果:

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 pixnote21 的頭像
    pixnote21

    pixnote21的隨手筆記

    pixnote21 發表在 痞客邦 留言(0) 人氣()