17windows_17_listbox列表框
#include <windows.h>
#include <iostream>
#include "resource.h"
CHAR szText[256] = { 0 };
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
HINSTANCE g_hInst = NULL; //窗口句柄
HANDLE g_hStdout = NULL; //控制台句柄
HWND g_hSingle = NULL;
HWND g_hMulti = NULL;
void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
g_hSingle = CreateWindow("LISTBOX", "SINGLE", WS_CHILD | WS_VISIBLE|LBS_NOTIFY/*发送LBN_SELCHANG消息需要*/,
50, 50, 200, 400, hWnd, (HMENU)1001, g_hInst, NULL);
g_hMulti = CreateWindow("LISTBOX", "SINGLE", WS_CHILD | WS_VISIBLE | LBS_MULTIPLESEL|LBS_NOTIFY/*发送LBN_SELCHANG消息需要*/,
350, 50, 200, 400, hWnd, (HMENU)1002, g_hInst, NULL);
}
void OnAdd()
{
for (int i = 0; i < 10; i++)
{
//添加项
sprintf_s(szText, 256, "LISTBOX %d",i);
SendMessage(g_hSingle, LB_ADDSTRING,
0, (LPARAM)szText);
SendMessage(g_hMulti, LB_ADDSTRING,
0, (LPARAM)szText);
}
}
void OnSelect()
{
//设置当前选择项
SendMessage(g_hSingle, LB_SETCURSEL, 2, 0); //和Combo一样
//Multi,设置指定项的选择状态
for (int i = 0; i < 10; i++)
{
SendMessage(g_hMulti, LB_SETSEL, TRUE, i);
}
//获取所有选择项的数量
LRESULT nCount = SendMessage(g_hMulti, LB_GETSELCOUNT, 0, 0);
if (nCount == 10)
{
MessageBox(NULL, "it‘s ten", "OK", MB_OK);
}
//获取选择项的索引号
INT nSel[20] = { 0 };
SendMessage(g_hMulti, LB_GETSELITEMS, 20, (LPARAM)nSel);
}
void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
int nNotifyCode = HIWORD(wParam);
int nCtrlID = LOWORD(wParam);
switch (nCtrlID)
{
case ID_LISTBOX_ADDITEM:
OnAdd();
break;
case ID_LISTBOX_SELETEITEM:
OnSelect();
break;
case 1001:
switch (nNotifyCode)
{
case LBN_SELCHANGE:
MessageBox(NULL, "LBN_SELCHANG", "LBN", MB_OK);
break;
}
break;
case 1002:
switch (nNotifyCode)
{
case LBN_SELCHANGE:
MessageBox(NULL, "LBN_SELCHANGMulti", "LBNMulti", MB_OK);
break;
}
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
//Create WM_CREATE
case WM_CREATE:
OnCreate(hWnd, nMsg, wParam, lParam);
break;
//menu use
case WM_COMMAND:
OnCommand(hWnd, nMsg, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
BOOL RegisterWnd(LPSTR pszClassName)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
ATOM atom = RegisterClassEx(&wce);
if (atom == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
HWND CreateWnd(LPSTR pszClassName)
{
HMENU hMainMENU = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, hMainMENU, g_hInst, 0);
return hWnd;
}
void ShowWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Msg()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void ConsoleWnd()
{
AllocConsole();
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR szText[] = "Debug start:\n";
WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_hInst = hInstance;
//ConsoleWnd();
RegisterWnd("oooo");
HWND hWnd = CreateWnd("oooo");
ShowWnd(hWnd);
Msg();
return 0;
}
时间: 2024-11-16 14:32:18