在VC++中創建控件數組的方法有以下幾種:
#include <Windows.h>
#include <vector>
HWND hButtons[5]; // 控件指針數組
for (int i = 0; i < 5; i++) {
hButtons[i] = CreateWindowEx(0, L"BUTTON", L"Button", WS_VISIBLE | WS_CHILD,
10 + i * 100, 10, 80, 30, hWnd, NULL, hInstance, NULL);
}
#include <Windows.h>
#include <vector>
std::vector<HWND> hButtons; // 控件指針向量
for (int i = 0; i < 5; i++) {
HWND hButton = CreateWindowEx(0, L"BUTTON", L"Button", WS_VISIBLE | WS_CHILD,
10 + i * 100, 10, 80, 30, hWnd, NULL, hInstance, NULL);
hButtons.push_back(hButton); // 添加控件指針到向量
}
#include <Windows.h>
HWND* hButtons = new HWND[5]; // 動態數組
for (int i = 0; i < 5; i++) {
hButtons[i] = CreateWindowEx(0, L"BUTTON", L"Button", WS_VISIBLE | WS_CHILD,
10 + i * 100, 10, 80, 30, hWnd, NULL, hInstance, NULL);
}
// 使用完后記得釋放內存
delete[] hButtons;
以上是幾種常見的方法,具體選擇哪種方法取決于具體的需求和場景。