111,098
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace My
{
public partial class Form2 : Form
{
//纵坐标增量
int pointY = -25;
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 点击一次按钮创建一个PictureBox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//将控件添加至父容器中
this.rtbTest.Controls.Add(CreateSoundPic("aa"));
}
/// <summary>
/// 动态创建一个图片框
/// </summary>
/// <param name="mediaPath">音频地址</param>
/// <returns></returns>
private PictureBox CreateSoundPic(string mediaPath)
{
pointY += 25;
//创建控件
PictureBox picBox = new PictureBox();
//设置坐标
picBox.Location = new Point(this.rtbTest.Location.X + 10, this.rtbTest.Location.Y + pointY);
//设置图片路径
picBox.Image = Image.FromFile(Application.StartupPath + "\\Images\\sound.gif");
//图片显示模式
picBox.SizeMode = PictureBoxSizeMode.AutoSize;
//图片的tag保存音频地址
//--------------------
picBox.Tag = mediaPath;
//--------------------
//设置鼠标样式
picBox.Cursor = Cursors.Hand;
//父容器为RichtextBox
picBox.Parent = this.rtbTest;
//控件的Clik事件
picBox.Click += new EventHandler(picBox_Click);
//返回PictureBox对象
return picBox;
}
/// <summary>
/// 图片框的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void picBox_Click(object sender, EventArgs e)
{
//还原事件源
PictureBox pic = sender as PictureBox;
//调用播放方法
PlaySound(pic.Tag.ToString());
}
/// <summary>
/// 点击一次创建一个超链接标签
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//将控件添加至父容器中
this.rtbTest.Controls.Add(CreateSoundLable("bb"));
}
/// <summary>
/// 动态创建LinkLable
/// </summary>
/// <param name="mediaPath">音频地址</param>
/// <returns></returns>
private LinkLabel CreateSoundLable(string mediaPath)
{
pointY += 25;
//创建控件
LinkLabel lklbl = new LinkLabel();
//设置坐标
lklbl.Location = new Point(this.rtbTest.Location.X + 10, this.rtbTest.Location.Y + pointY);
//设置显示文本
lklbl.Text = "点我播放音乐";
//设置鼠标样式
lklbl.Cursor = Cursors.Hand;
//Lable的tag保存音频地址
//--------------------
lklbl.Tag = mediaPath;
//--------------------
//设置下划线行为 = 当鼠标Hover时显示下划线
lklbl.LinkBehavior = LinkBehavior.HoverUnderline;
//父容器为RichtextBox
lklbl.Parent = this.rtbTest;
//控件的LinkClicked事件 我这样写是不准确的 应该新写一个LinkClicked事件 这里测试就不那么讲究了
lklbl.LinkClicked += new LinkLabelLinkClickedEventHandler(lnkLable_Click);
//返回LinkLable对象
return lklbl;
}
/// <summary>
/// LinkLable的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lnkLable_Click(object sender, EventArgs e)
{
//还原事件源
LinkLabel lnk = sender as LinkLabel;
//调用播放方法
PlaySound(lnk.Tag.ToString());
}
/// <summary>
/// 音频播放方法 里面换成你需要的播放音频的代码
/// </summary>
/// <param name="mediaPath">音频路径</param>
private void PlaySound(string mediaPath)
{
MessageBox.Show("开始播放" + mediaPath);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace My
{
public partial class Form2 : Form
{
//纵坐标增量
int pointY = -25;
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 点击一次按钮创建一个PictureBox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
pointY += 25;
//创建控件
PictureBox picBox = new PictureBox();
//设置坐标
picBox.Location = new Point(this.rtbTest.Location.X + 10, this.rtbTest.Location.Y + pointY);
//设置图片路径
picBox.Image = Image.FromFile(Application.StartupPath+"\\Images\\sound.gif");
//图片显示模式
picBox.SizeMode = PictureBoxSizeMode.AutoSize;
//设置鼠标样式
picBox.Cursor = Cursors.Hand;
//父容器为RichtextBox
picBox.Parent = this.rtbTest;
//控件的Clik事件
picBox.Click += new EventHandler(picBox_Click);
//将控件添加至父容器中
this.rtbTest.Controls.Add(picBox);
}
/// <summary>
/// 播放音频的代码放这里
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void picBox_Click(object sender, EventArgs e)
{
MessageBox.Show("用播放音频的代码替换我这个框框~");
}
/// <summary>
/// 点击一次创建一个超链接标签
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
pointY += 25;
//创建控件
LinkLabel lklbl = new LinkLabel();
//设置坐标
lklbl.Location = new Point(this.rtbTest.Location.X + 10, this.rtbTest.Location.Y + pointY);
//设置显示文本
lklbl.Text = "点我播放音乐";
//设置鼠标样式
lklbl.Cursor = Cursors.Hand;
//设置下划线行为 = 当鼠标Hover时显示下划线
lklbl.LinkBehavior = LinkBehavior.HoverUnderline;
//父容器为RichtextBox
lklbl.Parent = this.rtbTest;
//控件的LinkClicked事件 我这样写是不准确的 应该新写一个LinkClicked事件 这里测试就不那么讲究了
lklbl.LinkClicked += new LinkLabelLinkClickedEventHandler(picBox_Click);
//将控件添加至父容器中
this.rtbTest.Controls.Add(lklbl);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //播放WAV用
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//播放WAV用
[DllImport("winmm")]
public static extern bool PlaySound(string szSound, IntPtr hMod, int flags);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//点击richTextBox控件中的“http://w--212.wav”便播放“212.wav”
richTextBox1.AppendText("http://w--212.wav");
}
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e) //打开richTextBox中的链接
{
string file = Application.StartupPath + "\\" + e.LinkText.Substring(10);
PlaySound(file, IntPtr.Zero, 0x00020000 | 0x0001);
}
}
}