AbbasSediqi
یک شنبه 01 فروردین 1395, 10:39 صبح
با سلام خدمت همه اساتید
در این تاپیک سعی بر اینه که به نحو خوبی سعی در استفاده بهینه از قابلیت های wpf بشه
چیزی که تا به الان دیدم مخصوصا در برنامه نویسی wpf در ایران اینه که هنوزم سعی میکنن به روش winform برنامه تهیه کنن
در wpf راه های بسیار زیادی وجود داره تا پروژه های بسیار شاخص و زیبا طراحی و اجرا کرد
به طور مثال و برای نخستین حرکت کنترل gift برای فایل های متحرک که در صورت استفاده نمای بهتری به برنامه میده
using Microsoft.VisualBasic;using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
public class AnimatedGIFControl : System.Windows.Controls.Image
{
private Bitmap _bitmap;
// Local bitmap member to cache image resource
private BitmapSource _bitmapSource;
public delegate void FrameUpdatedEventHandler();
/// <summary>
/// Delete local bitmap resource
/// Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx
/// </summary>
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
/// <summary>
/// Override the OnInitialized method
/// </summary>
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
}
/// <summary>
/// Load the embedded image for the Image.Source
/// </summary>
private void AnimatedGIFControl_Loaded(object sender, RoutedEventArgs e)
{
_bitmap = new Bitmap(Microsoft.VisualBasic.Right(Source.ToString , Strings.Len(Source.ToString) - 8));
_bitmapSource = Source;
}
/// <summary>
/// Close the FileStream to unlock the GIF file
/// </summary>
private void AnimatedGIFControl_Unloaded(object sender, RoutedEventArgs e)
{
StopAnimate();
}
/// <summary>
/// Start animation
/// </summary>
public void StartAnimate()
{
ImageAnimator.Animate(_bitmap, OnFrameChanged);
}
/// <summary>
/// Stop animation
/// </summary>
public void StopAnimate()
{
ImageAnimator.StopAnimate(_bitmap, OnFrameChanged);
}
/// <summary>
/// Event handler for the frame changed
/// </summary>
private void OnFrameChanged(object sender, EventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new FrameUpdatedEventHandler(FrameUpdatedCallback));
}
private void FrameUpdatedCallback()
{
ImageAnimator.UpdateFrames();
if (_bitmapSource != null) {
_bitmapSource.Freeze();
}
// Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
_bitmapSource = GetBitmapSource();
Source = _bitmapSource;
InvalidateVisual();
}
private BitmapSource GetBitmapSource()
{
IntPtr handle = IntPtr.Zero;
try {
handle = _bitmap.GetHbitmap();
_bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
} finally {
if (handle != IntPtr.Zero) {
DeleteObject(handle);
}
}
return _bitmapSource;
}
public AnimatedGIFControl()
{
Unloaded += AnimatedGIFControl_Unloaded;
Loaded += AnimatedGIFControl_Loaded;
}
}
Imports System.DrawingImports System.IO
Imports System.Runtime.InteropServices
Imports System.Windows
Imports System.Windows.Interop
Imports System.Windows.Media.Imaging
Imports System.Windows.Threading
Public Class AnimatedGIFControl
Inherits System.Windows.Controls.Image
Private _bitmap As Bitmap
' Local bitmap member to cache image resource
Private _bitmapSource As BitmapSource
Public Delegate Sub FrameUpdatedEventHandler()
''' <summary>
''' Delete local bitmap resource
''' Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx
''' </summary>
<DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
''' <summary>
''' Override the OnInitialized method
''' </summary>
Protected Overrides Sub OnInitialized(ByVal e As EventArgs)
MyBase.OnInitialized(e)
End Sub
''' <summary>
''' Load the embedded image for the Image.Source
''' </summary>
Private Sub AnimatedGIFControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.Loaded
_bitmap = New Bitmap(Microsoft.VisualBasic.Right(Source.ToString , Len(Source.ToString) - 8))
_bitmapSource = Source
End Sub
''' <summary>
''' Close the FileStream to unlock the GIF file
''' </summary>
Private Sub AnimatedGIFControl_Unloaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.Unloaded
StopAnimate()
End Sub
''' <summary>
''' Start animation
''' </summary>
Public Sub StartAnimate()
ImageAnimator.Animate(_bitmap, AddressOf OnFrameChanged)
End Sub
''' <summary>
''' Stop animation
''' </summary>
Public Sub StopAnimate()
ImageAnimator.StopAnimate(_bitmap, AddressOf OnFrameChanged)
End Sub
''' <summary>
''' Event handler for the frame changed
''' </summary>
Private Sub OnFrameChanged(ByVal sender As Object, ByVal e As EventArgs)
Dispatcher.BeginInvoke(DispatcherPriority.Normal, New FrameUpdatedEventHandler(AddressOf FrameUpdatedCallback))
End Sub
Private Sub FrameUpdatedCallback()
ImageAnimator.UpdateFrames()
If _bitmapSource IsNot Nothing Then
_bitmapSource.Freeze()
End If
' Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
_bitmapSource = GetBitmapSource()
Source = _bitmapSource
InvalidateVisual()
End Sub
Private Function GetBitmapSource() As BitmapSource
Dim handle As IntPtr = IntPtr.Zero
Try
handle = _bitmap.GetHbitmap()
_bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
Finally
If handle <> IntPtr.Zero Then
DeleteObject(handle)
End If
End Try
Return _bitmapSource
End Function
End Class
این هم یک نمونه فایل gif قرار دادم
http://s7.picofile.com/file/8244176418/gear_800_fullfixed641.gif
در این تاپیک سعی بر اینه که به نحو خوبی سعی در استفاده بهینه از قابلیت های wpf بشه
چیزی که تا به الان دیدم مخصوصا در برنامه نویسی wpf در ایران اینه که هنوزم سعی میکنن به روش winform برنامه تهیه کنن
در wpf راه های بسیار زیادی وجود داره تا پروژه های بسیار شاخص و زیبا طراحی و اجرا کرد
به طور مثال و برای نخستین حرکت کنترل gift برای فایل های متحرک که در صورت استفاده نمای بهتری به برنامه میده
using Microsoft.VisualBasic;using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
public class AnimatedGIFControl : System.Windows.Controls.Image
{
private Bitmap _bitmap;
// Local bitmap member to cache image resource
private BitmapSource _bitmapSource;
public delegate void FrameUpdatedEventHandler();
/// <summary>
/// Delete local bitmap resource
/// Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx
/// </summary>
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
/// <summary>
/// Override the OnInitialized method
/// </summary>
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
}
/// <summary>
/// Load the embedded image for the Image.Source
/// </summary>
private void AnimatedGIFControl_Loaded(object sender, RoutedEventArgs e)
{
_bitmap = new Bitmap(Microsoft.VisualBasic.Right(Source.ToString , Strings.Len(Source.ToString) - 8));
_bitmapSource = Source;
}
/// <summary>
/// Close the FileStream to unlock the GIF file
/// </summary>
private void AnimatedGIFControl_Unloaded(object sender, RoutedEventArgs e)
{
StopAnimate();
}
/// <summary>
/// Start animation
/// </summary>
public void StartAnimate()
{
ImageAnimator.Animate(_bitmap, OnFrameChanged);
}
/// <summary>
/// Stop animation
/// </summary>
public void StopAnimate()
{
ImageAnimator.StopAnimate(_bitmap, OnFrameChanged);
}
/// <summary>
/// Event handler for the frame changed
/// </summary>
private void OnFrameChanged(object sender, EventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new FrameUpdatedEventHandler(FrameUpdatedCallback));
}
private void FrameUpdatedCallback()
{
ImageAnimator.UpdateFrames();
if (_bitmapSource != null) {
_bitmapSource.Freeze();
}
// Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
_bitmapSource = GetBitmapSource();
Source = _bitmapSource;
InvalidateVisual();
}
private BitmapSource GetBitmapSource()
{
IntPtr handle = IntPtr.Zero;
try {
handle = _bitmap.GetHbitmap();
_bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
} finally {
if (handle != IntPtr.Zero) {
DeleteObject(handle);
}
}
return _bitmapSource;
}
public AnimatedGIFControl()
{
Unloaded += AnimatedGIFControl_Unloaded;
Loaded += AnimatedGIFControl_Loaded;
}
}
Imports System.DrawingImports System.IO
Imports System.Runtime.InteropServices
Imports System.Windows
Imports System.Windows.Interop
Imports System.Windows.Media.Imaging
Imports System.Windows.Threading
Public Class AnimatedGIFControl
Inherits System.Windows.Controls.Image
Private _bitmap As Bitmap
' Local bitmap member to cache image resource
Private _bitmapSource As BitmapSource
Public Delegate Sub FrameUpdatedEventHandler()
''' <summary>
''' Delete local bitmap resource
''' Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx
''' </summary>
<DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
''' <summary>
''' Override the OnInitialized method
''' </summary>
Protected Overrides Sub OnInitialized(ByVal e As EventArgs)
MyBase.OnInitialized(e)
End Sub
''' <summary>
''' Load the embedded image for the Image.Source
''' </summary>
Private Sub AnimatedGIFControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.Loaded
_bitmap = New Bitmap(Microsoft.VisualBasic.Right(Source.ToString , Len(Source.ToString) - 8))
_bitmapSource = Source
End Sub
''' <summary>
''' Close the FileStream to unlock the GIF file
''' </summary>
Private Sub AnimatedGIFControl_Unloaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.Unloaded
StopAnimate()
End Sub
''' <summary>
''' Start animation
''' </summary>
Public Sub StartAnimate()
ImageAnimator.Animate(_bitmap, AddressOf OnFrameChanged)
End Sub
''' <summary>
''' Stop animation
''' </summary>
Public Sub StopAnimate()
ImageAnimator.StopAnimate(_bitmap, AddressOf OnFrameChanged)
End Sub
''' <summary>
''' Event handler for the frame changed
''' </summary>
Private Sub OnFrameChanged(ByVal sender As Object, ByVal e As EventArgs)
Dispatcher.BeginInvoke(DispatcherPriority.Normal, New FrameUpdatedEventHandler(AddressOf FrameUpdatedCallback))
End Sub
Private Sub FrameUpdatedCallback()
ImageAnimator.UpdateFrames()
If _bitmapSource IsNot Nothing Then
_bitmapSource.Freeze()
End If
' Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
_bitmapSource = GetBitmapSource()
Source = _bitmapSource
InvalidateVisual()
End Sub
Private Function GetBitmapSource() As BitmapSource
Dim handle As IntPtr = IntPtr.Zero
Try
handle = _bitmap.GetHbitmap()
_bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
Finally
If handle <> IntPtr.Zero Then
DeleteObject(handle)
End If
End Try
Return _bitmapSource
End Function
End Class
این هم یک نمونه فایل gif قرار دادم
http://s7.picofile.com/file/8244176418/gear_800_fullfixed641.gif