[英]How to add 2 listboxes to 1 window using Visual Studio C++?
I have this code that works for 1 listbox but adding another one right below the first one is problematic.我有这个适用于 1 个列表框的代码,但是在第一个列表框的正下方添加另一个是有问题的。 This is C code that works fine in C++.这是在 C++ 中正常工作的 C 代码。 Also, duplicating the hwndList just erases the first listbox.此外,复制 hwndList 只会删除第一个列表框。
#include "framework.h"
#include "environ.h"
#define ID_LIST 1
#define ID_TEXT 2
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Environ");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Environment List Box"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
fills the content of the listbox填充列表框的内容
void FillListBox(HWND hwndList)
{
int iLength;
TCHAR* pVarBlock, * pVarBeg, * pVarEnd, * pVarName;
TCHAR* pVarBlockCopy = GetEnvironmentStrings(); // Get pointer to environment block
pVarBlock = GetEnvironmentStrings(); // Get pointer to environment block
while (*pVarBlock)
{
if (*pVarBlock != '=') // Skip variable names beginning with `=`
{
pVarBeg = pVarBlock; // Beginning of variable name
while (*pVarBlock++ != '='); // Scan until `=`
pVarEnd = pVarBlock - 1; // Points to `=` sign
iLength = pVarEnd - pVarBeg; // Length of variable name
// Allocate memory for the variable name and terminating
// zero. Copy the variable name and append a zero.
pVarName = (TCHAR*)(calloc(iLength + 1, sizeof(TCHAR)));
CopyMemory(pVarName, pVarBeg, iLength * sizeof(TCHAR));
pVarName[iLength] = '\0' ;
// Put the variable name in the list box and free memory.
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)pVarName);
free(pVarName);
}
while (*pVarBlock++ != '\0') ; // Scan until terminating zero
}
FreeEnvironmentStrings(pVarBlockCopy);
}
This section of the code seems to handle the creation of the listbox and the text box.这部分代码似乎处理了列表框和文本框的创建。 I attempted to copy the listbox code and modify the code with a 2 to see if adjusting the location of the listbox might make the code function as two listbox windows program.我试图复制列表框代码并使用 2 修改代码,以查看调整列表框的位置是否可能使代码 function 成为两个列表框 windows 程序。
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndList, hwndText;
int iIndex, iLength, cxChar, cyChar;
TCHAR* pVarName, * pVarValue;
switch (message)
{
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
// Create listbox and static text windows.
hwndList = CreateWindow(TEXT("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
cxChar, cyChar * 3,
cxChar * 16 + GetSystemMetrics(SM_CXVSCROLL),
cyChar * 5,
hwnd, (HMENU)ID_LIST,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
hwndText = CreateWindow(TEXT("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_LEFT,
cxChar, cyChar,
GetSystemMetrics(SM_CXSCREEN), cyChar,
hwnd, (HMENU)ID_TEXT,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
FillListBox(hwndList);
return 0;
case WM_SETFOCUS:
SetFocus(hwndList);
return 0;
case WM_COMMAND:
if (LOWORD(wParam) == ID_LIST && HIWORD(wParam) == LBN_SELCHANGE)
{
// Get current selection.
iIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
iLength = SendMessage(hwndList, LB_GETTEXTLEN, iIndex, 0) + 1;
pVarName = (TCHAR*)(calloc(iLength, sizeof(TCHAR)));
SendMessage(hwndList, LB_GETTEXT, iIndex, (LPARAM)pVarName);
// Get environment string.
iLength = GetEnvironmentVariable(pVarName, NULL, 0);
pVarValue = (TCHAR *) (calloc(iLength, sizeof(TCHAR)));
GetEnvironmentVariable(pVarName, pVarValue, iLength);
// Show it in window.
SetWindowText(hwndText, pVarValue);
free(pVarName);
free(pVarValue);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
I added a new ID_NEWLIST
to your source code and created a listbox in the same way.我在您的源代码中添加了一个新的ID_NEWLIST
并以相同的方式创建了一个列表框。
Here is the sample:这是示例:
#include <windows.h>
#define ID_LIST 1
#define ID_TEXT 2
#define ID_NEWLIST 3
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void FillListBox(HWND hwndList)
{
int iLength;
TCHAR* pVarBlock, * pVarBeg, * pVarEnd, * pVarName;
TCHAR* pVarBlockCopy = GetEnvironmentStrings(); // Get pointer to environment block
pVarBlock = GetEnvironmentStrings(); // Get pointer to environment block
while (*pVarBlock)
{
if (*pVarBlock != '=') // Skip variable names beginning with `=`
{
pVarBeg = pVarBlock; // Beginning of variable name
while (*pVarBlock++ != '='); // Scan until `=`
pVarEnd = pVarBlock - 1; // Points to `=` sign
iLength = pVarEnd - pVarBeg; // Length of variable name
// Allocate memory for the variable name and terminating
// zero. Copy the variable name and append a zero.
pVarName = (TCHAR*)(calloc(iLength + 1, sizeof(TCHAR)));
CopyMemory(pVarName, pVarBeg, iLength * sizeof(TCHAR));
pVarName[iLength] = '\0';
// Put the variable name in the list box and free memory.
SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)pVarName);
free(pVarName);
}
while (*pVarBlock++ != '\0'); // Scan until terminating zero
}
FreeEnvironmentStrings(pVarBlockCopy);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Environ");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Environment List Box"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndList, hwndText, hwndNewList;
int iIndex, iLength, cxChar, cyChar;
TCHAR* pVarName, * pVarValue;
switch (message)
{
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
// Create listbox and static text windows.
hwndList = CreateWindow(TEXT("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
cxChar, cyChar * 3,
cxChar * 16 + GetSystemMetrics(SM_CXVSCROLL),
cyChar * 5,
hwnd, (HMENU)ID_LIST,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
hwndNewList = CreateWindow(TEXT("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
cxChar, cyChar * 8,
cxChar * 16 + GetSystemMetrics(SM_CXVSCROLL),
cyChar * 5,
hwnd, (HMENU)ID_NEWLIST,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
hwndText = CreateWindow(TEXT("static"), NULL,
WS_CHILD | WS_VISIBLE | SS_LEFT,
cxChar, cyChar,
GetSystemMetrics(SM_CXSCREEN), cyChar,
hwnd, (HMENU)ID_TEXT,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
FillListBox(hwndList);
FillListBox(hwndNewList);
return 0;
case WM_SETFOCUS:
SetFocus(hwndList);
return 0;
case WM_COMMAND:
if (LOWORD(wParam) == ID_LIST && HIWORD(wParam) == LBN_SELCHANGE)
{
// Get current selection.
iIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
iLength = SendMessage(hwndList, LB_GETTEXTLEN, iIndex, 0) + 1;
pVarName = (TCHAR*)(calloc(iLength, sizeof(TCHAR)));
SendMessage(hwndList, LB_GETTEXT, iIndex, (LPARAM)pVarName);
// Get environment string.
iLength = GetEnvironmentVariable(pVarName, NULL, 0);
pVarValue = (TCHAR*)(calloc(iLength, sizeof(TCHAR)));
GetEnvironmentVariable(pVarName, pVarValue, iLength);
// Show it in window.
SetWindowText(hwndText, pVarValue);
free(pVarName);
free(pVarValue);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
If you encounter other problems during the creation, I hope you can further point them out so that we can solve them for you.如果您在创作过程中遇到其他问题,希望您能进一步指出,以便我们为您解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.