မိတ္ေဆြ၊ ညီအစ္ကို၊ ေမာင္ႏွမအားလံုးပဲ မဂၤလာပါဗ်ာ။ က်ေနာ္တို႔ C# Graphics ေလ့က်င့္ခန္းေလးကို ဆက္ေလ့လာၾကည့္လိုက္ၾကရေအာင္။ အရင္အပတ္ေလ့က်င့္ခန္းမွာေတာ့ က်ေနာ္တို႔ Coordinate System နဲ႔ ပတ္သတ္ၿပီး World, Page, Device Coordinate System အေၾကာင္းေလးေတြကို ေလ့လာခဲ့ၿပီျဖစ္ပါတရ္ဗ်ာ။ Default Unit အေနနဲ႔ေတာ့ Coordinate System သံုးခုစလံုးဟာ Pixel နဲ႔ တိုင္းတာပါတရ္ဗ်ာ။ ဒီေတာ့ Graphic ရဲ႕ အတိုင္းအတာနဲ႔ ပတ္သတ္ၿပီး က်ေနာ္တို႔အေနနဲ႔ ေနာက္တစ္ခု မျဖစ္မေန သိထားရမွာက PageUnit ပါ။ ဒီေန႔ ေလ့က်င့္ခန္းေလး မွာေတာ့ က်ေနာ္တို႔ ၄င္း PageUnit အေၾကာင္းေလးကို ေလ့လာၾကည့္ၾကမွာ ျဖစ္ပါတရ္ဗ်ာ။ Graphic တစ္ခုကို Point ေတြခ်ရာမွာ PageUnit ဟာ အေရးႀကီးတဲ့ အခန္းက႑က ပါ၀င္ပါတရ္။ မိမိသံုးထားတဲ့ Graphics ရဲ႕ အတိုင္းအတာ Unit ကို သိမွသာ Point ေတြကို မွန္မွန္ကန္ကန္ ခ်ႏိုင္မွာ မဟုတ္ပါလားဗ်ာ။ Page Unit အေနနဲ႔ Visual C# မွာ Display, Document, Point, Pixel, World, Inch and Millimeter ဆိုၿပီးရွိပါတရ္။ ပံုဆြဲတဲ့အခါမွာ မိမိစိတ္ႀကိဳက္ အတိုင္းအတာ Unit ကို သံုးႏိုင္ပါတရ္ဗ်ာ။ Default Graphics Unit ကေတာ့ Point ပဲျဖစ္ပါတရ္။ Unit Conversion အေနနဲ႔ေတာ့ အနည္းငယ္ တီးမိေခါက္မိ သြားေအာင္ Inch to Other Conversion ကို ျပလိုက္ပါ့မရ္။ ေအာက္မွာ ေလ့လာၾကည့္လိုက္ပါအံုး။
GraphicsUnit Conversion (1 unit)
Form To
1 Inch --> 25.4 millimeter
72 Point
96 Pixel
100 Display
300 Document
က်န္တာေလးေတြကိုေတာ့ မိတ္ေဆြတို႔ နားလည္သြားေအာင္ ကိုယ္တိုင္ ေလ့လာၾကည့္ေစခ်င္ပါတရ္။
ကဲ… က်ေနာ္တို႔ အတိုင္းအတာေလးေတြကို သိၿပီးဆိုရင္ေတာ့ ဥပမာေလးတစ္ပုဒ္ေလာက္ ေရးၿပီး ေလ့လာ ၾကည့္ၾကရေအာင္ဗ်ာ။ ဥပမာ အက်ဥ္းခ်ဳပ္ေလးကေတာ့ Visual C# က ခြင့္ျပဳထားတဲ့ GraphicsUnit အမ်ိဳးအစား(၇)ခုနဲ႔ Rectangle Graphic တစ္ခုဆီကို ဆြဲၾကည့္ၾကမွာျဖစ္ပါတရ္။ GraphicsUnit တစ္ခုနဲ႔ တစ္ခုရဲ႕ ကြာျခားတဲ့ အခ်ိဳးနဲ႔ ေျပာင္းလဲသြားပံုကို ေလ့လာႏိုင္ပါတရ္ဗ်ာ။
အဆင့္(၁)။ Drawing အတြက္ using System.Drawing.Drawing2D; ကိုေၾကျငာပါ။
အဆင့္(၂)။ က်ေနာ္တို႔ Form တြင္ Panel Control ႏွစ္ခုသံုးထားၿပီး Properties ေတြကိုေတာ့ ေအာက္ပါ Table အတိုင္း ျပင္ယူလိုက္ပါဗ်ာ။
Control Name Backgroud Color Panel1 pnCreateBtnGraphicsUnit ControlLight Panel2 pnViewGraphicsUnit Silverဒီေလ့က်င့္ခန္းမွာေတာ့ က်ေနာ္တို႔ အလုပ္ လုပ္ပံုကို အပိုင္းႏွစ္ပိုင္းခြဲၿပီး ေလ့လာႏိုင္ပါတရ္ဗ်ာ။ ပထမ အပိုင္းက GraphicsUnit ေတြကို Button အျဖစ္ Display ဆြဲယူတဲ့အပိုင္းျဖစ္ၿပီး ဒုတိယအပိုင္းကေတာ့ ၄င္း Button ကို Click ေပးလိုက္တာနဲ႔ ၄င္း Unit အတိုင္းအတာနဲ႔ Graphics ဆြဲယူတဲ့ အပိုင္း ျဖစ္ပါတရ္ဗ်ာ။
အဆင့္(၃)။ GraphicsUnit ေတြကို Button အျဖစ္ Display ဆြဲယူျခင္း။
GraphicsUnit ဟာ Enum Type ျဖစ္ပါတရ္ဗ်ာ။ ဒါေၾကာင့္ က်ေနာ္တို႔ loop တစ္ခုခုကို သံုးၿပီး Enum ထဲက Data ေတြကို ဆြဲယူႏိုင္ပါတရ္ဗ်ာ။ က်ေနာ္ကေတာ့ ဒီေလ့က်င့္ခန္းမွာ Foreach( ) loop ကို သံုးထားပါတရ္ဗ်ာ။
foreach (GraphicsUnit item in Enum.GetValues(typeof(GraphicsUnit)))
ဒီေတာ့ item ထဲကို GraphicsUnit ျဖစ္တဲ့ Display, Document, Point, Pixel, World, Inch and Millimeter စတာတို႔ ေရာက္လာပါၿပီဗ်ာ။ မိမိႏွစ္သတ္ရာ Tool ေပၚမွာ Display ျပႏိုင္ပါတရ္။ က်ေနာ္ကေတာ့ Custom Control အပိုင္းကို နားလည္သြားေစခ်င္တဲ့အတြက္ Button Object တစ္ခုကို ကိုယ္တိုင္ ဖန္တီးၿပီး Item ထဲေရာက္လာတဲ့ GraphicsUnit ေတြကို ၄င္း Button ေတြေပၚသို႔ ဆြဲတင္ထားပါတရ္ဗ်ာ။
Button btn = new Button(); btn.Text = item.ToString();
နားလည္ၾကမရ္ ထင္ပါတရ္ဗ်ာ။ Coding အျပည့္အစံုေလးကေတာ့….
private void Form1_Load(object sender, EventArgs e)
{
//Create FlowLayoutPanel for Display fixContorl
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Dock = DockStyle.Fill;
flp.AutoScroll = true;
//Get GraphicsUnit Enum List in PanelBox
foreach (GraphicsUnit item in Enum.GetValues(typeof(GraphicsUnit)))
{
//Create Button Object and its properties
Button btn = new Button();
btn.Size = new Size(175, 35);
btn.Text = item.ToString();
btn.Font = new Font("Courier New", 9, FontStyle.Italic);
Create inside Method for Button Click
btn.Click += new EventHandler(btn_Click);
flp.Controls.Add(btn);
}
pnCreateBtnGraphicsUnit.Controls.Add(flp);
}
ရလာတဲ့ Result ေလးကေတာ့…
အဆင့္(၄)။ Button Click ေပးၿပီး GraphicsUnit နဲ႔ Graphics ဆြဲယူျခင္း
ဒီအပိုင္းေလးမွာေတာ့ Button Click က ေပးပို႔လာတဲ့ Sender ကို String အျဖစ္ဖမ္းယူျခင္း၊ ၄င္း String ကို GraphisUnit Type အျဖစ္သို႔ ေျပာင္းယူျခင္းနဲ႔ ၄င္း GraphicsUnit ျဖစ္ Graphic ဆြဲယူျခင္းဆိုၿပီး အဆင့္ သံုးဆင့္နဲ႔ အလုပ္ လုပ္ခိုင္းထားပါတရ္ဗ်ာ။ Button Click ရဲ႕ Sender ကိုေတာ့ ေအာက္ပါ Coding ေလးနဲ႔ ဖမ္းယူထားပါတရ္ဗ်ာ။
Button btnSender = (Button)sender;
string getGraphicsUnit = btnSender.Text;
String ကို GraphisUnit Type အျဖစ္ ေျပာင္းလဲခ်င္တရ္ဆိုရင္ေတာ့…
GraphicsUnit GU = (GraphicsUnit)Enum.Parse(typeof(GraphicsUnit), getGraphicsUnit, true); gr.PageUnit = GU;
အခုလိုေလး ေျပာင္းလဲ ယူႏိုင္ပါတရ္ဗ်ာ။ Graphics ဆြဲယူတဲ့အပိုင္းကေတာ့ ရိုးရွင္းပါတရ္ဗ်ာ။ ဒီေတာ့ မရွင္းျပေတာ့ပါဘူးေနာ္။ Button Click ေပးလိုက္တဲ့အခါမွာ ခိုင္းေစထားတဲ့ Coding အျပည့္အစံုေလး ကိုေတာ့ ေအာက္မွာ ေလ့လာၾကည့္လိုက္ပါဗ်ာ။
void btn_Click(object sender, EventArgs e)
{
try
{
//Create Graphics Object with SmmothingMode
Graphics gr = pnViewGraphicsUnit.CreateGraphics();
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.Clear(Color.Silver);
//Catch sender by User Clicked Button's information
Button btnSender = (Button)sender;
string getGraphicsUnit = btnSender.Text;
//Change String to GraphicsUnit and Assign PageUnit
GraphicsUnit GU = (GraphicsUnit)Enum.Parse(typeof(GraphicsUnit), getGraphicsUnit, true);
gr.PageUnit = GU;
//Draw Rectangle with Specified PageUnit
using (Pen p = new Pen(Color.Blue, 5))
{
gr.DrawRectangle(p, 5, 5, 50, 50);
}
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
အခုေလာက္ဆိုရင္ေတာ့ မိတ္ေဆြတို႔ အေနနဲ႔ PageUnit အေၾကာင္းကို အနည္းငယ္ တီးမိေခါက္မိၿပီးလို႔ ထင္ပါတရ္ဗ်ာ။
မိတ္ေဆြအားလံုး ေလ့လာျခင္းျဖင့္ ေက်နပ္ႏိုင္ၾကပါေစဗ်ာ။
Labels:
Graphics Programming

0 Responses so far.
Post a Comment