【WPF】label鼠标悬停提示、背景色

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/SevenGirl2017/article/details/84500589

效果

在这里插入图片描述

功能

1.鼠标悬停显示提示信息
2.鼠标划过修改背景色
3.鼠标离开恢复背景色

代码

设计界面

ToolTip属性用于设置鼠标悬停显示提示信息

<Label Name="lblClose" Content=" X" ToolTip="关闭" Foreground="White"  MouseLeftButtonDown="lblClose_MouseLeftButtonDown" 
MouseEnter="lblClose_MouseEnter" MouseLeave="lblClose_MouseLeave" />

后台

/// <summary>
/// 关闭-鼠标划过事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lblClose_MouseEnter(object sender, MouseEventArgs e)
{
    lblClose.Background = System.Windows.Media.Brushes.SlateGray;
}

/// <summary>
/// 关闭-鼠标离开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lblClose_MouseLeave(object sender, MouseEventArgs e)
{
    lblClose.Background = System.Windows.Media.Brushes.Transparent;
}
展开阅读全文

修改LABEL背景色

04-03

看看这个程序(实现竖行显示).怎么修改不了LABEL的背景色啊?如何才能把背景色设为白色啊?rn import java.awt.BorderLayout;rnimport java.awt.Dimension;rnimport java.awt.Font;rnimport java.awt.FontMetrics;rnimport java.awt.Graphics;rnimport java.awt.GridLayout;rnimport java.awt.event.ActionEvent;rnimport java.awt.event.ActionListener;rnimport java.awt.event.ItemEvent;rnimport java.awt.event.ItemListener;rnimport java.io.BufferedReader;rnimport java.io.PrintWriter;rnimport java.io.StringReader;rnimport java.io.StringWriter;rnimport java.util.Iterator;rnimport java.util.Vector;rnimport javax.swing.JButton;rnimport javax.swing.JComboBox;rnimport javax.swing.JFrame;rnimport javax.swing.JLabel;rnimport javax.swing.JPanel;rnimport javax.swing.JScrollPane;rnimport javax.swing.JTextPane;rnimport javax.swing.SwingConstants;rnimport javax.swing.UIManager;rnimport javax.swing.WindowConstants;rnpublic class JVLabel extends JLabel rn public JVLabel() rnthis.setBackground(Color.White); rnthis(null);rn rn public JVLabel(String text) rnthis.setBackground(Color.White); rn setText(text);rn rn private Vector lines=null;rn public void setText(String text) rn if(lines==null) rn lines=new Vector();rn rn this.lines.clear();rn if(text!=null && text.length()>0) rn BufferedReader reader=new BufferedReader(new StringReader(text));rn String line;rn try rn while((line=reader.readLine())!=null) rn lines.add(line);rn rn catch (Exception e) rn rn rn rn public String getText() rn StringWriter writer=new StringWriter();rn PrintWriter output=new PrintWriter(writer);rn for(Iterator it=lines.iterator();it.hasNext();) rn output.print(it.next().toString());rn if(it.hasNext()) rn output.println();rn rn rn return writer.toString();rn rn public void paint(Graphics g) rn g.clearRect(0,0,this.getWidth(),this.getHeight());rn FontMetrics metrics=g.getFontMetrics();rn int w[][]=new int[lines.size()][];rn int max[]=new int[lines.size()];rn int maxchar=0;rn int width=0;rn for(int i=0;imaxchar) rn maxchar=one.length();rn rn max[i]=0;rn for(int j=0;jmax[i]) rn max[i]=w[i][j];rn rn rn width+=max[i];rn rn int height=metrics.getFont().getSize()*maxchar;rn int x,y;rn int ha=this.getHorizontalAlignment();rn if(ha==SwingConstants.LEFT || ha==SwingConstants.LEADING) rn x=0;rn else if(ha==SwingConstants.CENTER) rn x=(this.getWidth()-width)/2;rn else rn x=this.getWidth()-width;rn rn int va=this.getVerticalAlignment();rn if(va==SwingConstants.TOP) rn y=0;rn else if(va==SwingConstants.CENTER) rn y=(this.getHeight()-height)/2;rn else rn y=this.getHeight()-height;rn rn x+=width;rn int oneheight=metrics.getFont().getSize();rn for(int i=0;i 论坛

没有更多推荐了,返回首页