• 0

[C#, Win8 app] Create a drawable canvas


Question

Hello I am trying to make a paint app in this metro design language as an experiment. I want to create an array of just say 500X500 of objects. the object would store colour , layer , est. The mouse position would intersect with the grid object (pixel) then the colour would change depending on the colour picked. I can do this simple in game programming languages and SDKs. However how can I draw this colour pixel object using this new XAML mark-up stuff now.

I can create buttons within grids using XAML.

simply how can I create and draw lots of objects on the screen without giving each element an event hander such as and manually creating them in XAML thanks.

Link to comment
https://www.neowin.net/forum/topic/1156064-c-win8-app-create-a-drawable-canvas/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Ok, I will try and take it from a another angle.

I want to have a grid just say 5 X5 of rectangles set as the colour blue, you can do this in manually in XMAL.

I want a way to in code (c#) to create a grid of 100X100 rectangles all set the colour blue. To code this in XMAL would take ages. I want the size of the gird to be dynamic.

Then I want the colour of the rectangles to be set to green if the mouse runs over them. Currently the rectangles need an event handers. I want a way to acsess the colour of these rectangles without giving them an event handler.

In other languages (C# , XNA) I would do this ^^^^^ like this

int gridSide = 500;

for (int x = 0;x < gridSide;x++)
{
for (int y = 0; y < gridSide;y++)
{

grid[x,y].add(new rectangle(pos*x,pos*y,1,1, color.blue))
}
}


// to set the colours

If( mousePos.interscts with grid[x.y].collision)
{
grid[x,y].color = Color.green.;
}
[/CODE]

Its not 100% correct above but its on the right lines.

I just dont know, how i can create the layout in XAML dynamic in code instead of manually typing it out like this

[CODE]
<Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="150" RenderTransformOrigin="0.662,0.067" Canvas.Left="65">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Button x:Name="_Yellow" Content="Y" Width="50" Height="50" Click="YellowClicked" Grid.ColumnSpan="2" Grid.Column="1"/>
<Button x:Name="_Red" Content="R" Width="50" Height="50" Click="RedClicked"/>
<Button x:Name="_Blue" Content="B" Width="50" Height="50" Click="BlueClicked" Grid.Row="1"/>
</Grid>
[/CODE]

Giving each rectangle an event hander to determin an interaction (like above with an button) would be stupid for what im doing.

  • 0

ah ok,now i get it. you don't have to use XAML at all when youre doing dynamic stuff. i just wrote some code that you can try out.im pretty sure this is what youre looking for..

first make your grid in XAML, heres a snippet of mine

&lt;Grid x:Name="mygrid" HorizontalAlignment="Left" Height="600" Margin="372,84,0,0" VerticalAlignment="Top" Width="600" Background="#FF4F6F9E"/&gt;
&lt;/Grid&gt;

notice how i gave my grid a name of mygrid. this will be useful when we use code. theres no more XAML from here on out

first we tell it the size of our rows,and we add however many rows we want. notice we are adding them to mygrid.im making a 3x3 grid.

for (int i = 0; i &lt; 3; i++)
			{
				RowDefinition row = new RowDefinition();
				row.Height = new GridLength(200);
				mygrid.RowDefinitions.Add(row);
			}

now the same but our colums. again,3 here

for (int i = 0; i &lt; 3; i++)
			{
				ColumnDefinition col = new ColumnDefinition();
				col.Width = new GridLength(200);
				mygrid.ColumnDefinitions.Add(col);
			}

now we are going to create a button,and put it in a specific row and column, and we can dynamically change the text or color of this button in code

			Button button1 = new Button();
			Color b1color = Color.FromArgb(255,50,205,50);   //you have to add "using Windows.UI" at the top for this to work
			button1.Background = new SolidColorBrush(b1color);
			button1.Content = "Button 1";
			button1.Width = 200;
			button1.Height = 200;

			//add this button to our grid
			mygrid.Children.Add(button1);

			//now we specify which row and column to place our colored button
			//we are placing it at 0x0
			Grid.SetRow(button1, 0);
			Grid.SetColumn(button1, 0);

you can create a loop and make as many colored boxes as you want,and

now whenever we want to change the color in code, we can simply specify our color in the color structure,then simply doing

button1.Background = new SolidColorBrush(b1color);

if we want to change text,we do

button1.Content = "newtext";

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Samsung Galaxy Z Fold 8, Flip 8, Z Fold Wide: Everything you need to know The ONLY thing I need to know is the price, which I know will be way higher than I (and most people) are willing to pay for a phone... so basically nothing here I need to know. PS: Nice job getting that Apple reference to a non-existent and unrevealed product as "competition" in there. Cheque is in the mail.
    • Well I really think the repasting helped if your higher clocks have returned, maybe the next thing to look at is if there is a problem with your case airflow? I guess this because your 3080 has returned to optimal state, but is still staying too warm, which might suggest it was thermal throttling before you repasted, of which the only logical conclusion could be outside factors.
    • Samsung Galaxy Z Fold 8, Flip 8, Z Fold Wide: Everything you need to know by Hamid Ganji Galaxy Z Fold 7 - Image via Samsung The next generation of Samsung foldables is set to be unveiled next month at the second Unpacked event of the year. Samsung’s 2026 foldables are not expected to offer significant upgrades over their predecessors, with the Korean firm instead focusing on design refinements and conventional upgrades such as faster processors and better cameras. However, Samsung is reportedly planning to unveil an all-new passport-style foldable this year to rival Apple’s first foldable iPhone, which is expected to debut this September. Here’s a roundup of everything we know about Samsung’s upcoming foldable devices ahead of their official debut. When can we expect Samsung’s new foldables? The Galaxy Z Fold 7 and Z Flip 7 series were unveiled in July, and Samsung is expected to maintain this timeframe in 2026. Based on previous reports from Korean sources, Samsung will hold its Unpacked event on July 22 in London, UK, to pull back the curtain on the Galaxy Z Fold 8 series. The devices are also expected to hit the shelves a few weeks after launch. However, Samsung has yet to announce an official date. A new naming scheme? One of the most interesting changes we might see this year is a new naming scheme for Samsung’s latest foldables. SamMobile reported that since Samsung is expected to unveil three foldables this year, it has adopted a new naming strategy to simplify product identification for customers. Accordingly, the standard Galaxy Z Fold 8 will reportedly be called the Galaxy Z Fold 8 Ultra and will serve as the direct successor to last year’s Galaxy Z Fold 7. The “Ultra” suffix suggests the phone could feature higher-end specifications, such as additional rear camera modules. Samsung’s new passport-style foldable is expected to carry the Galaxy Z Fold 8 name without any suffix. This model is reportedly equipped with two rear cameras. No major changes are expected for the Flip model. Galaxy Z Fold 8 Ultra and Z Flip 8 anticipated specs Rumors over the past few months suggest Samsung is preparing several upgrades for its upcoming foldables, although the devices may continue to rely on larger batteries and faster charging speeds rather than dramatic design changes. The primary focus this year is expected to be the Galaxy Z Fold 8 and its wide-screen design. Galaxy Z Fold 8 Ultra official CAD renders - Image via AndroidHeadlines Here are the anticipated specifications for the Galaxy Z Fold 8 Ultra based on previous leaks: 6.5-inch outer display and 8-inch inner display, 120Hz refresh rate, and 2,600 nits peak brightness Snapdragon 8 Elite Gen 5 processor, paired with 12GB or 16GB of RAM and 256GB, 512GB, or 1TB of storage 4.1mm thickness when unfolded and a weight of 210g 200MP main camera, 50MP ultrawide camera, 10MP or 12MP telephoto camera, 10MP cover camera, and 10MP selfie camera 5,000mAh battery with 45W wired charging Android 17 and One UI 9 As for the Galaxy Z Flip 8, the device is not expected to be a major departure from its predecessor, although it could become slightly slimmer. Expected specifications include: Snapdragon 8 Elite Gen 5 or Exynos 2600 processor 12GB of RAM with 256GB and 512GB storage options 6.9-inch Dynamic AMOLED 2X inner dispaly and 4.1-inch Super AMOLED outer dispaly 50MP main camera, 12MP ultrawide camera, and 10MP selfie camera 4,300mAh battery with 25W wired charging Android 17 and One UI 9 Samsung’s foldables are also expected to launch with Gemini Intelligence, Google’s AI suite for automating tasks in Android ecosystem. Moreover, given current memory and component costs, some Galaxy Z Fold 8 Ultra and Z Flip 8 variants could see a price hike. Galaxy Z Fold 8 adopts a wide-screen design The centerpiece of the upcoming Unpacked event could be the Galaxy Z Fold 8, previously rumored as the Galaxy Z Fold Wide. This model adopts a passport-style form factor and is expected to compete directly with Apple’s iPhone Fold. Galaxy Z Fold 8 official CAD renders - Image via AndroidHeadlines Here’s what to expect: 7.6-inch primary OLED display and 5.4-inch cover display, 120Hz refresh rate, 2,600 nits peak brightness, and 4:3 aspect ratio Snapdragon 8 Elite Gen 5 processor, 12GB or 16GB of RAM, and 256GB, 512GB, or 1TB storage options 4,800mAh battery with 45W wired charging 50MP main camera, 50MP ultrawide camera, and 10MP selfie camera Android 17 and One UI 9 The three new foldable phones are unlikely to be the only devices unveiled at Samsung’s Unpacked event. The company is also expected to introduce the Galaxy Watch Ultra 2 and the Galaxy Watch 9 series.
    • Thanks
  • Recent Achievements

    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
    • One Month Later
      agatameier earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      505
    2. 2
      +Edouard
      196
    3. 3
      PsYcHoKiLLa
      141
    4. 4
      ATLien_0
      89
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!