vb.net combobox中加tooltip的问题

ailby0709 2012-02-23 02:25:18
附上我的代码,

If e.State = DrawItemState.Selected Then
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.HighlightText, e.Bounds)
ToolTipCmb.Show(CboGraphSelection.Items(e.Index).ToString(), CboGraphSelection, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height)
e.DrawFocusRectangle()
Else
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.WindowText, e.Bounds)
End If

这样得到的combox和原来的不一样,原来的下拉框下拉的时候,鼠标放到某一项时则替颜色是白色的,其余的是黑色的
而我用以上代码重画combobox时,不管鼠标放没放到显示的都是黑色的。
请各位高手指教
...全文
214 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ailby0709 2012-02-24
  • 打赏
  • 举报
回复
还是不对... 你仔细看下
背景色是对的,不对的是他的字体的颜色
正常的下拉框,你下拉之后,鼠标没有滑倒某项时,字体都是黑色的,北京是白色的
用这个方法重画的下拉框,下拉之后,鼠标没有滑倒某项时字体是黑色的,滑倒某项时,字体还是黑色的
现在CSDN打不开,等会能打开上个图
dabuyingyeyaoda 2012-02-24
  • 打赏
  • 举报
回复
下拉框颜色还没找准,应该可以了。


Private ToolTipCmb As ToolTip
Private CboGraphSelection As ComboBox
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.State = DrawItemState.Selected Then
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.HighlightText, e.Bounds)

e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds)
ToolTipCmb.Show(CboGraphSelection.Items(e.Index).ToString(), CboGraphSelection, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height)
e.DrawFocusRectangle()
Else
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.WindowText, e.Bounds)
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ToolTipCmb = New ToolTip
CboGraphSelection = ComboBox1
End Sub
ailby0709 2012-02-24
  • 打赏
  • 举报
回复
回复楼上,得到的新的下拉框和系统自带的有些不一样,你发现了么?
ailby0709 2012-02-24
  • 打赏
  • 举报
回复
刚才那个图片有点小,看这个
ailby0709 2012-02-24
  • 打赏
  • 举报
回复
这时候的鼠标是放在第一行"11111"的那里的,还是不对...
ailby0709 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dabuyingyeyaoda 的回复:]
填充字体在填充矩形之后
[/Quote]
我按照你写的,得到结果是下图,不知道和你的结果一不一样
dabuyingyeyaoda 2012-02-24
  • 打赏
  • 举报
回复
填充字体在填充矩形之后


Private ToolTipCmb As ToolTip
Private CboGraphSelection As ComboBox
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.State = DrawItemState.Selected Then
e.Graphics.FillRectangle(Brushes.MidnightBlue, e.Bounds)
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.HighlightText, e.Bounds)
ToolTipCmb.Show(CboGraphSelection.Items(e.Index).ToString(), CboGraphSelection, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height)
e.DrawFocusRectangle()
Else
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.WindowText, e.Bounds)
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ToolTipCmb = New ToolTip
CboGraphSelection = ComboBox1
End Sub
ailby0709 2012-02-24
  • 打赏
  • 举报
回复
插入两张图片 看一下效果就知道了
dabuyingyeyaoda 2012-02-23
  • 打赏
  • 举报
回复
dabuyingyeyaoda 2012-02-23
  • 打赏
  • 举报
回复
用了楼主的代码,发现可以实现。有个combobox属性drawmode要变成OwnerDrawFixed


Public Class Form1
Private ToolTipCmb As ToolTip
Private CboGraphSelection As ComboBox
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.State = DrawItemState.Selected Then
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.HighlightText, e.Bounds)
ToolTipCmb.Show(CboGraphSelection.Items(e.Index).ToString(), CboGraphSelection, _
e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height)
e.DrawFocusRectangle()
Else
e.Graphics.DrawString(CboGraphSelection.Items(e.Index).ToString(), _
CboGraphSelection.Font, SystemBrushes.WindowText, e.Bounds)
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ToolTipCmb = New ToolTip
CboGraphSelection = ComboBox1
End Sub
End Class



ailby0709 2012-02-23
  • 打赏
  • 举报
回复
我在CSDN上还看到用API来实现的,但是这段代码不知道怎么用。。。

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Threading;

