📄 skinner.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Threading;
using System.Text;
using System.Runtime.InteropServices;
using gowk.common;
namespace gowk.utility.skin
{
public class Skinner
{
static Skinner m_skinner=new Skinner();
private Hooker m_hooker;
private ArrayList m_al;
private Skinner()
{
this.m_al=new ArrayList();
this.m_hooker=new Hooker(HookType.WH_CBT);
this.m_hooker.HookEvent+=new HookEventHandler(m_hooker_HookEvent);
SkinFactory.Instance.Add(typeof(Button),typeof(gowk.utility.skin.subclass.ButtonSubClass));
}
public static Skinner Instance{get{return m_skinner;}}
public void Install()
{
this.EnumWindowInCurrentThreadAndSkinIt();
this.m_hooker.Install();
}
public void Uninstall()
{
this.m_hooker.Uninstall();
lock(this.m_al)
{
foreach(SubClassBase nw in this.m_al)
{
nw.ReleaseHandle();
}
}
this.m_al.Clear();
}
#region
/*
HCBT_MOVESIZE = 0,
HCBT_MINMAX = 1,
HCBT_QS = 2,
HCBT_CREATEWND = 3,
HCBT_DESTROYWND = 4,
HCBT_ACTIVATE = 5,
HCBT_CLICKSKIPPED = 6,
HCBT_KEYSKIPPED = 7,
HCBT_SYSCOMMAND = 8,
HCBT_SETFOCUS = 9*/
#endregion
private void m_hooker_HookEvent(object sender, HookEventArgs e)
{
switch(e.nCode)
{
case 3:
System.Diagnostics.Trace.WriteLine(e.wParam.ToString());
/* try
{
CBT_CREATEWND ccw=(CBT_CREATEWND)Marshal.PtrToStructure(e.lParam,typeof(CBT_CREATEWND));
System.Diagnostics.Trace.WriteLine(ccw.lpcs.ToString());
CREATESTRUCT ct=(CREATESTRUCT)Marshal.PtrToStructure(ccw.lpcs,typeof(CREATESTRUCT));
System.Diagnostics.Trace.WriteLine(ct.lpszName);
}
catch(System.Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}*/
this.SkinWindow(e.wParam);
break;
case 4:
this.UnSkinWindow(e.wParam);
break;
case 5://HCBT_ACTIVATE
break;
}
}
private void SkinWindow(IntPtr hwnd)
{
StringBuilder bd=new StringBuilder(128);
int rz=GetClassName(hwnd,bd,bd.Capacity);
if(rz!=0)//if error returns 0
{
System.Diagnostics.Trace.Write(hwnd.ToString());
System.Diagnostics.Trace.Write(bd.ToString());
}
Control c=Control.FromHandle(hwnd);
if(c!=null)
{
System.Diagnostics.Trace.WriteLine(c.Text);
Type t=c.GetType();
SkinAttribute sa=(SkinAttribute)Attribute.GetCustomAttribute(t,typeof(SkinAttribute),true);
if(null!=sa && sa.NoSkin)
{
return;
}
else
{
SubClassBase nw=SkinFactory.Instance.GetSubClass(hwnd);
if(nw!=null)
{
this.m_al.Add(nw);
}
}
}
}
private void UnSkinWindow(IntPtr hwnd)
{
lock(this.m_al)
{
for(int i=0;i<this.m_al.Count;i++)
{
SubClassBase nw=(SubClassBase)this.m_al[i];
if(nw.Handle==hwnd)
{
nw.ReleaseHandle();
this.m_al.Remove(nw);
}
}
}
}
#region
EnumThreadWndProc m_enumproc;
private void EnumWindowInCurrentThreadAndSkinIt()
{
this.m_enumproc=new EnumThreadWndProc(this.EnumProc);
EnumThreadWindows(AppDomain.GetCurrentThreadId(),this.m_enumproc,IntPtr.Zero);
}
private bool EnumProc(IntPtr hwnd,IntPtr lParam)
{
this.SkinWindow(hwnd);
EnumChildWindows(hwnd,this.m_enumproc,IntPtr.Zero);
return true;
}
#endregion
#region
delegate bool EnumThreadWndProc(IntPtr hwnd,IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId,EnumThreadWndProc lpfn,IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumChildWindows(IntPtr hWndParent,EnumThreadWndProc lpfn,IntPtr lParam);
[DllImport("user32.dll",CharSet=CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd,StringBuilder lpClassName,int nMaxCount);
#endregion
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] internal class CREATESTRUCT
{
public IntPtr lpCreateParams;
public IntPtr hInstance;
public IntPtr hMenu;
public IntPtr hwndParent;
public int cy;
public int cx;
public int y;
public int x;
public long style;
public string lpszName;
public string lpszClass;
public int dwExStyle;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] internal struct CBT_CREATEWND
{
public IntPtr lpcs;
public IntPtr hwndInsertAfter;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -