السلام عليكم ورحمة الله وبركاتة
اقدم لكم كلاس SplashScreen يقوم بعمل شاشة افتتاحية للبرنامج.
نتعلم في هذا الكلاس عدة امور منها:
- كيفية استخدام دوال API
- كيفية انشاء struct وتعاملها مع دوال API
- كيفية التعامل مع الكلمة المفتاحية static
- كيفية استخدام المكتبة System.Drawing
- كيفية عمل ظهور "FadeIn" وتلاشي "FadeOut" بطريقة جديدة
- كيفية استخدام Command Line مع البرنامج والتحكم بها
واترككم مع الشيفرة
// SplashScreen.cs
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class SplashScreen
{
#region ' Win32 API '
[StructLayout(LayoutKind.Sequential)]
class BlendFunction
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
public static BlendFunction Initialize(byte alpha)
{
const byte AC_SRC_OVER = 0x00;
const byte AC_SRC_ALPHA = 0x01;
BlendFunction blendFun = new BlendFunction();
blendFun.BlendOp = AC_SRC_OVER;
blendFun.BlendFlags = 0; // Must be zero.
blendFun.SourceConstantAlpha = alpha;
blendFun.AlphaFormat = AC_SRC_ALPHA;
return blendFun;
}
}
enum ULW : uint
{
ColorKey = 1,
Alpha = 2,
Opaque = 4
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern Boolean UpdateLayeredWindow(
IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, BlendFunction pblend, ULW dwFlags);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern bool DeleteObject(IntPtr hObject);
#endregion
#region ' SplashForm '
private class SplashForm : Form
{ public SplashForm()
{
base.ShowInTaskbar = false;
base.FormBorderStyle = FormBorderStyle.None;
base.StartPosition = FormStartPosition.CenterScreen;
}
protected override CreateParams CreateParams
{ get
{ CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x80000;
return cp;
}
}
}
#endregion
public static bool Show(Image image)
{
return Show(image, 5000);
}
public static bool Show(Image image, int timer)
{
//if(image == null) return false;
SplashForm frm = null;
Bitmap bitmap = null;
Graphics graphics = null;
IntPtr screenDc = IntPtr.Zero;
IntPtr memDc = IntPtr.Zero;
IntPtr hBitmap = IntPtr.Zero;
IntPtr oldBitmap = IntPtr.Zero;
try {
frm = new SplashForm();
frm.Size = image.Size;
bitmap = new Bitmap(image);
graphics = frm.CreateGraphics();
screenDc = graphics.GetHdc();
memDc = CreateCompatibleDC(screenDc);
hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
oldBitmap = SelectObject(memDc, hBitmap);
Size pSize = new Size(bitmap.Width, bitmap.Height);
Point pSrc = new Point(0, 0);
Point pDst = new Point(
((Screen.PrimaryScreen.WorkingArea.Width - bitmap.Width) / 2),
((Screen.PrimaryScreen.WorkingArea.Height - bitmap.Height) / 2));
frm.Show();
for(int i = 0; i < 255; i += 5) {
UpdateLayeredWindow(
frm.Handle, screenDc, ref pDst, ref pSize, memDc, ref pSrc, 0, BlendFunction.Initialize((byte)i), ULW.Alpha);
Thread.Sleep(5);
}
Thread.Sleep(timer);
for(byte i = 255; i > 0; i -= 5) {
UpdateLayeredWindow(
frm.Handle, screenDc, ref pDst, ref pSize, memDc, ref pSrc, 0, BlendFunction.Initialize(i), ULW.Alpha);
Thread.Sleep(5);
}
frm.Close();
return true;
} catch {
return false;
} finally {
if(image != null){
graphics.ReleaseHdc(screenDc);
if(!((hBitmap == IntPtr.Zero))) {
SelectObject(memDc, oldBitmap);
DeleteObject(hBitmap);
} else {
DeleteDC(memDc);
}
}
}
}
}طريقة الاستدعاء
SplashScreen.Show(Bitmap); SplashScreen.Show(Bitmap, int);
مثال
// SplashScreenDemo.cs
namespace SplashScreenDemo {
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
class frmMain : Form
{
public frmMain() {
// btnCancel
Button btnCancel = new Button();
btnCancel.Location = new Point(255, 281);
btnCancel.Text = "&Cancel";
btnCancel.Click += new EventHandler(Cancel);
// frmMain
this.Name = "frmMain";
this.Text = "SplashScreen Demo";
this.StartPosition = FormStartPosition.CenterScreen;
this.ClientSize = new Size(342, 316);
this.Controls.Add(btnCancel);
}
private void Cancel(object o, EventArgs e){
Application.Exit();
}
[STAThread]
static void Main(string[] args) {
Application.EnableVisualStyles();
try {
if(args.Length > 0){
bool splashLoaded = false;
if(File.Exists(args[0])){
switch(args.Length)
{
case 1:
splashLoaded = SplashScreen.Show(Bitmap.FromFile(args[0]));
break;
case 2:
splashLoaded = SplashScreen.Show(Bitmap.FromFile(args[0]), int.Parse(args[1]));
break;
}
if(!splashLoaded)
MessageBox.Show(string.Format("Can not load SplashScreen form '{0}'", args[0]));
}
} else {
if(!SplashScreen.Show(Bitmap.FromFile(@".\SplashScreen.png"), 5000))
MessageBox.Show("Can not load SplashScreen");
}
} catch(Exception ex){
MessageBox.Show(ex.Message);
}
Application.Run(new frmMain());
}
}
}بالتوفيق ان شاء الله