namespace comboBoxApp
...{
/**//// <summary>
/// 可带ToolTip的组合框控件
/// </summary>
public class ComboBoxEx : ComboBox
...{
/**//// <summary>
/// 这个子类窗口用来存放下拉列表窗口,通过它来操作下拉列表
/// </summary>
private SubWindow m_SubWindow;
/**//// <summary>
/// 通常的构造函数
/// </summary>
public ComboBoxEx()
...{
}
/**//// <summary>
/// 处理Windows的消息
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
...{
//通过这个消息可以得到下拉列表的窗口名柄
if (m.Msg == 0x210 && (int)m.WParam == 0x3e80001)
...{
//构建子类化窗口
SubWindow sw = new SubWindow();
//把当前ComboBox实例做为属性传入方便处理
sw.Owner = this;
//把得到的列表句柄关联到子类窗口类上。
sw.AssignHandle(m.LParam);
//这里的做用是保证子类窗口和ComboBoxEx生存期同步
this.m_SubWindow = sw;
}
base.WndProc(ref m);
}
/**//// <summary>
/// 重写以释放子类
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
...{
if (disposing && this.m_SubWindow != null)
...{
this.m_SubWindow.DestroyHandle();
}
base.Dispose(disposing);
}
}
/**//// <summary>
/// 子类化窗口的类
/// </summary>
internal class SubWindow : NativeWindow
...{
/**//// <summary>
/// 为了得到列表上的鼠标坐标而使用Api函数及其所用到的数据结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public class POINT
...{
public int x;
public int y;
public POINT(int x, int y)
...{
this.x = x;
this.y = y;
}
}
/**//// <summary>
/// 映射窗体的坐标
/// </summary>
/// <param name="hWndFrom">源窗口句柄</param>
/// <param name="hWndTo">要影射到的窗口句柄</param>
/// <param name="pt">转换前后的坐标数据</param>
/// <param name="cPoints"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, [In, Out] POINT pt, int cPoints);
/**//// <summary>
/// 为了得到指定坐标下的项而需要向列表发送消息
/// </summary>
/// <param name="hWnd"></param>
/// <param name="msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
/**//// <summary>
/// 为了得到指定索引的列表的内容而需要向列表发送消息,因为列表文本可能被格式化,所以这是合理的。
/// </summary>
/// <param name="hWnd"></param>
/// <param name="msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, StringBuilder lParam);

/**//// <summary>
/// 上一索引值
/// </summary>
private int m_Index;
/**//// <summary>
/// 用来显示信息ToolTip
/// </summary>
private ToolTip toolTip;
/**//// <summary>
/// 所属性的ComboBox
/// </summary>
private Control m_Owner;
/**//// <summary>
/// 构造函数
/// </summary>
public SubWindow()
...{
this.m_Index = -1;
this.toolTip = new ToolTip();
}
/**//// <summary>
/// 所属的控件
/// </summary>
public Control Owner
...{
get ...{ return m_Owner; }
set ...{ m_Owner = value; }
}
/**//// <summary>
/// 处理鼠标的消息以显示ToolTip信息
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
...{
if (m.Msg == 0x200)
...{
//获取鼠标坐标
Point msPoint = Cursor.Position;
POINT pt = new POINT(msPoint.X, msPoint.Y);
//影射到列表上的坐标
MapWindowPoints(IntPtr.Zero, this.Handle, pt, 1);
//获取鼠标下的项的索引
int index = SendMessage(m.HWnd, 0x1a9, 0, (pt.y << 0x10) | (pt.x & 0xffff));
if (((index >> 0x10) & 0xffff) == 0)
...{
index = (index & 0xffff);
if (m_Index != index)
...{
//获取项的字符串的长度
int num = SendMessage(this.Handle, 0x18a, index, 0);
StringBuilder lParam = new StringBuilder(num + 1);
//获取项的字符串内容
SendMessage(this.Handle, 0x189, index, lParam);
//获取鼠标在所属的控件的坐标信息
Point owPoint = this.Owner.PointToClient(msPoint);
//设置ToolTip信息并显示
this.toolTip.RemoveAll();
this.toolTip.Show(lParam.ToString(), this.Owner, owPoint.X + 10, owPoint.Y + 10, 1000);
m_Index = index;
}
}
}
base.WndProc(ref m);
}
}
}

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