• 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

    • Well I've done a grand total of nothing, and it now clocks between 2010mhz and 1995mhz (stock is 1710mhz) and hovers around 80c, warmer than it used to, but tolerable clocks seem to have returned. Thanks for all the advice on this thread. Will review the evidence and make a choice.
    • Audacious 4.6.1 by Razvan Serea Audacious is a lightweight, open-source audio player that emphasizes simplicity, performance, and sound quality. Designed for Linux, Windows, and macOS, it supports a wide range of audio formats, internet radio streaming, and playlist management. Users can customize the interface with Winamp-style skins or modern themes, making it flexible for different preferences. Audacious also includes an equalizer, advanced audio effects, and a plugin system for extending functionality. Its low resource usage makes it especially suitable for older computers or users who value efficiency without sacrificing playback quality. Audacious key features: High audio quality – delivers clean, gapless playback with minimal distortion. Wide format support – plays MP3, FLAC, Ogg Vorbis, AAC, WAV, WMA, and more. Internet radio streaming – supports Shoutcast, Icecast, and other online streams. Winamp skin support – classic, nostalgic look for users who prefer the old-school style. Modern GTK-based interface – clean, simple UI with a more modern feel. Customizable themes – change appearance through skins and themes. Advanced playlist management – organize, save, and edit playlists with ease. Equalizer – fine-tune audio output with a built-in graphical equalizer. Audio effects – built-in DSP options like crossfade, replay gain, and more. Plugin system – extend functionality with additional components. File metadata support – displays and organizes music based on tags. Drag-and-drop support – quickly add songs or playlists. Global hotkey support – control playback without switching windows. Bit-perfect output modes – bypass system mixers for pure audio output. ReplayGain support – normalizes track loudness automatically. Cue sheet support – play entire albums from a single audio file with .cue. MPRIS2 integration – integrates with Linux desktop environments for media controls. Advanced resampling options – adjust playback quality with different resampler settings. Gapless playback – seamless transition between tracks encoded properly. Crossfade plugin – blend one song into the next smoothly. Last.fm scrobbling plugin – track listening history online. Remote control support – control Audacious via command-line or scripts. Lyrics plugin – display song lyrics if available. Alarm / timer plugin – start or stop playback at set times. SOX resampler plugin – high-quality resampling for audiophiles. Spectrum analyzer / visualization plugins – visual feedback while playing music. Headphone crossfeed effect – simulates speaker listening for headphones. Customizable buffer size – tweak latency and playback smoothness. Audacious 4.6.1 changelog: Use XDG cache dir to store temporary files (#1817) Accept embedded lyrics in more cases (#1818) Bump .so and plugin ABI versions retrospectively (#1819) Include Georgian translation (#1820) Fix build on systems using musl instead of glibc (#1823) Download: Audacious 4.6.1 | 48.2 MB (Open Source) Download: Portable Audacious 4.6.1 | 69.8 MB View: Audacious Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I really wonder if this has to do with the built in VPN or "private DNS" of browsers that trip up legal requirements like cookie consent and Cloudflare (to avoid all the botnet attacks we get). And BTW some botnets still manage to get past Cloudflare, we are constantly having to tweak it to block malicious traffic that ultimately cause a DDoS.
  • 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
      197
    3. 3
      PsYcHoKiLLa
      142
    4. 4
      ATLien_0
      89
    5. 5
      Steven P.
      80
  • Tell a friend

    Love Neowin? Tell a friend!