温馨提示×

vc中创建控件数组的方法是什么

VC
小亿
81
2024-01-19 01:04:19
栏目: 编程语言

在VC++中创建控件数组的方法有以下几种:

  1. 使用普通的C++数组:可以声明一个指向控件指针的数组,然后使用循环逐个创建控件并保存到数组中。
#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);
}
  1. 使用std::vector:可以使用std::vector容器来保存控件指针,动态添加和删除控件。
#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);  // 添加控件指针到向量
}
  1. 使用动态数组:可以使用new操作符动态分配数组来保存控件指针。
#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;

以上是几种常见的方法,具体选择哪种方法取决于具体的需求和场景。

0