WPF使用PictureBox「建议收藏」

WPF使用PictureBox「建议收藏」一、添加引用按照下图添加相关引用二、代码实现1.Xaml<Windowx: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.micros

大家好,欢迎来到IT知识分享网。

一、添加引用

按照下图添加相关引用
引用

二、代码实现

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());
    }
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/16346.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信