目录
1.全选,反选
源码:
2.两个ListBox中的内容左右相互移动
运行效果:
点击添加到购物车按钮后:
源码:
3. 部门添加
运行效果:
源码:
4. 轮播图
运行效果:
源码:
Form1源码:
ChildForm源码:
Puchi源码:
6.2048纯手写代码(放代码里就能用,无需添加控件)
7.番茄钟
运行效果:
源码:
8.视频播放器
源码
1.全选,反选
思路:
全选: 当你一个一个把选项全部点亮时,全选选项也会亮
反选: 点击按钮后所有选项内容反过来
本文用到:
CheckBox(复选框)、Panel(容器控件)用来隔绝其他内容
winform窗口name属性
checkBox1 = 红双喜 checkBox2 = 白利 checkBox3 = 加多宝 checkBox4 = 喜之郎
checkBox5 = 压缩饼干 checkBox6 = 全选/取消全选 checkBox7 = 反选
![](https://img-blog.csdnimg.cn/f5f28880cea941728aea6aa35b640f88.png)
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
//创建一个CheckBox(多选)的集合
private CheckBox[] checkBoxes;
public Form1()
{
InitializeComponent();
//往集合里添加多选控件
checkBoxes = new CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
}
private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
//如果全选亮起,则以上五个选项亮起
if (checkBox6.Checked == true)
{
//遍历全部多选控件
foreach (CheckBox checkBox in this.checkBoxes)
{
checkBox.Checked = checkBox6.Checked;
}
}
//当以上五个选项全部亮起并且全选等于false的时候遍历全部多选控件让他们等于false
//检查是否所有复选框都被选中。如果是,则进入条件判断中。
if (!checkBoxes.Any(checkBox => !checkBox.Checked))
{
//检查全选复选框 checkBox6.Checked 是否为 false,如果是,则将所有复选框的状态设置为全选复选框的状态。
if (!checkBox6.Checked)
{
foreach (CheckBox checkBox in checkBoxes)
{
checkBox.Checked = checkBox6.Checked;
}
}
}
}
private void checkBox7_CheckedChanged(object sender, EventArgs e)
{
//遍历工具集合
//把五个选项全部赋予他们的反值
foreach (CheckBox checkBox in this.checkBoxes)
{
checkBox.Checked = !checkBox.Checked;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//引用UpdateSelectAllCheckBox方法
UpdateSelectAllCheckBox();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
UpdateSelectAllCheckBox();
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
UpdateSelectAllCheckBox();
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
UpdateSelectAllCheckBox();
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
UpdateSelectAllCheckBox();
}
//封装
public void UpdateSelectAllCheckBox()
{
//当你一个一个把选项全部点亮时,全选选项也会亮
//当你有一个选项不亮时,全选选项就不会亮
// 使用 LINQ 的 All 方法检查复选框集合中的所有元素是否被选中
bool allChecked = checkBoxes.All(checkBox => checkBox.Checked);
checkBox6.Checked = allChecked;
}
}
}
2.两个ListBox中的内容左右相互移动
思路:
先把左边选中的内容添加到右边,再把选中的内容删除,右边同理
运行效果:
![](https://img-blog.csdnimg.cn/2e7233673a3341eeb497a0384b338271.png)
点击添加到购物车按钮后:
![](https://img-blog.csdnimg.cn/78afac4b848f4e1fbcbb22874d31c365.png)
点击从购物车删除效果相反
本文用到:
两个botton按钮,两个ListBox列表框
winform窗口
listBox1 = listBox1 shopping = shopping 添加到购物车 = addition 从购物车删除 = delete
![](https://img-blog.csdnimg.cn/18bcdece1300438cb4dd1b662ed409b1.png)
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//在listbox中添加元素
listBox1.Items.Add("白酒");
listBox1.Items.Add("红酒");
listBox1.Items.Add("葡萄酒");
listBox1.Items.Add("幺幺九");
}
private void addition_Click(object sender, EventArgs e)
{
//调用方法A
A(listBox1,shopping);
}
private void delete_Click(object sender, EventArgs e)
{
A(shopping,listBox1);
}
//封装方法
public void A(ListBox listBox1, ListBox shopping)
{
//把你选中的选项添加到shopping框里
foreach (var selectedItem in listBox1.SelectedItems)
{
shopping.Items.Add(selectedItem);
}
//创建一个叫lists的用来存储索引的集合
List lists = new List();
//把你选中的选项遍历进去
foreach (int selectedIndex in listBox1.SelectedIndices)
{
//把你要选中选项的索引添加进去
lists.Add(selectedIndex);
}
//按索引逆序删除选中项
for (int i = lists.Count - 1; i >= 0; i--)
{
listBox1.Items.RemoveAt(lists[i]);
}
}
}
}
3. 部门添加
运行效果:
输入姓名后点击添加按钮
![](https://img-blog.csdnimg.cn/b116bd399a7c41b88e7565498fea3bd8.png)
本文用到:
botton按钮,comboBox下拉框,label标签,panel容器。
winform窗口
![](https://img-blog.csdnimg.cn/d78896e54601486088663d2ff850d5b4.png)
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//添加数组
comboBox1.Items.AddRange(new string[] { "销售部", "人力资源部", "技术部" });
comboBox3.Items.AddRange(new string[] { "男", "女" });
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//每次选择之前先清除comboBox2里的内容
comboBox2.Items.Clear();
//获取选择内容的索引
int selectedIndex = comboBox1.SelectedIndex;
//根据索引选择添加的内容
switch (selectedIndex)
{
case 0:
comboBox2.Items.AddRange(new string[] {"销售经理","销售副经理","销售员工" });
return;
case 1:
comboBox2.Items.AddRange(new string[] { "人力资源经理", "人力资源副经理", "人力资源员工" });
return;
case 2:
comboBox2.Items.AddRange(new string[] { "技术经理", "技术副经理", "技术员工" });
return;
}
}
public int num2 = 1;
private void button1_Click(object sender, EventArgs e)
{
//判断用户是否填写完成
if (textBox1 == null && comboBox1.SelectedItem == null && comboBox2.SelectedItem == null && comboBox3.SelectedItem == null)
{
MessageBox.Show("内容未填写完成");
}
Panel panel3 = new Panel(); // 创建 Panel3 的实例
panel3.Location = new Point(0, panel1.Controls.Count * 47);//距离
panel3.Size = new Size(500, 47);//大小
fz(panel3);
panel1.Controls.Add(panel3); // 将 Panel3 添加到 Panel1 的 Controls 集合中
num2++;
}
private void button2_Click(object sender, EventArgs e)
{
if (panel1.Controls.Count > 0)
{
Panel panel3 = (Panel)panel1.Controls[panel1.Controls.Count - 1];
//询问是否删除最后一个
DialogResult result = MessageBox.Show("确认删除最后一条记录吗?", "删除确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
//判断用户是否选择
if (result == DialogResult.Yes)
{
//删除
panel1.Controls.Remove(panel3);
panel3.Dispose();
//序号减一,用于下一步添加
num2--;
}
}
}
//创建五个label标签用于添加内容
public void fz(Panel panel3)
{
List labels = new List();
Label label = new Label();
label.Text = num2.ToString();
labels.Add(label);
Label label2 = new Label();
label2.Text = textBox1.Text;
labels.Add(label2);
Label label3 = new Label();
label3.Text = comboBox1.Text;
labels.Add(label3);
Label label4 = new Label();
label4.Text = comboBox2.Text;
labels.Add(label4);
Label label5 = new Label();
label5.Text = comboBox3.Text;
labels.Add(label5);
int int1 = 30;
foreach (Label control in labels)
{
control.Font = new Font("宋体", 9);
control.Size = new Size(100, 18);
control.Location = new Point(int1, 15);
panel3.Controls.Add(control);
int1 += 100;
}
}
}
}
4. 轮播图
首先在Resources文件夹中放几个图片,再对照源码进行修改
运行效果:
点击下一个换下一个,点击上一个就换上一个
![](https://img-blog.csdnimg.cn/768c710c73624d7fa9047cc202fadab1.png)
本文用到:
botton按钮,pictureBox图片框。
![](https://img-blog.csdnimg.cn/900dc236eba34eb4830014aea5aac2f9.png)
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Resources/后面就是图片名称,我这里一共有4个
public string[] str = new string[] { @"../../Resources/003102-1679675462d34a.jpg", @"../../Resources/004437-1677775477681c.jpg", @"../../Resources/010528-1676912728d2cc.jpg", @"../../Resources/155619-16766205795905.jpg" };
int num = 0;
private void button3_Click(object sender, EventArgs e)
{
num--;
if (num < 0)
{
num = 3;//图片数量4-1
}
Image image = Image.FromFile(str[num]); // 加载图像
pictureBox1.Image = image; // 在PictureBox控件中显示图像
}
private void button2_Click(object sender, EventArgs e)
{
num++;
if (num > 3)//图片数量4-1
{
num = 0;
}
Image image = Image.FromFile(str[num]); // 加载图像
pictureBox1.Image = image; // 在PictureBox控件中显示图像
}
}
}
5. menuStrip菜单栏
思路:
IsMdiContainer 窗体属性 false无法在内部创建窗体, true可以在内部创建窗体
先改属性为true
添加两个windows窗体一个叫ChildForm.cs,另一个叫Puchi.cs
Form1窗体:
![](https://img-blog.csdnimg.cn/a9bfa412aa9147559c661f2cfd7f86a8.png)
![](https://img-blog.csdnimg.cn/4471a0ab5e58469ab2ba13ec10bd3de6.png)
ChildForm窗体:
![](https://img-blog.csdnimg.cn/af0dbad68839442287b8d1c7fb524996.png)
Puchi窗体:
![](https://img-blog.csdnimg.cn/44d3088a84524beda8c6e483681ca030.png)
Form1源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 创建一个窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
ChildForm chi = new ChildForm();
chi.MdiParent = this;
chi.Show();
}
private void 创建扑哧扑哧窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
Puchi pu = new Puchi();
pu.MdiParent = this;
pu.Show();
}
private void 查看窗口数量ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(this.MdiChildren.Length.ToString() + "个子窗口");
}
private void 更改字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fontDialog = new FontDialog();
if (fontDialog.ShowDialog() == DialogResult.OK)
{
// 获取用户选择的字体
Font selectedFont = fontDialog.Font;
// 遍历所有打开的窗体,设置其字体
foreach (Form form in Application.OpenForms)
{
SetControlFont(form, selectedFont);
}
}
}
// 递归设置控件的字体
private void SetControlFont(Control control, Font font)
{
control.Font = font;
// 如果控件是容器控件,则递归设置其子控件的字体
if (control.HasChildren)
{
foreach (Control childControl in control.Controls)
{
SetControlFont(childControl, font);
}
}
}
private void 退出所有子窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach(Form item in this.MdiChildren)
{
item.Close();
}
}
private void 随机改变所有子窗口颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
Random random = new Random();
int num = random.Next(0, 256);
int num1 = random.Next(0, 256);
int num2 = random.Next(0, 256);
Color randomColor = Color.FromArgb(num, num1, num2);
foreach (var item in this.MdiChildren)
{
item.BackColor = randomColor;
}
}
}
}
ChildForm源码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WindowsFormsApp2
{
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
public static string a = "未输入";
public static string b = "未输入";
public static string c = "未输入";
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
a = textBox1.Text;
}
if (!string.IsNullOrEmpty(textBox2.Text))
{
b = textBox2.Text;
}
if (!string.IsNullOrEmpty(textBox3.Text))
{
c = textBox3.Text;
}
Close();
}
}
}
Puchi源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WindowsFormsApp2
{
public partial class Puchi : Form
{
public Puchi()
{
InitializeComponent();
string a = ChildForm.a;
string b = ChildForm.b;
string c = ChildForm.c;
label4.Text = a;
label5.Text = b;
label6.Text = c;
}
}
}
6.2048纯手写代码(放代码里就能用,无需添加控件)
首先修改属性
![](https://img-blog.csdnimg.cn/ab9f6bdf552a4786b405a8765d87ad91.png)
一定要更改为True否则键盘无法响应
这是一个放代码里就可以运行的2048
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private const int BoardSize = 4; // 游戏板的大小
private Label[,] cells; // 游戏板上的方格
private int[,] board; // 游戏板上的数字
private Random random; // 随机数生成器
public Form1()
{
InitializeComponent();
InitializeGame();
this.KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.W)
{
MoveUp();
e.Handled = true; // 防止按键事件继续传播
}
else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.S)
{
MoveDown();
e.Handled = true;
}
else if (e.KeyCode == Keys.Left || e.KeyCode == Keys.A)
{
MoveLeft();
e.Handled = true;
}
else if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
MoveRight();
e.Handled = true;
}
if (IsGameOver())
{
MessageBox.Show("游戏结束!");
// 这里可以添加重新开始游戏的逻辑
}
}
private void InitializeGame()
{
// 初始化游戏板
cells = new Label[BoardSize, BoardSize];
board = new int[BoardSize, BoardSize];
random = new Random();
// 创建游戏板上的方格
int cellSize = 100;
for (int row = 0; row < BoardSize; row++)
{
for (int col = 0; col < BoardSize; col++)
{
cells[row, col] = new Label
{
Size = new Size(cellSize, cellSize),
Location = new Point(col * cellSize, row * cellSize),
BorderStyle = BorderStyle.FixedSingle,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font("Arial", 20, FontStyle.Bold),
Visible = true
};
Controls.Add(cells[row, col]);
}
}
// 开始游戏
AddNewNumber();
UpdateBoard();
}
private void AddNewNumber()
{
// 在空白方格中随机选择一个位置生成新数字
List emptyCells = new List();
for (int row = 0; row < BoardSize; row++)
{
for (int col = 0; col < BoardSize; col++)
{
if (board[row, col] == 0)
{
emptyCells.Add(new Point(row, col));
}
}
}
if (emptyCells.Count > 0)
{
Point randomCell = emptyCells[random.Next(emptyCells.Count)];
int newValue = (random.Next(2) + 1) * 2; // 生成 2 或 4
board[randomCell.X, randomCell.Y] = newValue;
}
}
private void UpdateBoard()
{
// 更新游戏板上的方格和数字
for (int row = 0; row < BoardSize; row++)
{
for (int col = 0; col < BoardSize; col++)
{
cells[row, col].Text = board[row, col] == 0 ? "" : board[row, col].ToString();
cells[row, col].BackColor = GetCellColor(board[row, col]);
cells[row, col].ForeColor = GetTextColor(board[row, col]);
}
}
}
private Color GetCellColor(int value)
{
// 根据数字值返回对应的方格颜色
switch (value)
{
case 2: return Color.LightGray;
case 4: return Color.LightBlue;
case 8: return Color.LightGreen;
case 16: return Color.LightYellow;
case 32: return Color.Orange;
case 64: return Color.OrangeRed;
case 128: return Color.Red;
case 256: return Color.Pink;
case 512: return Color.Purple;
case 1024: return Color.BlueViolet;
case 2048: return Color.DarkViolet;
default: return Color.LightGray;
}
}
private Color GetTextColor(int value)
{
// 根据数字值返回对应的文本颜色
return value >= 8 ? Color.White : Color.Black;
}
private bool MoveUp()
{
// 向上移动方块
bool moved = false;
for (int col = 0; col < BoardSize; col++)
{
for (int row = 1; row < BoardSize; row++)
{
if (board[row, col] != 0)
{
for (int i = row; i > 0; i--)
{
if (board[i - 1, col] == 0)
{
board[i - 1, col] = board[i, col];
board[i, col] = 0;
moved = true;
}
else if (board[i - 1, col] == board[i, col])
{
board[i - 1, col] *= 2;
board[i, col] = 0;
moved = true;
break;
}
}
}
}
}
if (moved)
{
AddNewNumber();
UpdateBoard();
}
return moved;
}
private bool MoveDown()
{
// 向下移动方块
bool moved = false;
for (int col = 0; col < BoardSize; col++)
{
for (int row = BoardSize - 2; row >= 0; row--)
{
if (board[row, col] != 0)
{
for (int i = row; i < BoardSize - 1; i++)
{
if (board[i + 1, col] == 0)
{
board[i + 1, col] = board[i, col];
board[i, col] = 0;
moved = true;
}
else if (board[i + 1, col] == board[i, col])
{
board[i + 1, col] *= 2;
board[i, col] = 0;
moved = true;
break;
}
}
}
}
}
if (moved)
{
AddNewNumber();
UpdateBoard();
}
return moved;
}
private bool MoveLeft()
{
// 向左移动方块
bool moved = false;
for (int row = 0; row < BoardSize; row++)
{
for (int col = 1; col < BoardSize; col++)
{
if (board[row, col] != 0)
{
for (int i = col; i > 0; i--)
{
if (board[row, i - 1] == 0)
{
board[row, i - 1] = board[row, i];
board[row, i] = 0;
moved = true;
}
else if (board[row, i - 1] == board[row, i])
{
board[row, i - 1] *= 2;
board[row, i] = 0;
moved = true;
break;
}
}
}
}
}
if (moved)
{
AddNewNumber();
UpdateBoard();
}
return moved;
}
private bool MoveRight()
{
// 向右移动方块
bool moved = false;
for (int row = 0; row < BoardSize; row++)
{
for (int col = BoardSize - 2; col >= 0; col--)
{
if (board[row, col] != 0)
{
for (int i = col; i < BoardSize - 1; i++)
{
if (board[row, i + 1] == 0)
{
board[row, i + 1] = board[row, i];
board[row, i] = 0;
moved = true;
}
else if (board[row, i + 1] == board[row, i])
{
board[row, i + 1] *= 2;
board[row, i] = 0;
moved = true;
break;
}
}
}
}
}
if (moved)
{
AddNewNumber();
UpdateBoard();
}
return moved;
}
private bool IsGameOver()
{
// 检查游戏是否结束
for (int row = 0; row < BoardSize; row++)
{
for (int col = 0; col < BoardSize; col++)
{
// 如果还有空白方格或相邻方格有相同数字,则游戏未结束
if (board[row, col] == 0 ||
(row > 0 && board[row, col] == board[row - 1, col]) ||
(row < BoardSize - 1 && board[row, col] == board[row + 1, col]) ||
(col > 0 && board[row, col] == board[row, col - 1]) ||
(col < BoardSize - 1 && board[row, col] == board[row, col + 1]))
{
return false;
}
}
}
return true;
}
}
}
7.番茄钟
运行效果:
当你选择时间之后,会出来一个小窗口,小窗口无法关闭,无法拖动,时间完毕之后会响起音乐
![](https://img-blog.csdnimg.cn/6bb31d7fd2634496a67398052783c233.png)
本程序用到
label两个(没有关于label的代码,只用来提示),comboBox一个
![](https://img-blog.csdnimg.cn/8bb87b4678374bcb8e7e26ea298f55f0.png)
源码:
音乐无法正常响起,可能是格式问题,我这里用的是.wav格式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private SoundPlayer soundPlayer; // 声音播放器
public Form1()
{
InitializeComponent();
// 初始化下拉框选项
for (int i = 1; i
{
// 每次定时器触发时执行的操作
if (countdownSeconds == 0)
{
if (countdownMinutes == 0)
{
// 倒计时结束,停止定时器并关闭倒计时窗体
countdownTimer.Stop();
countdownForm.Close();
MessageBox.Show("倒计时结束!");
soundPlayer.SoundLocation = "../../音乐/鸡你太美.wav";//设置音乐路径
soundPlayer.Play(); // 播放音乐
}
else
{
// 倒计时分钟数减一,倒计时秒数设置为59
countdownMinutes--;
countdownSeconds = 59;
}
}
else
{
// 倒计时秒数减一
countdownSeconds--;
}
countdownLabel.Text = string.Format("{0:00}:{1:00}", countdownMinutes, countdownSeconds);
};
countdownTimer.Start(); // 启动倒计时定时器
countdownForm.ShowDialog(); // 显示倒计时窗体
}
}
}
}
}
8.视频播放器
注:找不到WindowsMediaPlayer控件可以看一下我的另一个博文
winform视频播放控件_再见到樱花的博客-CSDN博客
运行效果:
单击文件在本地选择视频文件(可多选),然后选择所有视频循环播放
![](https://img-blog.csdnimg.cn/e669b76844bf4ff7ac48b4f6f77a0924.png)
本程序用到
FToolStripMenuItem一个,WindowsMediaPlayer一个
![](https://img-blog.csdnimg.cn/43f8e1d4fe744127a9bbc8c4fbff98b1.png)
源码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void FToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenViedoFile();
}
private void OpenViedoFile()
{
OpenFileDialog vidoFile = new OpenFileDialog(); // 创建一个打开文件对话框实例
vidoFile.Title = "选择视频文件"; // 设置对话框标题
vidoFile.InitialDirectory = @"C:\User\Administrator\Desktop"; // 设置初始目录
vidoFile.Multiselect = true; // 允许多选文件
vidoFile.Filter = "视频文件|*.mp4;*avi|所有文件|*.*"; // 设置文件过滤器,只显示视频文件
vidoFile.ShowDialog(); // 显示打开文件对话框并等待用户选择文件
this.axWindowsMediaPlayer1.settings.setMode("loop", true); // 设置 Windows Media Player 的播放模式为循环播放
foreach (var item in vidoFile.FileNames) // 遍历选择的文件列表
{
this.axWindowsMediaPlayer1.currentPlaylist.appendItem(this.axWindowsMediaPlayer1.newMedia(item));
// 将每个文件添加到 Windows Media Player 的当前播放列表中
// 使用 newMedia 方法创建媒体项,使用 appendItem 方法将其添加到播放列表中
}
}
}
}
|