📄 gtooltipex.cs
字号:
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using gowk.common;
namespace gowk.controls
{
public enum GToolTipIcon:int
{
Null,Info,Warning,Error
}
public class GTooltipEX : System.ComponentModel.Component
{
TTNW ttnw;
public GTooltipEX()
{
}
private void WndProc(ref Message m)
{
switch(m.Msg)
{
case API.WM_MOUSEMOVE:
this.Hide();
break;
case API.WM_PAINT:
this.Paint(ref m);
break;
default:
this.ttnw.DefWndProc(ref m);
break;
}
}
private void Paint(ref Message m)
{
this.ttnw.DefWndProc(ref m);return;
Rectangle rect=new Rectangle();
gowk.common.API.GetWindowRect(this.Handle,ref rect);
if(rect.Width==0||rect.Height==0)
{
this.ttnw.DefWndProc(ref m);
return;
}
System.IntPtr hdc=gowk.common.API.GetWindowDC(this.Handle);
System.Drawing.Graphics g=System.Drawing.Graphics.FromHdc(hdc);
Bitmap bm=new Bitmap(rect.Width,rect.Height);
Graphics gbm=Graphics.FromImage(bm);
System.IntPtr hbm=gbm.GetHdc();
gowk.common.API.BitBlt(hbm,0,0,rect.Width,rect.Height,hdc,0,0,gowk.common.API.SRCCOPY);
//bm.Save(@"c:\\gow.bmp");
this.ttnw.DefWndProc(ref m);
gowk.common.API.BitBlt(hdc,0,0,rect.Width,rect.Height,hbm,0,0,gowk.common.API.SRCCOPY);
// g.Clear(Color.White);
gbm.ReleaseHdc(hbm);
g.Dispose();
gowk.common.API.ReleaseDC(this.Handle,hdc);
}
private class TTNW:System.Windows.Forms.NativeWindow
{
private GTooltipEX gt;
public TTNW (GTooltipEX gt)
{
this.gt=gt;
}
protected override void WndProc(ref Message m)
{
this.gt.WndProc(ref m);
}
}
private TOOLINFO_T CreateT(string text)
{
TOOLINFO_T t=new TOOLINFO_T();
t.cbSize=System.Runtime.InteropServices.Marshal.SizeOf(typeof(TOOLINFO_T));
t.uFlags=gowk.common.API.TTF_TRACK;
t.lpszText=text;return t;
}
private void CreateHandle()
{
ttnw=new TTNW(this);
CreateParams params1 = new CreateParams();
params1.ClassName = "tooltips_class32";
params1.ExStyle = gowk.common.API.WS_EX_TOPMOST;
params1.Style=gowk.common.API.TTS_BALLOON|gowk.common.API.WS_POPUP;//|gowk.common.API.TTS_ALWAYSTIP|2|0x0010|0x0100;
ttnw.CreateHandle(params1);
API.SendMessage(this.Handle, API.TTM_ADDTOOLW, true, 0);
}
public void Show(string title,string text,GToolTipIcon ico,Point p)
{
if(this.IsHandleCreate)this.Close();
this.CreateHandle();
API.SendMessage(this.Handle,API.TTM_SETMAXTIPWIDTH,0,this.maxTipWidth);
if(title!=null ||title!=string.Empty)
SendMessage(this.Handle,Marshal.SystemDefaultCharSize==2?gowk.common.API.WM_USER+33:gowk.common.API.WM_USER+32,(int)ico,title);
SendMessage(this.ttnw.Handle,Marshal.SystemDefaultCharSize==2?gowk.common.API.TTM_ADDTOOLW:gowk.common.API.TTM_ADDTOOLA,0,CreateT(text));
gowk.common.API.SendMessage(this.ttnw.Handle,gowk.common.API.TTM_TRACKPOSITION,0,p.Y<<16 |p.X);
SendMessage(this.ttnw.Handle,gowk.common.API.TTM_TRACKACTIVATE,1,CreateT(null));
}
public void Show(string text,Point p)
{
this.Show(null,text,GToolTipIcon.Null,p);
}
public void Close()
{
this.Hide();
}
public void Hide()
{
if(this.IsHandleCreate)
{
SendMessage(this.ttnw.Handle,gowk.common.API.TTM_TRACKACTIVATE,0,CreateT(null));
this.ttnw.DestroyHandle();
}
}
private bool IsHandleCreate
{
get{return this.Handle!=IntPtr.Zero;}
}
public int MaxTipWidth
{
get//{return (int)API.SendMessage(this.Handle,(int)API.TTM_GETMAXTIPWIDTH,0,0);}
{
return this.maxTipWidth;
}
set
{
if(this.maxTipWidth!=value)
{
API.SendMessage(this.Handle,API.TTM_SETMAXTIPWIDTH,0,value);
this.maxTipWidth=value;
}
}
}
public IntPtr Handle
{
get{return this.ttnw==null?IntPtr.Zero:this.ttnw.Handle;}
}
int maxTipWidth=200;
#region
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
internal class TOOLINFO_T
{
public int cbSize;
public int uFlags;
public IntPtr hwnd;
public IntPtr uId;
public System.Drawing.Rectangle rect;
public IntPtr hinst;
public string lpszText;
public IntPtr lParam;
}
[DllImport("user32.dll", CharSet=CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, TOOLINFO_T lParam);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
internal static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -