How can C++ Builder display multiple images?

In C++ Builder, you can display multiple images by using the TImage component and TOpenPictureDialog component.

Here is one way to display multiple images:

  1. In the interface designer of C++ Builder, drag and drop a TImage component onto the form as the display area for the image.
  2. Add a TOpenPictureDialog component to the form for selecting multiple images.
  3. In the code of the form, create a TStringList object to store the file paths of selected multiple images.
  4. In events where multiple images need to be displayed (such as a button click event), use the Execute method of the TOpenPictureDialog component to select multiple image files, and save the selected file paths to a TStringList object.
  5. Iterate through the file paths in the TStringList object, load each image using the Picture property of the TImage component, then adjust the position and size of the TImage component to display multiple images.

Here is an example code:

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <Vcl.Dialogs.hpp>

#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TOpenPictureDialog *OpenPictureDialog = new TOpenPictureDialog(this);
    TStringList *ImageFiles = new TStringList();
    if (OpenPictureDialog->Execute())
    {
        ImageFiles->Assign(OpenPictureDialog->Files);
        for (int i = 0; i < ImageFiles->Count; i++)
        {
            TImage *Image = new TImage(this);
            Image->Parent = this;
            Image->Picture->LoadFromFile(ImageFiles->Strings[i]);
            // 根据需要调整图片的位置和尺寸
            Image->Left = i * 100;
            Image->Top = 50;
            Image->Width = 100;
            Image->Height = 100;
        }
    }
    delete OpenPictureDialog;
    delete ImageFiles;
}

In the code above, when the button is clicked, a file selection dialog box will pop up. After selecting multiple image files, multiple images will be displayed on the form. Each image is displayed using a new TImage component, with the position and size adjusted as needed.

bannerAds