How to get Windows Phone 7 Theme Colour with XNA - c#

I've been trying to use the guide available at http://geekswithblogs.net/mikebmcl/archive/2010/09/16/using-wp7-themes-in-your-xna-game.aspx but I cannot find the Application name, nor do I seem to be able to find a replacement for SolidColorBrush.
Unfortunately there is no library or easy to use code on the net to programmitcally get the tile colour in XNA on windows phone, even though its simple with Silverlight.
Any ideas how to go about this?

You can get the theme (dark/light) on the phone in a shorter way (works for XNA too):
Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
if(darkBackgroundVisibility == Visibility.Visible)
//Theme is Dark
else
//Theme is Light
To get the AccentColor, you need a but more code (I got it from this article on MSDN: How to: Apply Theme Resources for Windows Phone). I shortened the code from the switch-statement for readability and put it in a method. I also tested this in an XNA app and this works fine! :)
var currentAccentColorHex = (System.Windows.Media.Color)Application.Current.Resources["PhoneAccentColor"];
string currentAccentColor = ColorNameFromHex(currentAccentColorHex);
private string ColorNameFromHex(System.Windows.Media.Color hexColor)
{
switch(hexColor.ToString())
{
case "#FF1BA1E2": return "Blue";
case "#FFA05000": return "Brown";
case "#FF339933": return "Green";
case "#FFE671B8": return "Pink";
case "#FFA200FF": return "Purple";
case "#FFE51400": return "Red";
case "#FF00ABA9": return "Teal";
case "#FF8CBF26":
case "#FFA2C139": return "Lime";
case "#FFFF0097":
case "#FFD80073": return "Magenta";
case "#FFF09609": return "Mango";
default: return "custom eleventh color"; //Manufacturer color
}
}
Instead of returning a string containin 'Red' you could return a 'real' Color. For that you'll have to change return type of the method and the value.
Hope this helps!

You can get the current theme from the Resources for example getting the background color like this. In an App you could check this in the Application_Launching as well as Application_Activated to see if the theme changed while the App was in the background.
I'm pretty sure you can do a similar thing in an XNA game:
public enum PhoneTheme
{
Light,
Dark
};
public static PhoneTheme CurrentTheme { get; private set; }
Following in your activated/startup code:
string theme = Resources["PhoneBackgroundColor"].ToString();
CurrentTheme = theme == "#FF000000"
? PhoneTheme.Dark
: PhoneTheme.Light;

Related

Could a switch statement that switches resources be more generic?

I am working on a kiosk application that has a button to choose a language.
The button after that has a picture of the languages flag and it's name. Right now I am using a switch statement to set the image, but I would like to have it more generic so when adding languages I wouldn't have to change code everywhere. I am getting the image from my apps resources, and I would like to keep it that way.
The only way I can think of would be by retrieving the images from files and naming them according to the language string they are for. The language string is in this format "en-US". Also the language string is declared in a function before this one is called.
private void SetLanguageButton()
{
switch (language)
{
case "en-US":
buttonLanguage.Image = Properties.Resources.en_US;
break;
case "hu-HU":
buttonLanguage.Image = Properties.Resources.hu_HU;
break;
case "sk-SK":
buttonLanguage.Image = Properties.Resources.sk_SK;
break;
}
buttonLanguage.Text = resourceManager.GetString("languageName", cultureInfo);
}
If you have cultural-ized resources, you can get data as following
buttonLanguage.Image =
(Icon)Properties.Resources.ResourceManager.GetObject("langicon", cultureInfo);
buttonLanguage.Text =
Properties.Resources.ResourceManager.GetString("langname", cultureInfo);

Prompt user to answer boolean choice using Revit API in C#

I created a Revit plugin in C# that allow users totally new to 3D technology to choose a family, and insert it in their project. But right now the user does not have the choice between placing an object on the point anywhere or on a face. It's either one or the other.
Right now my code looks like this :
bool useSimpleInsertionPoint = false; //or true
bool useFaceReference = true; //or false
if (useSimpleInsertionPoint)
{
//my code for insertion on point here
}
if (useFaceReference)
{
//my code for face insertion here
}
What I would like to do is ask the user what does he want to do.
Does TaskDialog.Show would do the trick or is it something else ?
Thanks in advance.
Vincent's approach is good. The one thing that I like a little bit more is to use the CommandLink options with TaskDialog. This gives you the "big option" buttons to pick from, provides both an answer as well as an optional line of "explanation" about each answer.
The code looks like:
TaskDialog td = new TaskDialog("Decision");
td.MainContent = "What do you want to do?";
td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1,
"Use Simple Insertion Point",
"This option works for free-floating items");
td.AddCommandLink(TaskDialogCommandLinkId.CommandLink2,
"Use Face Reference",
"Use this option to place the family on a wall or other surface");
switch (td.Show())
{
case TaskDialogResult.CommandLink1:
// do the simple stuff
break;
case TaskDialogResult.CommandLink2:
// do the face reference
break;
default:
// handle any other case.
break;
}
This should do the trick:
TaskDialog dialog = new TaskDialog("Decision");
dialog.MainContent = "What do you want to do?";
dialog.AllowCancellation = true;
dialog.CommonButtons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No;
TaskDialogResult result = dialog.Show();
if(result == TaskDialogResult.Yes){
// Yes
TaskDialog.Show("yes", "YES!!");
}
else
{
// No
TaskDialog.Show("no", "NO!!");
}
Code tested and proved to work in a Revit macro in 2014 so should work fine anywhere else in an add-in as well.

