![](/img/trans.png)
[英]Is there a Windows 3.11 WPF style? If not, how can I draw some?
[英]How to draw Windows Classic style window elements
我們在程序中創建了一些自定義的“窗口”,當啟用VisualStyles
,我們可以找到窗口的每個元素及其大小,並使用相應的渲染器自行繪制,包括最小化和關閉按鈕。
當VisualStyles
被禁用並且當前繪制我們自己的窗口時我們也想做同樣的事情,但它們非常難看。 在WinForms C#中是否可以繪制Windows經典風格的窗口? 我找到了ClassicBorderDecorator
但是它適用於WPF。
或者,如果不這樣做,我們怎樣才能得到我們以下列方式做的窗飾的像素大小:
// Get the height of the window caption.
if (SetRenderer(windowElements["windowCaption"]))
{
captionHeight = renderer.GetPartSize(graphics.Graphics, ThemeSizeType.True).Height;
}
// Get the thickness of the left, bottom,
// and right window frame.
if (SetRenderer(windowElements["windowLeft"]))
{
frameThickness = renderer.GetPartSize(graphics.Graphics, ThemeSizeType.True).Width;
}
Windows不提供經典風格的渲染器,您必須自己做飯。 使用SystemInformation類獲取指標Color.FromKnownColor()以獲取顏色。
唯一棘手的部分是讓框架按鈕看起來很好。 最好的辦法是使用字體中的字形而不是嘗試自己繪制它們。 Webdings字體非常適合這種情況。
我無法驗證它的匹配程度,我的機器啟動Windows 8並且不再支持經典風格。 否則強烈暗示你可能不應該投入太多時間進入這個:)一些示例代碼:
protected override void OnPaintBackground(PaintEventArgs e) {
base.OnPaintBackground(e);
var captionHeight = SystemInformation.CaptionHeight;
int border = SystemInformation.Border3DSize.Width;
var color1 = Color.FromKnownColor(activated ? KnownColor.ActiveCaption : KnownColor.InactiveCaption);
var color2 = Color.FromKnownColor(activated ? KnownColor.GradientActiveCaption : KnownColor.GradientInactiveCaption);
var captionrc = new Rectangle(0, 0, this.ClientSize.Width, captionHeight);
using (var brush = new LinearGradientBrush(captionrc, color1, color2, 0, false)) {
e.Graphics.FillRectangle(brush, captionrc);
}
int textx = border;
if (this.Icon != null) {
int height = SystemInformation.SmallIconSize.Height;
var iconrc = new Rectangle(border, (captionHeight - height)/2, height, height);
textx += height + border;
e.Graphics.DrawIcon(this.Icon, iconrc);
}
var color = Color.FromKnownColor(activated ? KnownColor.ActiveCaptionText : KnownColor.InactiveCaptionText);
using (var font = new Font(this.Font.FontFamily, SystemInformation.CaptionHeight - 4 * border, GraphicsUnit.Pixel)) {
TextRenderer.DrawText(e.Graphics, this.Text, font, new Point(textx, border), color);
}
using (var font = new Font(new FontFamily("Webdings"), captionHeight - 4 * border, GraphicsUnit.Pixel)) {
var glyphs = this.WindowState == FormWindowState.Maximized ? "\u0030\u0031\u0072" : "\u0030\u0031\u0072";
var width = TextRenderer.MeasureText(glyphs, font).Width;
TextRenderer.DrawText(e.Graphics, glyphs, font,
new Point(this.ClientSize.Width - width, border),
Color.FromKnownColor(KnownColor.WindowFrame));
}
}
在我的機器上看起來像這樣,並不完全丑陋:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.