picture viewer C ++ builder

时间:2015-06-07 19:28:42

标签: c++ c++builder

__fastcall TForm1::FillList() {
TSearchRec FoundedFile;
int r, i;
int count = 0;
int ir = 1;
char ext[3] = {'jpg', 'gif', 'png'};
for (i = 0; i < 3; i++) {

r = FindFirst(Path + "*." + ext[i], faAnyFile, FoundedFile);

ListBox1->Items->Clear();
if (r == 0) {

    ListBox1->Items->Add(FoundedFile.Name);

    while (FindNext(FoundedFile) == 0) {
        ListBox1->Items->Add(FoundedFile.Name);

    }
    ListBox1->ItemIndex = 0;

    String File =
        String(Path + ListBox1->Items->Strings[ListBox1->ItemIndex]);
    //ShowMessage(File);
    Image1->Picture->LoadFromFile(File);
    count = ListBox1->Items->Count;
    }

}

if (ir == 0) {
    return -1;
}
else {
    count = ListBox1->Items->Count;
    return count;
}

}

按钮不会读取图片扩展数组ext [i]。 程序不会显示.gif,.png和.jpg扩展名的图片。 如果没有数组,只有r = FindFirst(Path +&#34;(我的扩展名)&#34;,faAnyFile,FoundedFile);,那么它会显示带有我的扩展名的图片。

2 个答案:

答案 0 :(得分:0)

您正在尝试填充ListBox并以相同的代码显示图像。它应该作为单独的操作实现。 FillList()应仅填充ListBox,然后您可以在ListBox的OnClick事件中显示所选图像(没有OnChange事件,除非您将ListBox子类化为拦截直接LBN_SELCHANGE通知),例如:

void __fastcall TForm1::FillList()
{
    TSearchRec FoundedFile;
    int r, i;
    char* ext[3] = {"jpg", "gif", "png"};

    ListBox1->Items->BeginUpdate();
    try
    {
        ListBox1->Items->Clear();

        for (i = 0; i < 3; i++)
        {
            r = FindFirst(Path + "*." + ext[i], faAnyFile, FoundedFile);
            if (r == 0)
            {
                do
                {
                    ListBox1->Items->Add(FoundedFile.Name);
                }
                while (FindNext(FoundedFile) == 0);
                FindClose(FoundedFile);
            }
        }
    }
    __finally
    {
        ListBox1->Items->EndUpdate();
    }

    ListBox1->ItemIndex = 0;
}

void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
    int index = ListBox1->ItemIndex;
    if (index != -1)
    {
        String File = Path + ListBox1->Items->Strings[ListBox1->ItemIndex]);
        //ShowMessage(File);
        Image1->Picture->LoadFromFile(File);
    }
    else
        Image1->Picture->Assign(NULL);
}

答案 1 :(得分:0)

应该是char ext[3] = {'jpg', 'gif', 'png'};而不是hubs = Hub.objects.annotate(num_users=Count('user')).order_by('num_users')