How to parse an HTML color name to a SolidColorBrush in a Windows Store App

In .NET there is a class to cast a text HTML colour name to a Color (in this case "Red"):
Color col=(Color)ColorConverter.ConvertFromString("Red");
Brush brush=new SolidColorBrush(col);
(Which I took from here: Cast Color Name to SolidColorBrush)
This works for pretty much all the colours that can be found on wikipedia
Is there an equivalent class/library for Windows Store Apps that can do the same thing?
Try this
using System.Reflection;
public SolidColorBrush ColorStringToBrush(string name)
{
var property = typeof(Colors).GetRuntimeProperty(name);
if (property != null)
{
return new SolidColorBrush((Color)property.GetValue(null));
}
else
{
return null;
}
}

How to add customized component in C#?

I need to call acadcolor component from visual studio.when i add the component it's fine.
After I need to use that component so just I drag and drop that control to windows form visual studio automatically closed without passed any message.
Can anybody know how to add and how to work with acadcolor component from visual studio?
Thanks advance.
Here's an ADN article on just what Alex Filipovici was mentioning:
Use 64-bit ActiveX Component from a .NET Assembly
There are other alternatives too. Here's an ADN article replicating the control with WPF: WPF Implementation To Mimic Color Layer Controls.
You can also open the color dialog if it's to select a color. That's what I've done most recently:
using acColor = Autodesk.AutoCAD.Colors;
using acWindows = Autodesk.AutoCAD.Windows;
//...
public acColor.Color GetAutoCADColor()
{
acWindows.ColorDialog colorDialog = new acWindows.ColorDialog();
DialogResult dialogResult = new DialogResult();
dialogResult = colorDialog.ShowDialog();
switch (dialogResult)
{
case DialogResult.OK:
return colorDialog.Color;
case DialogResult.Cancel:
return Color.Empty.ConvertToAutoCADColor();
default:
return Color.Empty.ConvertToAutoCADColor();
}
}
Extension methods:
internal static class ColorExtensions
{
internal static Color ConvertToWindowsColor(this acColor.Color acColor)
{
return Color.FromArgb(acColor.ColorValue.ToArgb());
}
internal static acColor.Color ConvertToAutoCADColor(this Color winColor)
{
return acColor.Color.FromRgb(winColor.R, winColor.G, winColor.B);
}
}
Just a thought or two.

Get Aero Window Colour

I've made a pretty slick Windows 8-ish interface using WPF. It already turns out way better than I could wish for, but I was wondering the following:
Is it somehow possible to retrieve the current window colour set by the user? You know, you can set the Aero colour when you right-click the desktop... My plan is to use that colour for a couple of canvas elements on my GUI.
Thanks in advance!
The SystemColours class exists for this very purpose. You can bind directly to it like so
"{DynamicResource {x:Static SystemColors.WindowColorKey}}"
You can query the ColorizationColor registry key for this.
I've even went a step further and created a method to get the hexadecimal colour value, hope this helps you:
public void SomeMethod()
{
int argbColor = (int)Microsoft.Win32.Registry.GetValue(#"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM","ColorizationColor", null);
var color = System.Drawing.Color.FromArgb(argbColor);
string hexadecimalColor = ConverterToHex(color);
}
private static String ConverterToHex(System.Drawing.Color c)
{
return String.Format("#{0}{1}{2}", c.R.ToString("X2"), c.G.ToString("X2"), c.B.ToString("X2"));
}
I managed to get the correct colour using the following code:
Little sidenote: It has a small correction in it to ignore the alpha bit of the hex number, so I get the full color rather than the less saturated one.
string colorizationValue = string.Format("{0:x}", Microsoft.Win32.Registry.GetValue(#"HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", "ColorizationColor", "00000000"));
StringBuilder bl = new StringBuilder(colorizationValue);
bl[0] = 'd';
bl[1] = '9';
colorizationValue = bl.ToString();
BrushConverter bc = new BrushConverter();
Brush brush = (Brush)bc.ConvertFrom("#" + colorizationValue);
cvs_barColor.Background = brush;
I created an open-source library for this here which is also available on NuGet.
install-package aerocolor-wpf.AeroColor
After installing the package, you can refer to a DynamicResource called AeroColor and AeroBrush depending on what you need.
There's some setup code that's needed too, but it isn't much. Just put something in your Loaded event handler of the window, as seen below.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
AeroResourceInitializer.Initialize();
}
}
The neat thing about this library is that it installs a hook as well, which updates those resources as the actual Aero color changes in the system too. This means you don't have to handle that either, and if you use a DynamicResource to point to the color in your XAML instead of a StaticResource, WPF will automatically update the color in your UI as well.
Looks very cool when Windows 8 changes the Aero color transitionally and your color follows.

Categories

Resources