WPF 自定义 Chrome 窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15316592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Custom Chrome Window?
提问by Hunter Mitchell
i have been trying to make something along the lines of this...
我一直在尝试做一些类似的事情......
I have looked and looked and only found this article. I am having trouble integrating this into my application. I just started WPF today, so i am learning. I have downloded the window.Shell dLL. What else do i need? Thanks!
我看了又看,才找到这篇文章。我无法将其集成到我的应用程序中。我今天刚开始使用 WPF,所以我正在学习。我已经下载了 window.Shell dLL。我还需要什么?谢谢!
回答by RJ Moeller
If you are looking for a step-by-step guide on how to add this to your application I can give it a try; I just happened to need a bit of a brush-up for a small app, I liked this and gave it a try - it took me about 45 minutes to apply. Cool stuff actually!
如果您正在寻找有关如何将其添加到您的应用程序的分步指南,我可以尝试一下;我只是碰巧需要对一个小应用程序进行一些复习,我喜欢这个并尝试了一下 - 我花了大约 45 分钟来申请。其实很酷的东西!
First: Download the sourceapplication and extract it to your computer.
首先:下载源应用程序并将其解压缩到您的计算机。
In it you will find three subfolders. One with the sample application, one named Microsoft.Windows.Shell, one named CustomChromeLibrary. Copy the latter two to the root folder of yourproject map, addthem to your project map (add existing project) and, from your startup project, referencethem.
您会在其中找到三个子文件夹。一个带有示例应用程序,一个名为 Microsoft.Windows.Shell,一个名为 CustomChromeLibrary。将后两个复制到您的项目映射的根文件夹,将它们添加到您的项目映射(添加现有项目),并从您的启动项目中引用它们。
Now open the Window you want to apply CustomChromeLibrary to. You need to change the root from
现在打开要应用 CustomChromeLibrary 的窗口。您需要将根从
<Window>
to
到
<ccl:CustomChromeWindow>
, this is done by using this code as the document root:
,这是通过使用此代码作为文档根来完成的:
<ccl:CustomChromeWindow
Title="YourWindowTitle" Height="268" Width="733" ResizeMode="CanResize"
x:Class="YourNamespace.YourWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
xmlns:ccl="clr-namespace:CustomChromeLibrary;assembly=CustomChromeLibrary"
xmlns:local="clr-namespace:YourNamespace"
>
Pay attention to the last three lines in the sample. These need to be updated to reference the correct libraries; the last one actually referencing to YOUR namespace.
请注意示例中的最后三行。这些需要更新以引用正确的库;最后一个实际上引用了您的命名空间。
Next you need to update the source code of your window as this is still a simple Window and you will receive an error from it.
接下来您需要更新窗口的源代码,因为这仍然是一个简单的窗口,您将收到一个错误消息。
Change this
改变这个
public partial class YourWindow : Window
to this
对此
public partial class YourWindow : CustomChromeLibrary.CustomChromeWindow
You are already half way there!
你已经成功了一半!
Next you just need to create the objects for your window (title bar etc.). This is wonderfully done in the sample project No. 5; I did really just copy it.
接下来您只需要为您的窗口创建对象(标题栏等)。这在示例项目 5 中做得非常好;我真的只是复制它。
Take everything from
把一切从
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome
...
to here
到这里
<!--min/max/close buttons-->
<ccl:CaptionButtons/>
Now you can fill your Window like this
现在你可以像这样填充你的窗口
<Grid>
The content of your Window goes here
</Grid>
And close the xaml like this
并像这样关闭 xaml
</Grid>
</ccl:CustomChromeWindow>
Now, if you try to run this you will receive another error. There are still three files missing:
现在,如果您尝试运行它,您将收到另一个错误。仍然缺少三个文件:
The first one you need is a Microsoft file: CaptionButtonRectToMarginConverter.cs; you will also find it in the sample. Copy it to your project and add it (add existing file).
您需要的第一个文件是 Microsoft 文件:CaptionButtonRectToMarginConverter.cs;您还将在示例中找到它。将其复制到您的项目并添加它(添加现有文件)。
You need to make one change to it:
您需要对其进行一项更改:
namespace YourNamespace
{ ...
instead of the sample's namespace.
而不是示例的命名空间。
Finally you need the two xaml files that create the buttons: GlassButton.xaml and GlassIcon.xaml; they can be found in the "Resources" subfolder (and are referenced as resource dictionaries in the xaml). Copy the whole subfolder to your project and add the two files to your project (add existing file).
最后,您需要创建按钮的两个 xaml 文件:GlassButton.xaml 和 GlassIcon.xaml;它们可以在“Resources”子文件夹中找到(并且在 xaml 中被称为资源字典)。将整个子文件夹复制到您的项目并将这两个文件添加到您的项目(添加现有文件)。
Now you should finally be able to run your project.
现在您应该终于可以运行您的项目了。
Let's not forget this: Lots of kudos to gbahns, the author of the original article over at codeplex.com!
让我们不要忘记这一点:在 codeplex.com 上为原始文章的作者gbahns 点赞!
回答by Viv
There are quite a few implementations you can find for a custom chrome.
您可以为自定义 chrome 找到很多实现。
Another helper library I've seen to one you linked is
我见过你链接的另一个帮助库是
Read section 3. It can be setup with Nuget making it more easier to integrate for someone new.
阅读第 3 节。它可以使用 Nuget 进行设置,从而使新人更容易集成。
Also section 3.3 3.4 3.5 talk about customising and expanding the MetroWindowcontrol which gives you a custom chrome and also allows adding controls to the chrome title bar
另外第 3.3 3.4 3.5 节讨论自定义和扩展MetroWindow控件,它为您提供自定义镶边,还允许向镶边标题栏添加控件
回答by Chris Covert
Over in this stack overflow question:
在这个堆栈溢出问题中:
How can I add a button to the WPF caption bar while using custom window chrome?
I was asking about how to insert buttons into the title bar of my Custom Window Chrome window. The xaml example might be enough to help you get going.
我问的是如何将按钮插入到我的自定义窗口 Chrome 窗口的标题栏中。xaml 示例可能足以帮助您开始。
Other than that, I'm not sure what you are looking for.
除此之外,我不确定你在找什么。
Edit: The button style I have in that other post is a fairly simplistic button, but you should be able to replace it with any styling that you want.
编辑:我在另一篇文章中的按钮样式是一个相当简单的按钮,但您应该能够将其替换为您想要的任何样式。

