• 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

    • Win11Debloat 2026.06.14 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 2026.06.14 changes: This is a minor release that hopefully addresses the false positives in Windows Defender and Bitdefender that prevented users from downloading and/or running Win11Debloat. Refactor Get-RegFileOperations.ps1 to address false positives by @Raphire in #626 Add logging around WinGet app retrieval and increase timeout to 20s by @Raphire Download: Win11Debloat 2026.06.14 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Still using Microsoft Money 2005 in 2026 here!
    • I have a couple to mention, and they still run great on Windows 11 Adobe Lightroom Version 2 Alcohol 120% CLZ Book, Comic, Game, Movie, & Music Collector (PC - No longer sold / Grandfathered in - now mobile apps/online only) DVDDecrypter ISO Buster Pro version 1.9.1 (Still supports HD-DVD too) Nero Burning Rom 8 (Only the burning software, no backup, media converter, etc)   OpenAL (Runtime) - GuildWars 1 Reforged still uses it for 3d headphone audio PowerDVD 12 Ultra SPTD (SCSI Pass through Direct Driver) UltraISO Windows Media Encoder 9 WinImage You can tell I still sport an optical drive    
    • Linux 7.1 arrives with an NTFS overhaul and major hardware performance boosts by Paul Hill The founder of the Linux kernel has just announced the availability of Linux 7.1. This is a stable version of the kernel that will now be tested by various Linux distributions before it is shipped to users through update managers. Some users, like those on Debian, for example, might not get it for a long time, if at all, while Fedora users can expect it in the near future. With Linux 7.1 out on time, the merge window for Linux 7.2 is now open, giving contributors the opportunity to send in major new features that have been waiting for the last two months. Torvalds warned that he is currently travelling and will be in another timezone, so timing for the merge window may be irregular due to timezone differences and limited internet access. Torvalds said that he has already fetched early pull requests to allow him to do some offline work, but the travel could still cause disruption. Right now, he is not planning to extend the release, but did consider it. He said he might later regret not extending, though. In terms of this last week of development for Linux 7.1, Torvalds said there were no major or alarming changes. This week consisted mostly of smaller driver updates to GPU, networking, and sound, networking fixes, trace tooling fixes, and misc minor fixes. The shortlog this week lists fixes for driver bugs, memory leaks, I/O and USB fixes, networking and RDMA fixes, DRM/graphics fixes, and tooling and verification improvements. Specific fixes include USB series heap-overflow and buffer overflow fixes, and multiple use-after-free, memory-leak, and refcount corrections across subsystems such as i2c, zram, gpio, and net. There are fixes for graphics drivers, including amdgpu, i915, and virtio, as well as hypervisor and virtualization tweaks affecting mshv, vmbus, and hyperv. According to Phoronix, anyone running Linux 7.1 should look out for the new NTFS driver, Intel FRED for improved performance on Panther Lake and future CPUs, faster graphics with Intel Arc Battlemage, and improvements for older AMD Radeon GPUs. If you are running Linux on your computer and everything is fine, then you don’t need to worry about updating to Linux 7.1 as a priority; just wait for it to be pushed to you. If you have tried Linux on hardware but it didn’t work properly, trying again with a distro that uses Linux 7.1 could cause Linux to work on your machine, thanks to the new hardware support.
    • you can also do this with this tool: PowerSettingsExplorer made by mbk1969 at 3dguru forum.. I found it by accident researching on modern standby and annoying quirks of it in 2022
  • 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
      507
    2. 2
      +Edouard
      197
    3. 3
      PsYcHoKiLLa
      139
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!