从昨天开始,我已经对谷歌进行了相当多的谷歌搜索,但是没有发现它有多大意义。 我有一个文件,其中包含应用程序的设置,我认为正确阅读,但更改我的背景颜色的代码可能不是。
我正在尝试根据设置文件文件中的信息更改用于窗口背景的颜色。
窗口代码
<Window x:Class="WindowFrame.MainWindow" Name="WindowFrame"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowFrame"
ResizeMode="NoResize"
ShowInTaskbar="False"
Topmost="True"
Focusable="False"
IsHitTestVisible="False"
AllowsTransparency="True"
WindowStyle="None"
Width="{DynamicResource {x:Static SystemParameters.VirtualScreenWidthKey}}"
Height="{DynamicResource {x:Static SystemParameters.VirtualScreenHeightKey}}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="#7FAC0000"
WindowState="Maximized">
<Grid>
</Grid>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.IO;
using System.Diagnostics;
namespace Dusk_for_Windows
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
public MainWindow()
{
InitializeComponent();
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.txt");
string fileContent = File.ReadAllText(filePath);
string[] contentArray = fileContent.Split(':');
string contentColor;
string contentIntensity;
for (int i = 0; i < contentArray.Length; i++)
{
switch (i)
{
case 0:
{
if (contentArray[i].ToLower().Contains("color: "))
{
contentColor = contentArray[i].Replace("title: ", "");
if(contentColor == "light")
{
BrushConverter bc = new BrushConverter();
Brush brush = (Brush)bc.ConvertFrom("#C7DFFC");
brush.Freeze();
WindowFrame.Background = brush;
}
}
if (contentArray[i].ToLower().Contains("intensity: "))
{
contentIntensity = contentArray[i].Replace("intensity: ", "");
}
break;
}
}
}
}
public static class WindowsServices
{
const int WS_EX_TRANSPARENT = 0x00000020;
const int GWL_EXSTYLE = (-20);
public static void SetWindowExTransparent(IntPtr hwnd)
{
var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
}
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var hwnd = new WindowInteropHelper(this).Handle;
WindowsServices.SetWindowExTransparent(hwnd);
}
}
}
我相当确定第42行和第65行之间的代码实际上是正确读取文件,但如果不是,请告诉我。
正在阅读的文件如下所示:
color: light
intensity: 1