一、添加引用
按照下图添加相关引用
二、代码实现
1.Xaml
<Window x:Class="PictureBoxInWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PictureBoxInWPF"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<wfi:WindowsFormsHost x:Name="pictureBoxHost" ToolTip=""></wfi:WindowsFormsHost>
</Grid>
</Window>
2.后台代码
添加了ToolTip,并且鼠标移动ToolTip跟随变化
public partial class MainWindow : Window
{
System.Windows.Forms.PictureBox m_pictureBox;
System.Windows.Forms.ToolTip m_toolTip;
ToolTip m_tp;
public MainWindow()
{
InitializeComponent();
m_pictureBox = new System.Windows.Forms.PictureBox();
m_tp = new ToolTip();
m_toolTip = new System.Windows.Forms.ToolTip();
m_toolTip.SetToolTip(m_pictureBox, "1");
m_tp.PlacementTarget = pictureBoxHost;
pictureBoxHost.Child = m_pictureBox;
m_pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(picturebox_Paint);
m_pictureBox.MouseMove += pictureBox_MouseMove;
}
//绘制事件
void picturebox_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Bitmap bmp = new Bitmap(@"C:\Users\admin\Pictures\111.jpg");
System.Drawing.Point ulPoint = new System.Drawing.Point(0, 0);
e.Graphics.DrawImage(bmp, ulPoint);
//m_toolTip.Show("222", this);
}
private void pictureBox_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Random rd = new Random();
int i = rd.Next(100);
m_toolTip.SetToolTip(m_pictureBox, i.ToString());
}
}