winform中的Balloon提示

本文介绍如何使用C#自定义ToolTip控件的样式,包括设置气球提示风格及背景颜色,通过调用NativeMethods类的方法实现。适用于希望增强应用程序用户交互体验的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.扩展类库
None.gifnamespace DvsCC
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
using System;
InBlock.gif    
using System.Drawing;
InBlock.gif    
using System.Reflection;
InBlock.gif    
using System.Runtime.InteropServices;
InBlock.gif    
using System.Windows.Forms;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for NativeMethods.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class NativeMethods
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private const long WS_POPUP = 0x80000000;
InBlock.gif        
private const long TTS_BALLOON = 0x40;
InBlock.gif        
private const long TTS_NOFADE = 0x20;
InBlock.gif        
private const int GWL_STYLE = -16;
InBlock.gif        
private const int WM_USER = 0x0400;
InBlock.gif        
private const int TTM_SETTIPBKCOLOR = WM_USER + 19;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private NativeMethods() dot.gif{}
InBlock.gif
InBlock.gif
InBlock.gif        
public static void SetBalloonStyle ( ToolTip toolTip )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NativeWindow window 
= GetNativeWindow ( toolTip );
InBlock.gif            NativeMethods.SetWindowLong ( window.Handle, GWL_STYLE , WS_POPUP 
| TTS_BALLOON | TTS_NOFADE );
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
public static void SetBackColor ( ToolTip toolTip, Color color )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int backColor =  ColorTranslator.ToWin32( color );
InBlock.gif            NativeWindow window 
= GetNativeWindow ( toolTip );
InBlock.gif            
//setting back color
InBlock.gif
            SendMessage( window.Handle, TTM_SETTIPBKCOLOR, backColor, 0 );  
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static NativeWindow GetNativeWindow ( ToolTip toolTip )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FieldInfo windowField 
= toolTip.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance );
InBlock.gif            NativeWindow window  
= (NativeWindow)windowField.GetValue ( toolTip );
InBlock.gif            
if ( window.Handle == IntPtr.Zero ) throw new ArgumentNullException ( "window handle is not crated." );
InBlock.gif            
return window;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"user32.dll")]
InBlock.gif        
private static extern long SetWindowLong(IntPtr hwnd,int index,long val);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll")]
InBlock.gif        
private static extern int SendMessage( IntPtr hwnd, int msg, int wParam, int lParam);
ExpandedSubBlockEnd.gif    }

InBlock.gif 
ExpandedBlockEnd.gif}

None.gif

2.使用方法
None.gifNativeMethods.SetBalloonStyle ( toolTip1 );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值