![پازل فارسی با سی شارپ](http://img.ehsanavr.com/persian-puzzle.jpg)
برنامه ای که مشاهده می کنید یک پازل حرفه ای است که سال ۸۹ از روی بیکاری طراحی کردم اما نکات آموزنده ی زیادی در سورس آن یافت می شود.
این نرم افزار تعداد ۱۶ عکس در Resource خود دارد اما مهمترین قسمت آن قابلیت وارد کردن عکس دلخواه با هر اندازه ای و پشتیبانی از تمام پسوندها می باشد.
تصویری از تنظیمات برنامه:
![تنظیمات پازل فارسی](http://img.ehsanavr.com/persian-puzzle-setting.jpg)
قسمت مهم سورس برنامه را می توانید در اینجا مشاهده کنید:
// programmed by EhsanAvr
// visit : http://ehsanavr.com
// Free Pgoramming Center
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Puzzle
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
Bitmap main_image = new Bitmap(Puzzle.Properties.Resources.image1);
List<PictureBox> buttons = new List<PictureBox>();
Bitmap[] all_images = new Bitmap[25];
Bitmap[] tmp_image = new Bitmap[25];
bool tpm_image_t;
PictureBox oldnam = null;
int imageindex = 1;
int chi = 0;
int sahih = 0;
int[] t = new int[6];
private Bitmap ResizeImage(Bitmap BMP, int Width, int Height)
{
Bitmap Thump = new Bitmap(Width, Height);
Graphics Graphic;
Graphic = Graphics.FromImage(Thump);
Graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Graphic.DrawImage(BMP, new Rectangle(0, 0, Width, Height), new Rectangle(0, 0, BMP.Width, BMP.Height), GraphicsUnit.Pixel);
Graphic.Dispose();
return Thump;
}
private Bitmap SplitImage(int x, int y, int w, int h)
{
Bitmap p1b = new Bitmap(main_image.Clone(new Rectangle(new Point(x, y), new System.Drawing.Size(w, h)), System.Drawing.Imaging.PixelFormat.Undefined));
return p1b;
}
private void BuildPictureBox(int x, int y,int size)
{
for (int ii = 0; ii < x; ii++)
{
for (int i = 0; i < y; i++)
{
PictureBox btn = new PictureBox();
btn.Size = new Size(size, size);
btn.Location = new Point(2, i * size);
btn.Left = ii * size;
btn.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
buttons.Add(btn);
}
}
MainPanel.Controls.AddRange(buttons.ToArray());
}
private void New_Game()
{
string ipath = Puzzle.Properties.Settings.Default.imagepath.ToString();
if (ipath != "" && File.Exists(ipath) == true && Puzzle.Properties.Settings.Default.imagetype==1)
{
try
{
Bitmap image_orginal = (Bitmap)Image.FromFile(ipath);
main_image = ResizeImage(image_orginal, 500, 500);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Puzzle.Properties.Settings.Default.imagepath = "";
}
}
else
{
switch (Puzzle.Properties.Settings.Default.imageindex)
{
case 0: main_image = new Bitmap(Puzzle.Properties.Resources.image1);
break;
case 1: main_image = new Bitmap(Puzzle.Properties.Resources.image2);
break;
case 2: main_image = new Bitmap(Puzzle.Properties.Resources.image3);
break;
case 3: main_image = new Bitmap(Puzzle.Properties.Resources.image4);
break;
case 4: main_image = new Bitmap(Puzzle.Properties.Resources.image5);
break;
case 5: main_image = new Bitmap(Puzzle.Properties.Resources.image6);
break;
case 6: main_image = new Bitmap(Puzzle.Properties.Resources.image7);
break;
case 7: main_image = new Bitmap(Puzzle.Properties.Resources.image8);
break;
case 8: main_image = new Bitmap(Puzzle.Properties.Resources.image9);
break;
case 9: main_image = new Bitmap(Puzzle.Properties.Resources.image10);
break;
case 10: main_image = new Bitmap(Puzzle.Properties.Resources.image11);
break;
case 11: main_image = new Bitmap(Puzzle.Properties.Resources.image12);
break;
case 12: main_image = new Bitmap(Puzzle.Properties.Resources.image13);
break;
case 13: main_image = new Bitmap(Puzzle.Properties.Resources.image14);
break;
case 14: main_image = new Bitmap(Puzzle.Properties.Resources.image15);
break;
case 15: main_image = new Bitmap(Puzzle.Properties.Resources.image16);
break;
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int a = 0; a < 5; a++)
{
for (int b = 0; b < 5; b++)
{
all_images[(a * 5) + b] = SplitImage(a * 100, b * 100, 100, 100);
}
}
// randome image
Random r = new Random();
int[] ir = new int[25];
int indexx = 0;
int rm;
int tek;
while (indexx != 24)
{
rm = r.Next(0, 25);
tek = 0;
for (int d = 0; d < 25; d++)
{
if (ir[d] == rm) tek++;
}
if (tek == 0)
{
ir[indexx] = rm;
indexx++;
}
}
for (int c = 0; c < 25; c++)
{
buttons[c].BackgroundImage = all_images[ir[c]];
}
for (int i = 0; i < 5; i++)
{
t[i] = 0;
}
clock_timer.Enabled = true;
timer_progress.Value = 0;
}
private void ReadSetting()
{
show_btn.Visible = Puzzle.Properties.Settings.Default.enable_view_image;
help_btn.Visible = Puzzle.Properties.Settings.Default.enable_true_count;
time_lbl.Visible = Puzzle.Properties.Settings.Default.enable_clock;
timer_progress.Visible = Puzzle.Properties.Settings.Default.enable_timer;
clock_timer.Enabled = Puzzle.Properties.Settings.Default.enable_clock;
}
private void MainForm_Load(object sender, EventArgs e)
{
ReadSetting();
BuildPictureBox(5, 5, 102);
New_Game();
}
private void MainPanel_Paint(object sender, PaintEventArgs e)
{
}
private void MainPanel_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.MouseDown += new MouseEventHandler(Control_MouseDown);
}
void Control_MouseDown(object sender, MouseEventArgs e)
{
PictureBox nam = (PictureBox)sender;
if (e.Button == MouseButtons.Left)
{
if (oldnam != null)
{
oldnam.Image = null;
}
if (imageindex == 2)
{
Bitmap ob = (Bitmap)oldnam.BackgroundImage;
oldnam.BackgroundImage = nam.BackgroundImage;
nam.BackgroundImage = ob;
imageindex = 0;
}
nam.Image = Puzzle.Properties.Resources.RedBorder;
oldnam = nam;
imageindex++;
}
if (e.Button == MouseButtons.Right)
{
oldnam.Image = null;
}
// check images
for (int i = 0; i < 25; i++)
{
if (buttons[i].BackgroundImage == all_images[i]) chi++;
}
sahih = chi;
if (chi == 25)
{
if (Puzzle.Properties.Settings.Default.enable_clock == false)
{
MessageBox.Show("!شما بازی را با موفقیت به پایان رساندید", "پایان بازی", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string gametime = t[5].ToString() + t[4].ToString() + ":" + t[3].ToString() + t[2].ToString() + ":" + t[1].ToString() + t[0].ToString();
MessageBox.Show("!شما بازی را با موفقیت به پایان رساندید"+"\n"+gametime+":زمان بازی شما", "پایان بازی", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
//this.Text = chi.ToString();
chi = 0;
}
}
private void NewGame_Menu_Click(object sender, EventArgs e)
{
New_Game();
}
private void Open_Image_Menu_Click(object sender, EventArgs e)
{
clock_timer.Enabled = false;
OpenFileDialog op = new OpenFileDialog();
op.Filter = "All Files|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
Puzzle.Properties.Settings.Default.imagepath = op.FileName;
Puzzle.Properties.Settings.Default.imagetype = 1;
Puzzle.Properties.Settings.Default.Save();
New_Game();
}
clock_timer.Enabled = true;
}
private void Exit_Menu_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Help_Menu_Click(object sender, EventArgs e)
{
HelpForm h = new HelpForm();
h.ShowDialog();
}
private void Setting_Menu_Click(object sender, EventArgs e)
{
clock_timer.Enabled = false;
SettingForm s = new SettingForm();
s.ShowDialog();
ReadSetting();
clock_timer.Enabled = true;
}
private void About_Menu_Click(object sender, EventArgs e)
{
AboutForm a = new AboutForm();
a.ShowDialog();
}
private void Web_Menu_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("http://ehsanavr.com/");
}
catch
{
System.Diagnostics.Process.Start("iexplore.exe","http://ehsanavr.com/");
}
}
private void clock_timer_Tick(object sender, EventArgs e)
{
t[0]++;
if (t[0] == 10)
{
t[1]++;
t[0] = 0;
}
if (t[1] == 6)
{
t[2]++;
t[1] = 0;
}
if (t[2] == 10)
{
t[3]++;
t[2] = 0;
}
if (t[3] == 6)
{
t[4]++;
t[3] = 0;
}
if (t[4] == 10)
{
t[5]++;
t[4] = 0;
}
if (t[5] == 10)
{
t[5] = 9;
clock_timer.Enabled = false;
}
time_lbl.Text = t[5].ToString() + t[4].ToString() + ":" + t[3].ToString() + t[2].ToString() + ":" + t[1].ToString() + t[0].ToString();
if (Puzzle.Properties.Settings.Default.enable_timer == true)
{
long n = Puzzle.Properties.Settings.Default.timer_value;
long v=t[0] + (t[1] * 10) + (t[2] * 100) + (t[3] * 1000) + (t[4] * 10000) + (t[5] * 100000);
int nv = int.Parse(((v * 100) / n).ToString());
if (nv <= 100) timer_progress.Value = nv;
if (v == n-1)
{
clock_timer.Enabled = false;
MessageBox.Show("وقت شما به پایان رسیده است\nتعداد "+sahih.ToString()+" عکس از ۲۵ عکس سرجای خود قرار گرفتند", "پایان بازی", MessageBoxButtons.OK, MessageBoxIcon.Stop);
New_Game();
}
}
}
private void show_btn_MouseUp(object sender, MouseEventArgs e)
{
for (int i = 0; i < 25; i++)
{
buttons[i].BackgroundImage = tmp_image[i];
}
tpm_image_t = false;
}
private void show_btn_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < 25; i++)
{
tmp_image[i] = (Bitmap)buttons[i].BackgroundImage;
buttons[i].BackgroundImage = all_images[i];
}
tpm_image_t = true;
}
private void show_btn_MouseLeave(object sender, EventArgs e)
{
if (tpm_image_t == true)
{
for (int i = 0; i < 25; i++)
{
buttons[i].BackgroundImage = tmp_image[i];
}
}
}
private void show_btn_ButtonClick(object sender, EventArgs e)
{
}
private void help_btn_MouseDown(object sender, MouseEventArgs e)
{
sahihtxt.Visible = true;
sahihtxt.Text = "تعداد " + sahih.ToString() + " عکس از ۲۵ عکس درست سرجای خود قرار گرفته اند.";
}
private void help_btn_MouseLeave(object sender, EventArgs e)
{
sahihtxt.Visible = false;
}
private void help_btn_MouseUp(object sender, MouseEventArgs e)
{
sahihtxt.Visible = false;
}
}
}
استفاده از این سورس در هر شرایطی آزاد است است اما مدیران سایتهایی که مطالب این سایت که با زحمت زیاد تهیه شده را به سادگی با نام خودشان منتشر می کنند سعی کنند لااقل کپی رایت را رعایت کنند.
(تعداد دانلود: 363)