The buttons in the application i've created, although set with custom images, are still Windows 7 themed (round borders, color gradience, etc). Is there a way to change them to a more windows classic or window 8 look? templates or anything that can be downloaded?
Basically you are limited to the properties provided by the windows forms button class that its shape depends on the environment (operating system version) you are running.
However, you still can customize your button shape by implementing your own CustomButton class that has the Button class as its base. In that class you have to override the painting methods (and possibly other methods) and using the passed paint event args graphics object to draw your prefered shape.
public class ExampleCustomButton : Button
{
protected override void OnPaintBackground(PaintEventArgs pevent)
{
using (Pen p = new Pen(Color.Yellow))
{
pevent.Graphics.DrawEllipse(p,
Left, Top, Width, Height); //for example
}
}
protected override void OnPaint(PaintEventArgs pevent)
{
using (Pen p = new Pen(Color.Yellow))
{
pevent.Graphics.DrawEllipse(p,
Left, Top, Width, Height); //for example
}
}
}
Please, do not forget to dispose IDisposable Graphics objects.
Good Luck
Related
In my code behind I used to render the bitmap from my grid with RenderTargetBitmap.
var renderBitmap = new RenderTargetBitmap();
await renderBitmap.RenderAsync(UIElement);
I want to use the MVVM pattern but now the RenderTargetBitmap class does not work anymore.
Now I'm trying to use the WinRT XAML Toolkit - Composition
var gridBitmap = await WriteableBitmapRenderExtensions.Render(Grid);
but then I get this error: Message "Unable to expand length of this stream beyond its capacity." string
Is there a other way to render this in MVVM? Maybe with SharpDX? Or Iam doing anything wrong?
The problem here is that rendering a UI element into a bitmap is really not a task for view model, as it is very View-specific. In this case then I would see no harm in putting this code in the page's code behind and making it possible to trigger this method from the view-model.
Depending on the MVVM framework you are using, you could trigger the function in different ways. First would be to make the page implement an interface like IRenderGrid with this one method, then add a new method to the view-model that will take IRenderGrid as parameter and store the instance for later use, and then in OnNavigatedTo of the page call this view model method.
interface IRenderGrid
{
Bitmap RenderGrid();
}
class MainViewModel
{
...
private IRenderGrid _renderGrid;
public void RegisterRenderGrid( IRenderGrid renderGrid )
{
_renderGrid = renderGrid;
}
}
class MainPage : Page, IRenderGrid
{
...
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var viewModel = DataContext as MainViewModel;//get the view model
viewModel.RegisterRenderGrid( this );
}
public Bitmap RenderGrid()
{
var renderBitmap = new RenderTargetBitmap();
await renderBitmap.RenderAsync(Grid);
}
}
Of course the problem is that if your view models reside in a different assembly, you will not have access to Bitmap there, so you will have to wrap it in some custom type.
Alternative approach to triggering the page's method would be to use a messenger, which many MVVM frameworks offer. In such case you would subscribe to a trigger message in the view and react to such message by executing the rendering and then pass the result to the view model either with another message or through a public method.
I'm making a custom TextBox for UWP to simplify Win2D outlined text solution, for that I created a UserControl that contains only a canvas on which I'll draw the text.
Of course I need some properties, like text, outline thickness and color, etc...
I also need some properties that are already exposed by the inherited UserControl like Foreground, FontSize, FontFamily...
So far so good, it seems like I won't need to implement each one of those common properties.
The problem is that I can't find a way to hook up an event when one of those properties changes, as I have to call the Canvas.Invalidate() method to redraw it when the format changes.
Looks like I have to hide all those properties and create new Dependency Properties to call Canvas.Invalidate().
There is no way to do it faster?
Nevermind, the answer was behind the corner.
In the constructor, you can call
RegisterPropertyChangedCallback(DependencyProperty dp, DependencyPropertyChangedCallback callback);
For example:
public OutlinedText()
{
InitializeComponent();
RegisterPropertyChangedCallback(FontFamilyProperty, OnPropertyChanged);
RegisterPropertyChangedCallback(FontSizeProperty, OnPropertyChanged);
}
private void OnPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
OutlinedText instance = sender as OutlinedText;
if (instance != null)
{
//Caching the value into CanvasTextFormat for faster drawn execution
if (dp == FontFamilyProperty)
instance.TextFormat.FontFamily = instance.FontFamily.Source;
else if (dp == FontSizeProperty)
instance.TextFormat.FontSize = (Single)instance.FontSize;
instance.needsResourceRecreation = true;
instance.canvas.Invalidate();
}
}
In the book "Agile Software Development: Principles, Patterns, and Practices", the "Single Responsibility Principle" section has an example about a class Rectangle which has more than one responsibility:
class Rectangle {
public void draw() { ... }
public double area() { ... }
}
And different classes may use only one of its methods:
class ComputationalGeometryApplication {
Rectangle rectangle;
public void someMethod() {
double area = rectangle.area();
}
}
class GraphicalApplication {
Rectangle rectangle;
public void someMethod() {
rectangle.draw();
}
}
(Note: The book takes c++ for example, and I use Java code here since I'm not familiar with c++)
I'm not clear about this sentence:
Second, if a change to the GraphicalApplication causes the Rectangle to change for some reason, that change may force us to rebuild, retest, and redeploy the ComputationalGeometryApplication.
Say if GraphicalApplication causes the draw() method to become draw(boolean), why will it force us to rebuild, retest, and redeploy the ComputationalGeometryApplication?
I mean,
ComputationalGeometryApplication can choose to use the old version of compiled file of Rectangle if we know the changes causes by GraphicalApplication is not related with itself
If ComputationalGeometryApplication wants to use the new version of Rectangle, yes, we should rebuild/retest/redeploy. But even if the draw() method is not in Rectangle, but in another class of same codebase, we still need to rebuild/retest/redeploy the ComputationalGeometryApplication if we want to use the new version of the library which contains Rectangle
I can't really understand the benefit, what am I missing?
Update: The referenced question did help me a lot, since there is a slight difference with the two questions, I want to add my understanding here.
The key is this sentence in the accepted answer of that question:
"If the GraphicalApplication requires a new method or change in semantics in the Rectangle class, then that affects the ComputationalGeometryApplication since they both "link" to the Rectangle library"
The keyword link reminds me the Rectangle is in a shared library. When it's changed by GraphicalApplication, the ComputationalGeometryApplication will automatically uses the new version of the Rectangle class. If the change to the Rectangle is not compatible with previous one, say, it adds some references of extra class in the constructor of Rectangle:
class Rectangle {
Rectangle() {
SomeExtraClass cls = new SomeExtraClass()
}
}
For ComputationalGeometryApplication, it will fail if we didn't rebuild it since it may not find the class SomeExtraClass when initialise the Rectangle class
I created a program that controls the mouse using the keyboard. I implement KeyListener to a class I called Keyboard, and then I create a JFrame called frame that adds this keyboard class. Here's part of the code to explain what I'm doing:
class Keyboard implements KeyListener
{
//Overrides key methods
}
class MainClass
{
public static void main(String [] args)
{
JFrame frame = new JFrame();
frame.setSize(width, height);
frame.addKeyListener(new Keyboard());
frame.setVisible(true);
}
}
My problem is I want the program to run when the frame is not selected (or even better, without the frame). For example, when I select another window of another program (a browser, Word, etc) the mouse won't move when I use the keyboard. I have to select my program window to control the mouse.
How can I achieve this?
Thanks.
In draw2d,How can I draw a figure without having any border?
How to implements the CustomBorder for rectangles to remove the border?
I know if we implement a class which extends Border, and in the paint method what should I do to remove the border?
Figures don't have a border unless you explicitly set one by calling setBorder(..). If you just want a blank figure that doesn't draw anything, then new Figure() will give you just that. There's no need to implement any custom borders or figures. If you are using a Rectangle then that's exactly what you will get: a rectangle; which is what you probably confused for a border.
You can disable the border with figure.setBorder(null); or you can put it in the constructor:
public static class BorderlessFigure extends Figure {
public BorderlessFigure() {
ToolbarLayout layout = new ToolbarLayout();
setLayoutManager(layout);
setBorder(null);
add(new Label("test"));
}
}
If you want a Border that does not paint anything you can extend org.eclipse.draw2d.AbstractBorder:
public class NoBorderBorder extends AbstractBorder {
#Override
public void paint(IFigure f, Graphics g, Insets i) { }
#Override
public Insets getInsets(IFigure f) {
return new Insets(0);
}
}
I don't know why would you do that though.