[C++] Lottozahlengenerator (GUI)
Ich habe einen Generator für Lottozahlen in C++ programmiert:
Es gibt folgende Elemente:
– Dynamisch generierte TLabels
– 2 Buttons
– 1 Scrollbar
– 1 Statusbar
– 1 Progressbar
Man kann auch die Transparenz des Fensters verändern. Das geschieht mit diesem Code:
void __fastcall TForm1::changetrans(TObject *Sender)
{
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle, 0, 255-ScrollBar1->Position , LWA_ALPHA);
}
Die Transparenz hängt also von der Position der Scrollbar ab!
Download
Alles komplett (Code, exe, Projektdatei, Form, usw.): lotto_komplett.zip
Der Code des Generators ist folgender:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "lotto.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TForm1 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
TLabel *mylabel[49];
TLabel *statuslabel;
int zahlen[6];
TProgressBar *progress;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Button1->Visible = false;
progress = new TProgressBar(this);
progress->Parent = StatusBar1;
progress->Height = 10;
progress->Left = 150;
progress->Top = 10;
progress->Max = 49;
statuslabel = new TLabel(this);
statuslabel->Parent = StatusBar1;
statuslabel->Left = 10;
statuslabel->Top = 10;
statuslabel->Caption = "Aufbauen des Feldes";
int topnow = 30;
int leftnow = 30;
for(int i = 0; i < 49; i++)
{
mylabel[i]= new TLabel(this);
mylabel[i]->Caption = IntToStr(i+1);
mylabel[i]->Visible = true;
mylabel[i]->Left = leftnow;
mylabel[i]->Top = topnow;
mylabel[i]->Name = "Label"+IntToStr(i);
mylabel[i]->Parent = Form1;
mylabel[i]->Width = 30;
mylabel[i]->Font->Size = 15;
mylabel[i]->Color = clGreen;
Sleep(5);
Application->ProcessMessages();
progress->Position = i+1;
if(leftnow > 200)
{
topnow = topnow + 30;
leftnow = 0;
}
leftnow += 30;
}
Button2->Visible = true;
statuslabel->Caption = "Fertig";
Sleep(1000);
progress->Position = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
for(int i = 0; i < 49; i++)
{
mylabel[i]->Color = clGreen;
}
int zahl_now = 0;
randomize();
progress->Max = 5;
for(int i = 0; i < 6; i++)
{
progress->Position = i;
Application->ProcessMessages();
statuslabel->Caption = "Generiere Zahl "+IntToStr(i+1);
Sleep(250);
zahl_now = random(49);
while(zahl_now == zahlen[1] ||
zahl_now == zahlen[2] ||
zahl_now == zahlen[3] ||
zahl_now == zahlen[4] ||
zahl_now == zahlen[5] ||
zahl_now == zahlen[6])
{
zahl_now = random(49);
}
zahlen[i] = zahl_now;
mylabel[zahl_now]->Color = clRed;
}
statuslabel->Caption = "Fertig";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::changetrans(TObject *Sender)
{
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle, 0, 255-ScrollBar1->Position , LWA_ALPHA);
}
//---------------------------------------------------------------------------
[…] [C++] Lottozahlengenerator (GUI) (38) […]