set value in C++/CX e.g. Box<Color>

79 views Asked by At

When I was setting the background colour of the TitleBar, I used ref new Color( ) because it is a Color^, but Color has no constructors to set the red colour. So I'm trying to change the R Field value of the BackgroundColor of the TitleBar directly, but it doesn't work.

I solved the problem with ColorHelper, or as Raymond Chen suggested, by treating Color as a regular variable and not boxing it. But I'm still not sure if I can't change the value of the variable once it is already Box.

    // change title bar color
    using Windows::UI::ColorHelper;
    using Windows::UI::Color;
    auto bar = ApplicationView::GetForCurrentView()->TitleBar;
    
    // no constrctor(int a,int r,int g,int b)
    // bar->BackgroundColor = ref new Color(255, 255, 0, 0);
    
    // error:
    // bar->BackgroundColor->Value.R = 255;

    // okay:
    bar->BackgroundColor = ColorHelper::FromArgb(255, 255, 0, 0);

    // okay:
    Color color;
    color.R = 255; color.A = 255; color.G = 255; color.B = 0;
    bar->BackgroundColor = color; // Color can set to Color^ (implicit)

Error:

using namespace Windows::UI;
Color^ color = ref new Color();
// error: lvalue required as left operand of assignment
color->Value.R = 255; 

R is writable: MSDN Color Struct

although code can run (below)

Color color2;
color2.R = 255;

but I still don't know how to set boxing value.

The Codes I already Try:

auto color = ref new Color();

// can't casting from Color* to Color
auto r2 = safe_cast<Color>(color); 
auto auto r3 = (Color)color;

// __this is not a member of Color 
// Value is not a member of Color
auto r4 = (Color)color->Value;

// can run, but color doesn't change
auto r5 = (Color)*color;
r5.B = 255;
auto& r6 = (Color)*color;
r6.B = 255;

Maybe someone can clear up my confusion.

0

There are 0 answers