• 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

    • My father still uses a programme written in dbase3. Still manages to work with a little help from dosbox. 
    • Microsoft hides these secret Windows 11 performance boost settings available on every PC by Sayan Sen Windows enthusiasts often look for ways to extract as much performance out of their systems as possible, and it's often the case that they try and do so while trying to minimize the heat and power consumption. This is especially relevant in the case of mobile Windows PCs since laptops and notebooks tend to get hot and management of that heat and power is harder in such a form factor. As such users often turn to techniques like under-volting which can be used to squeeze out the maximum capabilities of a chip while also maintaining lowered power levels. There are official apps from AMD and Intel with the likes of Ryzen Master and XTU (Extreme Tuning Utility). While these are quite handy, most enthusiasts probably prefer to dig into the BIOS and play around with settings there like Curve Optimizer on Ryzen, which lets users set various frequency-voltage scaling values. These are essentially called P-States. If you are not familiar with them, Processor Power Management is done through Advanced Configuration and Power Interface (ACPI) P-states and C-states. While P-states or performance pwoer states handle CPU voltage-frequency scaling, C-states deal with CPU sleep states so that some of the CPU functions, which are not necessary at that moment, can be disabled. The P-states and C-states work together to make the processor run more efficiently. It helps the OS and apps determine which cores can be parked and which should be boosted. Of course not every user is an enthusiast or knows the technicalities and integrities of how things like overclocking or undervolting work. Thankfully for them Windows itself offers something pretty cool, though it is hidden by default on all systems. By default, Windows only has two P-States, "Minimum Processor State" and "Maximum Processor State." However, this can be changed with a Registry trick to expand the options under a secret "Processor performance boost mode" dropdown. This essentially enables the HWP or hardware P-States available on a device, and these are not controlled just by the OS itself as the underlying hardware gets involved too. In total there are five Processor Performance Boost Mode profiles that control how Windows requests and allows CPU turbo/boost behavior under the different power policies. They are: Disabled: In this mode, processor boosting is effectively turned off. The CPU will avoid entering turbo or boost frequencies and instead operate closer to its base frequency ceiling. This can significantly reduce power consumption and heat output, but at the cost of reduced burst performance and responsiveness in short workloads. Enabled: This is the standard behavior where boost functionality is allowed under normal conditions. The processor can opportunistically increase frequency when workload demands it, balancing performance gains with power and thermal constraints as managed by the system. Aggressive: Aggressive mode favors performance more heavily, allowing the CPU to enter higher boost states more readily and sustain them longer. This should in theory improve responsiveness under bursty or heavy workloads but increases power draw and thermal output compared to the default enabled behavior. Efficient Enabled: This mode still allows boosting, but with a stronger bias toward energy efficiency. The system attempts to use boost more selectively, avoiding unnecessary frequency spikes when the performance gain is marginal. Efficient Aggressive: This is a hybrid approach where boost is still performance-responsive, but the system continuously weighs efficiency more heavily than in Aggressive mode. It aims to deliver noticeable performance improvements while reducing wasted power in less demanding scenarios. Here's how to enable the Processor performance boost mode: Open Registry Editor: Press Win+R, type regedit, and click OK. Go to: HKLM\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\54533251-82be-4824-96c1-47b60b740d00\be337238-0d82-4146-a960-4f3749d470c7 (where HKLM stands for HKEY_LOCAL_MACHINE_) Modify the value of Attributes from 1 to 2 (you can find modify option by right-clicking) After that, exit Registry, you should now be able to see the new "Processor performance boost mode" dropdown menu: As you can see there are now five new P-States or CPPC states or power profile available that help define the boost mode processor setting on your PC. Wrapping it up here's a quick run-down of the settings as defined by Microsoft itself. Setting Description Disabled The corresponding P-state-based behaviour is disabled. Collaborative Processor Performance Control (CPPC) behaviour is disabled. Enabled The corresponding P-state-based behaviour is enabled. CPPC behaviour is Efficient Enabled. Aggressive The corresponding P-state-based behaviour is enabled. CPPC behaviour is Aggressive. Efficient Enabled The corresponding P-state-based behaviour is Efficient. CPPC behaviour is Efficient Enabled. Efficient Aggressive The corresponding P-state-based behaviour is Efficient. CPPC behaviour is Aggressive. Aggressive At Guaranteed Windows calculates the desired extra performance above the guaranteed performance level, and asks the processor to deliver that specific performance level. Efficient Aggressive At Guaranteed Windows always asks the processor to deliver the highest possible performance above the guaranteed performance level. In the next part we shall be comparing these settings to explore how much of a benefit or regression they can provide in terms of performance and power efficiency. If you decide to change the values on your system and are experiencing problems like crashes or an overheating PC, make sure to revert the steps back to the original state.
    • I think he means you haven't reviewed previous UFC games. Of course it doesn't matter... Every time you just report on something that involves the President even if just simply what happened you guys usually get accused of being anti-Trump. We live in fun times.
    • So how did you solve the problem? Disabling Secure Boot isn’t a solution.
  • Recent Achievements

    • 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
    • Week One Done
      agatameier earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      518
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      147
    4. 4
      ATLien_0
      93
    5. 5
      Steven P.
      77
  • Tell a friend

    Love Neowin? Tell a friend!