(December 18, 2005)
I'm learning Avalon. I would like to access Avalon controls directly from C++/CLI code without separate XAML files. It's not straightforward, so I document steps to create such app here.
I used physical XP SP2 system (works under VMWare 5.0 too), Visual Studio 2005 Pro, WinFX November CTP Runtime Components (to run following Avalon apps on client computer it's enough to install only Runtime Components. Entire Runtime Components package (43 MB) is available at the bottom of the link).Screenshots were made under Windows Classic (aka gray) theme.
[System::STAThreadAttribute]
int main()
{
System::Windows::MessageBox::Show("Hello, Avalon");
}
Download solution.
namespace WinMedia = System::Windows::Media;
ref class MyCanvas : public System::Windows::Controls::Canvas{
protected:
virtual void OnRender(WinMedia::DrawingContext^ drawingContext) override
{
WinMedia::Brush^ brushForPen = gcnew WinMedia::SolidColorBrush(WinMedia::Colors::Blue);
WinMedia::Pen^ pen = gcnew WinMedia::Pen(brushForPen, 3);
System::Windows::Point p1(0, 0), p2(200, 200), p3(0, 200), p4(200, 0);
drawingContext->DrawLine(pen, p1, p2);
drawingContext->DrawLine(pen, p3, p4);
}
};
ref class MyWindow : public System::Windows::Window{
void SetBackgroundOrOnlyBlackWindowOnMyMachine(){
WinMedia::Imaging::RenderTargetBitmap^ bmp =
gcnew WinMedia::Imaging::RenderTargetBitmap(1, 1, 1, 1, WinMedia::PixelFormats::Default);
Background = gcnew WinMedia::ImageBrush(bmp);
}
public:
MyWindow(){
SetBackgroundOrOnlyBlackWindowOnMyMachine();
AddChild(gcnew MyCanvas());
Title = "Avalon Render";
}
};
[System::STAThreadAttribute]
int main()
{
System::Windows::Application^ app = gcnew System::Windows::Application();
app->Run(gcnew MyWindow());
}
Download solution.
Without SetBackground... workaround I have only black empty window. I read about it from Greg Kerr posting to microsoft.public.windows.developer.winfx.avalon newsgroup.
namespace WinMedia = System::Windows::Media;
ref class MyWindow : public System::Windows::Window{
void SetBackgroundOrOnlyBlackWindowOnMyMachine(){
WinMedia::Imaging::RenderTargetBitmap^ bmp =
gcnew WinMedia::Imaging::RenderTargetBitmap(1, 1, 1, 1, WinMedia::PixelFormats::Default);
Background = gcnew WinMedia::ImageBrush(bmp);
}
public:
MyWindow(){
SetBackgroundOrOnlyBlackWindowOnMyMachine();
AddChild(gcnew System::Windows::Controls::Button());
Title = "Avalon Button";
}
};
[System::STAThreadAttribute]
int main()
{
System::Windows::Application^ app = gcnew System::Windows::Application();
app->Run(gcnew MyWindow());
}
Download solution.
Copyright (C) 2005 Sergey Vlasov