Visual Basic Power Packs komponentalari va ularni ishlatish.
Bu komponentalar
guruhi alohida bo‘lib, ularni Microsoft Visual Basic Power Packs deb atashadi. Buni
yangi varintlarini ham internetdan olish va o‘rnatish mumkin.
Asosiy vazifasi formani
bezash uchun ishlatiladi
, shuning uchun barcha
komponentalarga bor bo‘lgan hodisalar mavjud.
Bu Microsoft.VisualBasic PowerPacks nomlar fazosida joylashgan bo‘lib, Visual
Basic Power Packs elementlar uchun sinflari mavjud. Visual Basic Power to‘plamlar
elementlari qo‘shimcha Windows formalari elementlari uchun mo‘ljallangan. Ular
dastlab bepul plagin-smaylik sifatida ishlatilgan va endi Visual Studio tarkibiga
kiritilgan.
12.15-rasm. Visual Basic Power Packs komponentalari.
12.3-jadval. Visual Basic Power Packs komponentalari
Rasmi
Komponenta nomi
vazifasi
PrintForm
Formani chop qilishga ruxsat berishni
boshqaradi
LineShape
Gorozantal, vertikal, diognal chiziqlarni
boshqarishni ta’minlaydi
OvalShape
Oval ko‘rinishdagi ob’ektlarni
boshqarishni ta’minlaydi
RectangleShape
To‘rtburchak ko‘rinishdagi ob’ektlarni
boshqarishni ta’minlaydi
DataRepeater
Talab asosida formatlangan
ma’lumotlarni ko‘rsatadi
OvalShape komponentasining ba’zi xususiyatlari va hodisalari
1.
BackColor – komponentaning orqa rangini o‘rnatadi va qiymatlari ranglar
to‘plami.
2.
BackStyle - komponentaning orqa stilini o‘rnatadi va qiymatlari 2 ta statik
berilgan.
3.
BorderColor - komponentaning chegara rangini o‘rnatadi va qiymatlari ranglar
to‘plami.
4.
BorderStyle - komponentaning chegara stilini o‘rnatadi va qiymatlari 6 ta statik
berilgan.
5.
BorderWight - komponentaning chegara rang qalinligini o‘rnatadi va qiymatlari
sonlar.
6.
FillColor - komponentaning aktiv bo‘lgandagi rangni o‘rnatadi va qiymatlari
ranglar.
7.
Location – komponentaning joylashuv o‘rnini o‘rnatish..
8.
SelectionColor - komponentaning tanlangan bo‘lgandagi rangni o‘rnatadi va
qiymatlari ranglar.
9.
Bir loyiha yarating va unda 12.16-rasm kabi formani tayyorlang.
12.16-rasm. Loyiha ko‘rinishi.
YAratilgan ovalShape1 ni forma bo‘yicha xarakatlanishni masalasini ko‘ramiz.
Sizning formadagi 2 ob’ektni yaratishda ularning xususiyatlari bilan tanishib
olgansiz.
Buning uchun loyihaga bir Timer1 ob’ektidan joylashtiramiz. Tez bajarilishi
uchun uning interval xususiyatiga 1 qiymatni kiritamiz. Timer1 ob’ektining
ustiga sichqonchani ikki marta bosib, timer1_Tick hodisasiga quyidagicha
algoritm kiritamiz.
if(ovalpos <= (this->Size.Width - ovalShape1->Size.Width)){
ovalpos += 5;
ovalShape1->Location = Point(ovalpos,ovalShape1->Location.Y);
}
}
if(ovalpos <= (this->Size.Width - ovalShape1->Size.Width)){
ovalpos += 5;
ovalShape1->Location = Point(ovalpos,ovalShape1->Location.Y);
}
}
Bunda ovalpos qiymati 0ga teng bo‘lgan butun son tipidagi o‘zgaruvchi. Location –
bu ob’ektning joylashinini belgilaydi va juft qiymatga ega Point qiymatni qabul
qiladi.
SHuningdek, Form1_Load hodisasida quyidagi algoritmni yozamiz.
ovalpos = 0;
timer1->Start();
Agar dasturni ishga tushirsangiz ovalShape1 ob’ektining harakatini ko‘rasiz.
SHakllarning FillColor, FillGradientColor, FillGradientStyle,
FillStyle
xususiyatlaridan foydalanib
, turli xil tugmalarni ham yaratish mumkin.
Berilgan button tugmani bezash masalasini qaraymiz. Buning uchun formaga bitta
tugma joylashtiramiz va uning xususiyatlarini quyidagicha aniqlaymiz.
using namespace System::Drawing::Drawing2D;
using namespace System::Drawing::Text;
// ...
this->button1->Location = System::Drawing::Point(115, 438);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(121, 49);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
Tugmaning button1_Paint hodisasiga o‘tib, quyidagi algoritimni yozamiz.
Pen^ pen = gcnew Pen(Color::Blue,10);
Brush^ brush = gcnew SolidBrush(Color::FromKnownColor
(KnownColor::Control));
SolidBrush ^ brushinside = gcnew SolidBrush(Color:: Aquamarine);
Graphics ^gr = e->Graphics;
gr->FillRectangle(brush, 0, 0, button1->Width,button1->Height);
gr->FillEllipse(brushinside, 0, 0, button1->Width, button1->Height);
gr->DrawEllipse(pen, 0, 0, button1->Width, button1->Height);
GraphicsPath^ path = gcnew GraphicsPath();
button1->Region = gcnew System::Drawing::Region(path);
String^ text = "Boshlash";
System::Drawing::Font^ drawfont = gcnew System::Drawing::Font("Arial", 14);
SolidBrush^ exbrush = gcnew SolidBrush(Color::Black);
RectangleF rect = RectangleF(button1->Width/2-40,button1->Height/2-10,button1-
>Width, button1->Height);
gr->DrawString(text, drawfont, exbrush,rect);
Pen^ pen = gcnew Pen(Color::Blue,10);
Brush^ brush = gcnew SolidBrush(Color::FromKnownColor
(KnownColor::Control));
SolidBrush ^ brushinside = gcnew SolidBrush(Color:: Aquamarine);
Graphics ^gr = e->Graphics;
gr->FillRectangle(brush, 0, 0, button1->Width,button1->Height);
gr->FillEllipse(brushinside, 0, 0, button1->Width, button1->Height);
gr->DrawEllipse(pen, 0, 0, button1->Width, button1->Height);
GraphicsPath^ path = gcnew GraphicsPath();
button1->Region = gcnew System::Drawing::Region(path);
String^ text = "Boshlash";
System::Drawing::Font^ drawfont = gcnew System::Drawing::Font("Arial", 14);
SolidBrush^ exbrush = gcnew SolidBrush(Color::Black);
RectangleF rect = RectangleF(button1->Width/2-40,button1->Height/2-10,button1-
>Width, button1->Height);
gr->DrawString(text, drawfont, exbrush,rect);
Bunda chizish uchun qalam - Pen , mo‘yqalam - Brush, to‘liq bo‘yash uchun
mo‘yqalam - SolidBrush yaratib olamiz. Kerali shakllarni chizish ob’ektinini
tugmaning o‘lchamiga moslab tayyorlaymiz. Tugmaning ustiga matn joylagtirish
uchun yana bir ob’ekt yaratamiz, unga matn, matn rangi, formati va egallash
sohasini ko‘rsatib, DrawString bilan tugmaning ustiga chizamiz.
12.17-rasm. Tugmani bezashga misol.
YAngi yaratilgan tugmachalar orqali shakllarni oqim orqali boshqarish masalasini
qaraymiz.
Buning uchun Loyiha formasiga
, 1 ta PictureBox, 2 ta button tugmani
yuqoridagidek qilib joylashtiramiz. PictureBox ning BackColor xususiyatiga oq
rangni o‘rnatimiz. SHaklning xarakatlantirish uchun uning tayanch nuchtasini
ko‘chirib, oldingisi oq bilan bo‘yaladi va keyingisi hosil qilinadi, shunday qilib
iteratsiya davom etaveradi.
Ikkita berilgan tugmalarni yuqoridagidek qilib, moslab olamiz.
Oqimni boshqarish uchun avval bir funksiya yaratib olamiz. Bu funksiya chizilgan
shaklni PictureBox bo‘ylab harakatlanish va oldin keltirilgan iteratsiyani amalga
oshirish uchun kerak. Funksiyaning algoritmi quyidagicha: public: void Go(){
int R = 30;
// button2->BeginInvoke(gcnew InvokeDelegateSetEnabled(this,
&Form1::setEnabledToButton), false);
Bitmap ^ image = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height);
Graphics^ g = Graphics::FromImage(image);
for (int x = 0; x <= pictureBox1->Width - 2 * R; x += 2){
g->Clear(Color::White);
g->FillEllipse(Brushes::Aqua, x, 0, 2 * R, 2 * R);
pictureBox1->BeginInvoke(gcnew InvokeDelegateSetImage(this,
&Form1::setImageToPictureBox), image);
Thread::Sleep(10);
}
for (int y = 0; y <= pictureBox1->Height - 2 * R; y += 2){
g->Clear(Color::White);
g->FillEllipse(Brushes::Aqua, pictureBox1->Width - 2 * R, y, 2 * R, 2 * R);
pictureBox1->BeginInvoke(gcnew InvokeDelegateSetImage(this,
&Form1::setImageToPictureBox), image);
Thread::Sleep(10);
}
for (int x = pictureBox1->Width - 2 * R; x >= 0; x -= 2){
public: void Go(){
int R = 30;
// button2->BeginInvoke(gcnew InvokeDelegateSetEnabled(this,
&Form1::setEnabledToButton), false);
Bitmap ^ image = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height);
Graphics^ g = Graphics::FromImage(image);
for (int x = 0; x <= pictureBox1->Width - 2 * R; x += 2){
g->Clear(Color::White);
g->FillEllipse(Brushes::Aqua, x, 0, 2 * R, 2 * R);
pictureBox1->BeginInvoke(gcnew InvokeDelegateSetImage(this,
&Form1::setImageToPictureBox), image);
Thread::Sleep(10);
}
for (int y = 0; y <= pictureBox1->Height - 2 * R; y += 2){
g->Clear(Color::White);
g->FillEllipse(Brushes::Aqua, pictureBox1->Width - 2 * R, y, 2 * R, 2 * R);
pictureBox1->BeginInvoke(gcnew InvokeDelegateSetImage(this,
&Form1::setImageToPictureBox), image);
Thread::Sleep(10);
}
for (int x = pictureBox1->Width - 2 * R; x >= 0; x -= 2){
g->Clear(Color::White);
g->FillEllipse(Brushes::Aqua, x, pictureBox1->Height - 2 * R, 2 * R, 2 * R);
pictureBox1->BeginInvoke(gcnew InvokeDelegateSetImage(this,
&Form1::setImageToPictureBox), image);
Thread::Sleep(10);
}
for (int y = pictureBox1->Height - 2 * R; y >= 0; y -= 2) {
g->Clear(Color::White);
g->FillEllipse(Brushes::Aqua, 0, y, 2 * R, 2 * R);
pictureBox1->BeginInvoke(gcnew InvokeDelegateSetImage(this,
&Form1::setImageToPictureBox), image);
Thread::Sleep(10);
}
// button2->BeginInvoke(gcnew InvokeDelegateSetEnabled(this,
&Form1::setEnabledToButton), true);
}
Bu funksiyani ishlatish uchun qiymatni to‘g‘ridan to‘g‘ri berib bo‘lmaydi shuning
uchun bir deligant (funksiya ko‘rsatkich saqlovchi) va qiymatni funksiya yaratamiz.
delegate void InvokeDelegateSetImage(Bitmap^ image);
public: void setImageToPictureBox(Bitmap^ image){
pictureBox1->Image = image;
}
Formaning ochiq joyiga oqim yaratib olamiz.
using namespace System::Threading;
// ...
private: Thread^ myThread;
private: Thread^ myThreadOne;
Birichi tugma bosilganda mazkur oqimlarni ishga tushurish uchun button1_Click
hodisasiga quyidagicha algoritm yoziladi.
myThread = gcnew Thread(gcnew ThreadStart(this,&Form1::Go));
myThread->Start();
myThreadOne = gcnew Thread(gcnew ThreadStart(this,&Form1::GoOne));
myThreadOne->Start();
myThread = gcnew Thread(gcnew ThreadStart(this,&Form1::Go));
myThread->Start();
myThreadOne = gcnew Thread(gcnew ThreadStart(this,&Form1::GoOne));
myThreadOne->Start();
Oqimni saqlab tuurish uchun yaratilgan funksiyada Thread::Sleep(10); dan
foydalanilgan.
Oqim
ishga tushgan paytdan ishlaydi
, ammo forma yopilganda uning ishini to‘xtatish
lozim.SHuning uchun oqimni to‘xtashish uchun button2_Click va
Form1_FormClosing hodisalariga quyidagi algoritmni kiritamiz.
try{
myThread->Abort();
myThreadOne->Abort();
}
catch(...){}
Loyihani ishga tushirsangiz, quyidagi 12.18- rasm hosil bo‘ladi.
12.18- rasm. Oqimlar asosida xarakatni boshqarish
Dasturlash boshlash tugmasini bosganda ikkita paralel bo‘lgan shakllarining
harakatini ko‘rasiz. To‘xtash bosilganda ularni to‘xtashini ko‘rasiz. Xarakat davom
etatgan vaqtda ham formani yopiq ishni tugallash mumkin.
Umuman olgada grafika bilan ishlash uchun geometriyani yaxshi bilash talab qilinadi.
Visual S++ da grafika sinfi, uning usullari va turli shakllarni chizish usullari, Chart va
Shape sinflarining xususiyatlari va usullarini kщrib chiыdik. Bularni щziga xos
ma’lumotlarda ishlatishni tavsiya qilamiz.
Visual C++ muhitida ko‘p oynali muhitni yaratish.
Visual C++ muhitida ko‘p
oynali dasturlarni yaratish uchun SDI va MDI ilovalar konstruktorlari mavjud.
Hozirda MDI ilova konstruktori orqali kichik ko‘p oynali dastur
Dostları ilə paylaş: |