我想在窗口中实现一个列表框和一个滑块。
我使用devcpp,而不使用Visual Studio。

我一直在寻找一种做到这一点的方法,但除了msdn.microsoft.com提供的理论性内容外,我什么也没找到。

我想要一个示例,它是实现列表框和滑块的最小代码。
这是更紧密,最有用的链接,但是仍然使用Visual Studio。
谢谢。

最佳答案

我找到了我想要的东西,似乎这是制作列表框时整个互联网上唯一的示例代码。为了运行它:下载并打开devc ++(我使用的)或任何其他类型的编译器,打开一个新的Windows应用程序项目(否则它将不起作用),清除任何默认代码,粘贴此代码并运行它。

这就是您帮助他人学习代码的方式。

http://www.dreamincode.net/forums/topic/291276-win32-listbox/

#include <Windows.h>


#define IDC_MAIN_BUTTON 101         // Button identifier


LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

HWND hWndListL;
HWND hWndListR;
HWND hWndButton;


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG Msg;
    HWND hWnd;


    //Settings All Window Class Variables
    WNDCLASSEX WndClsEx;
    WndClsEx.cbSize = sizeof(WNDCLASSEX);
    WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc = WndProcedure;
    WndClsEx.cbClsExtra = 0;
    WndClsEx.cbWndExtra = 0;
    WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClsEx.hInstance = hInstance;
    WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClsEx.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    WndClsEx.lpszMenuName = NULL;
    WndClsEx.lpszClassName = "My Window";


    //Register Window Class
    RegisterClassEx(&WndClsEx);


    //Create Window
    hWnd = CreateWindowEx(NULL, "My Window", "Windows Application", WS_OVERLAPPEDWINDOW, 200, 200, 640, 480, NULL, NULL, hInstance, NULL);



    SendMessage(hWndListL, LB_ADDSTRING, NULL, (LPARAM)"one");
    SendMessage(hWndListL, LB_ADDSTRING, NULL, (LPARAM)"two");



    //Show Window
    ShowWindow(hWnd, SW_SHOWNORMAL);


    while(GetMessage(&Msg, NULL, 0, 0))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }


    return 0;
}



LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    char buffer[50] = "";


    switch(Msg)
    {
        case WM_CREATE:
            //Create Listbox's
            hWndListL = CreateWindowEx(NULL, "LISTBOX", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | LBS_NOTIFY, 50, 35, 200, 100, hWnd, NULL, GetModuleHandle(NULL), NULL);
            hWndListR = CreateWindowEx(NULL, "LISTBOX", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | LBS_NOTIFY, 350, 35, 200, 100, hWnd, NULL, GetModuleHandle(NULL), NULL);

            //Create Button
            hWndButton = CreateWindowEx(NULL, "BUTTON", "OK", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_DISABLED, 50, 220, 100, 24, hWnd, (HMENU)IDC_MAIN_BUTTON, GetModuleHandle(NULL), NULL);

            break;
        case WM_COMMAND:
            switch(HIWORD(wParam))
            {
                case LBN_SELCHANGE:
                {
                    //EnableWindow( GetDlgItem( hWnd, (HMENU)IDC_BUTTON_MAIN ), TRUE );
                    EnableWindow(hWndButton, true);
                    break;
                }
            }
            switch(LOWORD(wParam))
            {
                case IDC_MAIN_BUTTON:
                {
                    //length = SendMessage(hWndListL, LB_GETTEXTLEN, NULL, NULL);
                    SendMessage(hWndListL, LB_GETTEXT, NULL, (LPARAM)buffer);
                    SendMessage(hWndListR, LB_ADDSTRING, NULL, (LPARAM)buffer);
                    SendMessage(hWndListL, LB_DELETESTRING, NULL, NULL);
                    EnableWindow(hWndButton, false);
                    break;
                }
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
            break;
    }

    return DefWindowProc(hWnd, Msg, wParam, lParam);
}

关于c++ - C++ Win32列表框和滑块创建Windows应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45392095/

10-13 08:03