- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
[C#] Hướng dẫn sử dụng thư viện AnimateWindow User32.dll dùng tạo hiệu ứng effect Winform
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn đến các bạn cách tạo hiệu ứng chuyển động (effect animation Winform) bằng ngôn ngữ lập trình C#.
[C#] Using AnimateWindow Lib User32.dll
Trong thư viện mặc định chuyển Windows, có cung cấp cho chúng ta một số hiệu ứng chuyển động.
Chúng ta có thể sử dụng thư viện này để tạo hiệu ứng cho form load hay form closing hay một control nào đó trong Winform.
Giao diện demo ứng dụng sử dụng hiệu ứng effect winform c#

Để sử dụng đầu tiên, các bạn cần import thư viện cho project của mình.
using System.Runtime.InteropServices;
Tiếp đến, các bạn sẽ sử dụng hàm AnimateWindow của lib User32.dll như sau:
[DllImport(“user32.dll”)]
2
static extern bool AnimateWindow(IntPtr hWnd, int time, AnimateWindowFlags flags);
Hàm AnimateWindow chúng ta sẽ truyền vào ba tham số:
Tham số 1: Là Handle của đối tượng chúng ta cần thực hiện hiệu ứng
Tham số 2: Thời gian delay của hiệu ứng (mili giây)
Tham số 3: Là các cờ hiệu ứng mà chúng ta khai báo ở Enum bên dưới
Và tiếp tục, chúng ta sẽ khai báo một Enum AnimateWindowFlags chứa các hiệu ứng của windows.
enum AnimateWindowFlags : uint
{
AW_HOR_POSITIVE = 0x00000001,
AW_HOR_NEGATIVE = 0x00000002,
AW_VER_POSITIVE = 0x00000004,
AW_VER_NEGATIVE = 0x00000008,
AW_CENTER = 0x00000010,
AW_HIDE = 0x00010000,
AW_ACTIVATE = 0x00020000,
AW_SLIDE = 0x00040000,
AW_BLEND = 0x00080000
}
Trong những cờ này chúng ta có thể sử dụng đơn lẻ hoặc có thể kết hợp chúng lại với nhau.
Tuy nhiên, các bạn cần chú ý là có một số hiệu ứng không kết hợp được với nhau, nếu các bạn kết hợp với nhau, nó sẽ mất tác dụng.
Cách sử dụng một hiệu ứng:
AnimateWindow(this.Handle, 500, AnimateWindowFlags.AW_BLEND);
Cách kết hợp nhiều hiệu ứng lại với nhau: (Hiệu ứng này bạn có thể áp dụng cho Form Closing)
AnimateWindow(this.Handle, 1000, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HIDE);
Full source demo ứng dụng AnimateWindow C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnimateWindow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int AnimateWindow
(IntPtr hwand, int dwTime, AnimateWindowFlags flags);
enum AnimateWindowFlags : uint
{
AW_HOR_POSITIVE = 0x00000001,
AW_HOR_NEGATIVE = 0x00000002,
AW_VER_POSITIVE = 0x00000004,
AW_VER_NEGATIVE = 0x00000008,
AW_CENTER = 0x00000010,
AW_HIDE = 0x00010000,
AW_ACTIVATE = 0x00020000,
AW_SLIDE = 0x00040000,
AW_BLEND = 0x00080000
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
AnimateWindow(frm.Handle, 1000, AnimateWindowFlags.AW_ACTIVATE | AnimateWindowFlags.AW_BLEND);
}
private void button2_Click(object sender, EventArgs e)
{
AnimateWindow(pictureBox1.Handle, 1000, AnimateWindowFlags.AW_SLIDE | AnimateWindowFlags.AW_CENTER | AnimateWindowFlags.AW_HIDE);
}
private void button3_Click(object sender, EventArgs e)
{
AnimateWindow(pictureBox1.Handle, 1000, AnimateWindowFlags.AW_SLIDE | AnimateWindowFlags.AW_CENTER | AnimateWindowFlags.AW_ACTIVATE);
}
private void button5_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
AnimateWindow(frm.Handle, 1000, AnimateWindowFlags.AW_SLIDE | AnimateWindowFlags.AW_CENTER | AnimateWindowFlags.AW_ACTIVATE);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
AnimateWindow(this.Handle, 1000, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HIDE);
}
}
}
Thanks for watching!