
WPF不屑于gif的简单动画!
但是这对程序员来说不大爽啊!急得我眼泪都下来了!
幸好WPF里有MediaElement这个东西,它是对MediaPlyer的一个封装,果然很强大啊。不过另我不爽的是我这里有N个gif图片就要有N个MediaElement,要了亲命了。
还是不好,如果你能想到用WebBrowseControl来实现,或者用Frame来实现,恭喜你,你太有才了!
我还是不想这么去做,才分不够啊!
重写一下WPF的image,good idea!
public class GIFImageControl : System.Windows.Controls.Image
{
delegate void OnFrameChangedDelegate()
private Bitmap m_Bitmap
public string Path { getset}
BitmapSource bitmapSource
public void AnimatedImageControl(string path)
{
Path = path
m_Bitmap = (Bitmap)Image.FromFile(path)
Width = m_Bitmap.Width
Height = m_Bitmap.Height
ImageAnimator.Animate(m_Bitmap, OnFrameChanged)
bitmapSource = GetBitmapSource()
Source = bitmapSource
}
private void OnFrameChanged(object sender, EventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new OnFrameChangedDelegate(OnFrameChangedInMainThread))
}
private void OnFrameChangedInMainThread()
{
ImageAnimator.UpdateFrames()
if (bitmapSource != null)
bitmapSource.Freeze()
bitmapSource = GetBitmapSource()
Source = bitmapSource
InvalidateVisual()
}
//private static bool loaded
private BitmapSource GetBitmapSource()
{
IntPtr inptr = m_Bitmap.GetHbitmap()
bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
inptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
DeleteObject(inptr)
return bitmapSource
}
[DllImport("gdi32")]
static extern int DeleteObject(IntPtr o)
}
ok,用window原有的东西去绑定到wpf上去。很好吧!是不是也比较有才呢?
来自: http://hi.baidu.com/mych/blog/item/1eb14f545f12a752564e00be.html
//新建Window,把其默认的Grid元素删除,然后//在Window_Loaded事件里粘贴如下代码
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Grid g = new Grid()
g.HorizontalAlignment = HorizontalAlignment.Stretch
g.VerticalAlignment = VerticalAlignment.Stretch
g.Background = Brushes.Blue
ScrollViewer s = new ScrollViewer()
s.Width = 100
s.Height = 100
Label l = new Label()
l.Content = @"wpf 控件内 动态添加 控件
Oo巴黎迷雾_ | 分类:C#/.NET | 浏览17次
请示范一个简单的完整例子,不是在form里, 而是在wpf grid里的scrollviewer控件添加label控件。"
s.Content = l
this.AddChild(s)
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)