65.9K
CodeProject 正在变化。 阅读更多。
Home

如何用 C++ 创建一个 Windows Forms 笑话点唱机

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1投票)

2010年5月10日

CPOL

1分钟阅读

viewsIcon

23791

downloadIcon

149

逐步介绍如何在 C++ 中创建 Windows Forms 笑话点唱机应用程序。

引言

在 Microsoft Visual C++ 2005 Express Edition 中设计应用程序非常简单。指向这里,点击那里,搞定!然后将其放置在表单上。但说正经的,点击 Visual C++ 2005 Express Edition 左侧工具栏中所需的组件(控件),并在表单的适当位置绘制它。

表单上的组件对齐到矩形网格,使您的应用程序看起来对称。这个简单的应用程序演示了在 Windows Forms 中创建有趣应用程序的简易性。

列表框

快进一点,我们已经创建了一个 Windows 表单,并将列表框、标签、常用对话框放置在表单上。为了让所有这些控件响应鼠标点击,我们必须在其中添加一些代码。因此,我们双击任何给定的控件,并将一些代码放置在该控件的事件方法中。

listBox1_SelectedIndexChanged 事件

当鼠标左键按下时,且位于 listBox 之上时,会调用 listBox1_SelectedIndexChanged 事件。一旦索引发生变化,就会调用此方法,它表示用户从包含笑话的一个文本文件切换到另一个文本文件。

private: System::Void listBox1_SelectedIndexChanged(
         System::Object^  sender, System::EventArgs^  e)
{

    label8->Text="";
    label1->Text="Jokefile= "+fileList[listBox1->SelectedIndex] ;
    ProcessFile( fileList[listBox1->SelectedIndex] );
    jokeFileCount=listBox1->SelectedIndex;
    listBox2->SetSelected(0,true);
    updateInfoDisplay();
    this->Text ="Joke Jukebox by TopCoder  [" + 
                   fileList[listBox1->SelectedIndex]+"]";
}

Form1::GenerateRandomFiles() 方法

调用 Form1::GenerateRandomFiles() 方法,将笑话从文本文件加载到托管字符串数组中。

void Form1::GenerateRandomFiles()
{
    Random^ random = gcnew Random( Environment::TickCount );

    for (int j=0;j<FileCount;j++)
    {
        iFileList[j] =random->Next() % (FileCount) ;
        for(int k=0;k<j;k++)
            if (iFileList[j]==iFileList[k]) j--;
    }
}

listBox2_SelectedIndexChanged_1 事件

当鼠标左键按下时,且位于 listBox 之上时,会调用 listBox2_SelectedIndexChanged_1 事件。一旦索引发生变化,就会调用此方法,它表示用户从一个笑话行切换到另一个笑话行。

private: System::Void listBox2_SelectedIndexChanged_1(
         System::Object^  sender, System::EventArgs^  e)
{
    label8->Text=JokeList[listBox2->SelectedIndex];
    jokeLineCount=listBox2->SelectedIndex;
    updateInfoDisplay();
}

Form1::displayRandomJokeLines() 方法

调用 Form1::displayRandomJokeLines() 方法,将字符串数组中的一行笑话显示在标签上。

void Form1::displayRandomJokeLines()
{
    String^ sDebug;

    if (RanDom==1)
    {
        jokeLineCount=(int)iJokeList[doneLines];
        listBox2->SetSelected(jokeLineCount,true);
        label8->Text=JokeList[jokeLineCount];
        label10->Text="Unread Lines "+(LineCount-doneLines) +"  ";
        label11->Text="Unread Files "+(FileCount-doneFiles);

        doneLines++;
        if (doneLines>=LineCount)
        {
            doneLines=0;
            updateInfoDisplay();
            jokeFileCount=(int)iFileList[doneFiles];
            ProcessFile( fileList[jokeFileCount] );
            listBox1->SetSelected(jokeFileCount,true);
            label1->Text="Jokefile= "+fileList[jokeFileCount] ;
            listBox2->SetSelected(0,true);
            label11->Text="Unread Files "+(FileCount-doneFiles);

            GenerateRandomLines();

            doneFiles++;
            if (doneFiles>=FileCount)
            {
                initRandomMode();
            }
        }
    }
}

这就是在 Windows Forms 中创建有趣应用程序的简易程度。

感谢阅读。

© . All rights reserved.