Is this a good case for use of RoutedCommand? - wpf

I have a WPF page that has 2 ContentControls on it. Both of the ContentControls have an image, one being much smaller than the other. When mouse over the larger image I want to show a zoomed in view on the smaller image. Something very similar to this: http://www.trekbikes.com/us/en/bikes/urban/soho/soho/.
I think I want the larger image control to send out something that actually contains an image - which the smaller image control would pick up and display. Would this be a good place to take advantage of RoutedCommands? Can I pass along an image like that?

RoutedCommands seem a bit misplaced in this case... you'll want the mouse to respond smoothly and the last thing you want are commands to be fired off here and there.
You're probably better off using a VisualBrush. While Ian Griffith's example here is a magnifying glass (an early canonical VisualBrush example in WPF) you could easily adapt it to show a portion of your image.

Related

Scale all screen elements with window vb.net wpf

First time working on a GUI project.. and first time doing work on Windows so apologies in advance if this is a really noob question.
I'm taking baby steps into windows programming starting with vb.net WPF. Working in Visual Studio Express 2012.
I'm trying to work out how I can scale all the elements in a window with the window itself.
So for example, I'd create a window, say 1280x720, and place some images in the window. Say one at the top and one in the corner. (this is a basic media based application)
When I resize that window, I want the entire window to scale with it, so image 1 & 2 will get larger if the window gets larger, however this has to happen proportionally so that if I make the window a lot bigger in one direction one image can't overlap the other. Imagine the window is an image and I'm trying to resize it. (The overlap thing is the closest I've gotten to getting this working in my current attempts).
The layout in produciton will be more complex, comprising of mediaelements (video), images, text etc and all must scale accordingly.
This isn't something the user interacts with and so there are no form elements etc, and so I don't need form fields etc to stay the same size throughout scaling. I just need everything to scale like I'm scaling a picture. If for example I displayed this 1280x720 (16:9) layout on a 1920x1080 screen, maximised it should look identical only larger.
Hoping someone can point me in the right direction with this.
What I've tried so far- the few articles I did find on google relating to this (I may well be searching the wrong things) lead me to put all the elements in a viewbox, this lead to the overlap I mentioned earlier.
Ideas ?
I think you could use ViewBox container. The basic idea is as follows: ViewBox scales its content just as if it was an image scaled. This seems to be the closest result to what you've described in your question. Just put a Grid with absolutely-sized columns and rows into the ViewBox and set its Stretch to be Uniform:
<Viewbox Stretch="Uniform">
<Grid>
<..>Your controls, MediaElements, etc
<Grid>
</Viewbox>
You could also combine it (or entirely replace) with (e.g.) Grid Container : it gives you an ability to specify cell width and size usign star-syntax which is similair to html's percent syntax.
Another way is to use the DockPanel.
All-in-all there are plenty ways to achieve something similair and the way to go largely depends on the nuances of your particulair requirements.
Have a look at This tutorial to see a good overview of WPF containers and how to use them.

Winforms semi-transparent PNG over semi-transparent PNG

I think I must be missing something obvious, but I'm unable to find this after several hours of searching. Is there no way to use a PictureBox or other control to contain an image with partial transparent/alpha-blended pixels, and place that over another image and have the blending be based on the image under it?
For example, this produces the results I want:
Place a panel on a form.
Add an OnPaint handler.
In the OnPaint handler draw 1 PNG, then draw another PNG over it, using Graphics.DrawImage for both.
This does not:
Place a PictureBox on a form and set it to a PNG.
Place another PictureBox on the form and set it to a PNG.
Place the 2nd picture box over the first.
...even if the 2nd picture box is just empty and has a background color of Transparent, it still covers the picture below it.
I've read this stems from all winform controls being windows, so by nature they aren't transparent.
...but even the 15 year old platform I'm migrating from, Borland's VCL, had several windowless controls, so it's hard to imaging winforms doesn't at least have some easy solution?
My first example above is one answer, true, but that adds a lot of work when you can only use one big panel and draw all of your "controls" inside of it. Much nicer if you can have separate controls with separate mouse events/etc. Even if not an image control, and a control I have to draw myself, that would be fine, as long as I can just put one image in each control. In VCL they called this a "paint box", just a rectangle area you could place on a form and draw whatever you want on it. Has it's own mouse events, Bounds, etc. If you don't draw anything in it, it is like it's not even there (100% transparent) other than the fact it still gets mouse events, so can be used as a "hot spot" or "target" as well.
The PictureBox control supports transparency well, just set its BackColor property to Transparent. Which will make the pixels of its Parent visible as the background.
The rub is that the designer won't let you make the 2nd picture box a child of the 1st one. All you need is a wee bit of code in the constructor to re-parent it. And give it a new Location since that is relative from the parent. Like this:
public Form1() {
InitializeComponent();
pictureBox1.Controls.Add(pictureBox2);
pictureBox2.Location = new Point(0, 0);
pictureBox2.BackColor = Color.Transparent;
}
Don't hesitate to use OnPaint() btw.
Sorry, I just found this... once I decided to Google for "winforms transparent panel" instead of the searches I was doing before, the TransPictureBox example show seems to do exactly what I need:
Transparency Problem by Overlapped PictureBox's at C#
Looks like there are 2 parts to it:
Set WS_EX_TRANSPARENT for the window style
Override the "draw background" method (or optionally could probably make the control style Opaque).

Remove all extra space around a button with an image?

Alright, try as I might, I cannot for the life of me get rid of this tiny little border around my buttons.
Edit: I should mention, in case I didn't make it clear, these are buttons with an image on them, set to flat with the button sized to the image.
Images below:
Number one, I can't for the life of me get these borders to GO AWAY. I've checked everything I can think of. They're:
flat
border 0
no margins
no padding
manually sized to the size of the image (75px)
in a table layout where the columns are all:
manually sized to the width of the image (75px)
borderless
Nothing seems to really "work" to get rid of these. If I size the columns down to be 74px instead of 75px, most of them go away, but a few remain. I've triple and quadruple checked the images, and they don't have anything that I can pick up on that should be causing this... no transparency around the borders, definitely no border that looks like that.
Which leads me to the second problem:
Settings button when dialog is small...
Settings button when dialog is stretched out.
Settings button is also in the same table layout panel.
I've checked all the settings on the table layout panel as well.. I can't find any padding or margin or anything settings that suggest this should be happening.
Does anyone have any experience with this? What am I missing..?
Simple solution: using directly a PictureBox as if it was a button. You can change your image on mouse over or mouse click.
Have you tried a Toolbar/strip/whatever it's called these days? Probably not going to help as I believe it pads on your behalf, but worth a shot.
In the end you can toss the buttons in the trash and write your own control. A single control that manages N buttons will work well here.
I don't understand your second problem. What's the problem? It'll be fixed if you roll your own control anyhow.
While not a fix for the spacing issue, as a workaround you can make that gray gradient currently "behind" the "tabs" and control panel image into a BackgroundImage for the TableLayoutPanel using BackgroundImageLayout of Stretch. While not fixing the spacing issue, it would make it unnoticeable.
Writing a winforms control has its challenges (experience speaking here). I would agree that that is whats needed however. Depending on your project you may consider using XAML and WPF. It provides that fine detail you seem to be looking for in you application.
There are ways to host XAML controls in a winform app, but if you went this route it would be best to create a native WPF application. The reverse is also true (winform controls in a WPF app).
Did you check if the image has transparent pixels around the graphic pixels you want?
May be a simple crop solution.

WPF, any easy way to work with different screen resolutions?

Given a WPF Application running full screen, a fair amount of controls some of which will animate from off screen to center. I was wondering if there are any special ways to save on the amount of time required to optimize an application for different screen resolutions?
For example, using Blend I've setup some text, which is originally off screen to scroll into view. Now in design mode the start positions are static. If resolution changes the start positions will obviously not be correct.
So I guess to correct this, during app startup. I need to
Check resolution
Move the text box to the desired start location
Adjust the storyboard as required, so the frames all have correct co-ordinates depending on the res of the screen.
I don't mind doing all of this, but if there is an easier way please share!
Thanks
In WPF layout of controls should be made in such way, that when size of window or content changes, controls automaticaly resize/reposition themselves to reflect this change.
This is highly affected how your layout is made, especialy by using specific panels, that do layout of their child elements.
This is also made obvious by using device-independent units for sizes, margins and sometimes positions of controls. And also allows different kind of zooming and scaling of whole UI without any need to redesign the whole thing.
If you are trying to position your controls absolutely, either by using Canvas panel or margins, your are doing it totaly wrong.
In WPF, scene is measured in abstract units, not pixels, and controls are freely scaled. There should be no problems to center something, or what?

Silverlight 4: how to highlight control on mouse over

My aim is to get fine control "animation" when it is mouse-over-ed. For example, I have a "map" of controls (game map that represent different type of terrain), each of them is an image with trees/rocks/hills on the green grass or water (lake or see) image of blue/cyan color. When user point any image with mouse it should get shiny: either get more bright background or get a shiny border.
It is hard to say what exactly I want to have (either background change or border), I would like to try each of them and see what is the most appropriate for me.
I am going to have a custom control (MapTile) that will represent a map tile. I know how to catch MouseEnter/MouseLeave events, but not sure how to change control style and if it is a good idea to work with control style in CodeBehind, probably there are better XAML-based solutions.
Could you please help with a solution that provide few goals:
Goal1: Add highlighted border around the control (it will be squares/rectangles, or circles; use what is easier) on mouse enter, remove border on move leave;
Goal2: Change some properties of my CustomControl (for example, background color).
Thank you very much!
1. How to han
You might find it easiest to get hold of Expression Blend and use it to create a custom template for your control.
The Learn Expression Blend page would be a good place to start. Look for tutorials on customising buttons and this is the same sort of thing that you want to do.
You need to use an attached behavior on your control. You don't need to learn Blend for this.
Check this one as an example, but you can search the site for Mouse Over for other examples.
http://gallery.expression.microsoft.com/en-us/MouseOver3D

Resources