到目前为止,我尝试通过SendMessage和其他几个调用使用GetThemePartSize,GetThemeMetric,GetSystemMetrics,WM_GETTITLEBARINFOEX,但没有任何内容甚至接近真实尺寸(启用了主题的 Windows 7). 基本上我的问题是:
基本上我的问题是:如何获得这些按钮的大小(和位置,理想情况下甚至处理)以及GetThemePartSize中检索到的值甚至意味着什么?它们有什么用?
我已经看过this answer和其他许多人,但他们根本不工作.
谢谢
更新
汉斯:
[DllImport("uxtheme", ExactSpelling=true)]
private extern static Int32 GetThemePartSize(IntPtr hTheme, IntPtr hdc, WindowPart part, WindowPartState state, ref RECT pRect, ThemeSize eSize, out SIZE size);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct SIZE
{
public int cx;
public int cy;
}
private const int TS_TRUE = 1;
private const int CBS_NORMAL = 1;
private const int WP_CLOSEBUTTON = 18;
private const string VSCLASS_WINDOW = "WINDOW";
/* inside a method */
var rect = new RECT {left = 0, right = 200, top = 0, bottom = 200};
var size = new SIZE();
var windowHandle = new WindowInteropHelper({MyWindow}).Handle;
var theme = OpenThemeData(windowHandle, VSCLASS_WINDOW);
GetThemePartSize(theme, null, WP_CLOSEBUTTON, CBS_NORMAL, ref rect, TS_TRUE, ref size);
// result on w7 with default theme -> size.cx == 28, size.cy == 17
在浏览Firefox 17.0.1源代码(即文件nsNativeThemeWin.cpp和nsUXThemeData.cpp)之后,我能够为在Vista或更新版本上运行的桌面应用程序提供部分解决方案,以满足我的需求:
private const int DWMWA_CAPTION_BUTTON_BOUNDS = 5;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("dwmapi.dll")]
public static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out RECT pvAttribute, int cbAttribute);
var windowHandle = new WindowInteropHelper(Main.Instance).Handle;
var buttonsRect = new RECT();
var sizeButtonsRect = Marshal.SizeOf(buttonsRect);
DwmGetWindowAttribute(windowHandle, DWMWA_CAPTION_BUTTON_BOUNDS, out buttonsRect, sizeButtonsRect);
buttonsRect中存储的结果有点奇怪:宽度差异(右 – 左= = 105)完全符合实际边界,但是,高度差(29px)比按钮实际占用的区域大8个像素.
由于这并没有真正回答任何问题,我将保留这个主题以供进一步回复.
再次感谢汉斯的宝贵意见和深思熟虑的评论.正如您可以清楚地看到的那样,它完全与编程语言和代码片段有关.
