Hidden background image C# winform in right to left layout? - c#

I set background image to form in my project C# winform, but when set Form property to
RightToLeft=Yes and RightToLeftLayout=True
then disappear my background image.
Does anyone help me?

You can paint the image manually, by overriding the OnPaintBackground method of your form:
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage(Properties.Resources.SampleImage,
new Rectangle(Point.Empty, this.ClientSize));
}

By using background image, you have to know that it is not supported by RightToLeftLayout so you cannot use it directly in this case, but that doesn't mean that you can't implement it manually.
MSDN Reference

Owner draw is not supported when RightToLeftLayout is set to Yes. The owner draw events will still occur, but the behavior of any code you author in these events is not defined. Additionally, BackgroundImage, Opacity, TransparencyKey, and the painting events are not supported.
Referece:
[http://msdn.microsoft.com/en-us/library/system.windows.forms.form.righttoleftlayout(v=vs.110).aspx][1]

Look this:
http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=ZH-CN&k=k(System.Windows.Forms.Form.RightToLeftLayout)%3bk(TargetFrameworkMoniker-.NETFramework%2cVersion%3dv4.5)%3bk(DevLang-csharp)&rd=true
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.backgroundimage(v=vs.110).aspx
If you set RightToLeftLayout=True ,you'll not use backgroundimage.

Related

c# Override high-contrast for background image

I wasn't able to find an answer for this:
I have a background image on my form that I want to remain visible even when the system is on High Contrast mode. Is there code that can be entered that overrides the HC mode?
I've tried this on the Form Load event but no luck- no definition for graphics. (Not sure this would even be a viable solution):
OnPaint: e.Graphics.DrawImage(new Bitmap(BackgroundImage), 0, 0);
Aside from creating a PictureBox across my form and putting the image that way, does anyone know of a way to show the BG image of the form always?
Override the OnPaintBackground method:
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage(new Bitmap(BackgroundImage), e.ClipRectangle);
}
This DrawImage overload will stretch the image to fit the rectangle. If the ClipRectangle doesn't work (sorry, I can't test this right now!), create a new Rectangle with the background dimensions

Position Background Image

I was wondering if there is any possible way to position a background image on a button in C# windows forms? I am using Visual Studio 2013, and I noticed that you can use the BackgroundImageLayout but that is very limited. I would like to move the background image around by pixel position, or relative to the button. Kind of like this:
I have been on google for a while now with no luck. If anyone could point me in the right direction, or show my an article to read it would be greatly appreciated. Thank you.
You could use the Paint event (or subclass Button to override OnPaint) to draw the image yourself:
private void myButton_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(myImage, myButton.ClientRectangle);
}
You could then use the TextAlign and Padding properties to control the location of the text.
Note that you should not assign the image to the Button's Image or BackgroundImage properties, otherwise .NET will also render the image.

Drawing an image onto a Panel control gives artefacts when resizing

Currently I'm trying to do what I thought would be a simple task:
Draw an image onto the full area of a Panel control in Windows Forms. (Please ignore for the moment that I could use the BackgroundImage property)
The image to draw looks like this:
I.e. a yellow box with an 1 pixel blue frame around.
To draw, I'm using the Paint event of the Panel control:
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(Resources.MyImage, panel1.ClientRectangle);
}
This looks fine when initially displaying the form:
When resizing the form (and the docked panel, too), it either cuts the edges when being made smaller...
...or it draws artefacts, when being made larger:
I'm pretty sure that there is going on something rather simple and straight-forward but I really cannot understand the reason.
Since I'm ignoring the ClipRectangle and always draw everything, I thought the image would be scaled all the time.
My questions are:
What is the reason for the artefacts? (I love to understand this!)
What do I have to do in order to get rid of the artefacts? (beside calling Invalidate on each resize)
Update, SOLUTION:
Thanks to Ryan's answer, I was able to find an acceptable solution. Basically I derived a class from Panel, did an override of OnPaintBackground and did not call the base method. Last, I added the following code to the constructor of my derived panel:
base.DoubleBuffered = true;
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();
The reason for the artefacts is that the entire surface isn't redrawn when the form is resized; only the necessary parts are. The generally best solution is what you don't want to do, calling Invalidate on each resize. However, if this is in fact your situation, just use a PictureBox instead. If it's not, you might consider overriding OnPaint in your form instead, and using this.SetStyle(ControlStyles.ResizeRedraw, true) to do this automatically.

Gradient Drawing Bug

I'm having trouble with a gradient drawing call. I have a Form that looks like this.
Screenshot http://img413.imageshack.us/img413/3570/30364682.png
The problem is every now and again the above gradient drawing bug will start happening. It should go right across of course. Sometimes it only takes some build-rebuild-mashing to fix and it'll simply just "start" after a build every now and again.
That control (the top white part) is a TableLayoutPanel. The BackColor is set to white and on the panel's Paint event I do this:
/// <summary>
/// Draws the background gradient.
/// </summary>
private void titleBarLayoutPanel_Paint(object sender, PaintEventArgs e)
{
Brush brush = new LinearGradientBrush(titleBarLayoutPanel.Bounds, TaskHeaderLeftColor, TaskHeaderRightColor, LinearGradientMode.Horizontal);
e.Graphics.FillRectangle(brush, titleBarLayoutPanel.Bounds);
}
Should I be doing something else? The problem is that it works, and then without so much as a rebuild or build this will start happening!
EDIT I have since rebuilt the class library it is contained in (it's a generic Form) then rebuilt the app it's used in and the gradient is now filling across completely. This is bizarre.
Building and re-building your application, with no changes, normally doesn't solve this (or most any other bug for that matter) save the ones in which you run your application without doing a clean/rebuild first and then notice that the code you just wrote doesn't run (not sure that's possible these days with the IDEs). I see this a lot with newer devs when they keep rebuilding hoping that somehow the compiler will make the code "correct" or that maybe the compiler is simply not generating the correct code to begin with. (Please note that I do not mean the aforementioned statements to be taken disparagingly.)
To solve the issue at hand, you might try deriving your own TableLayoutPanel class in which you override the OnBackgroundPaint event, painting your own background, or simply returning if you don't want to paint your own background. (You seem to be painting the background in the Paint event). What you are doing in the code above is simply painting over the background already painted by the control, hence the "bug" you see, (double paint). It appears that the form is not resizable. Try making it resizable. Then resize it and watch it paint, or simply move other windows over it.
class CustomTableLayoutPanel : TableLayoutPanel
{
protected override void OnPaintBackground(PaintEventArgs e)
{
Brush brush = new LinearGradientBrush(this.ClientRectangle, TaskHeaderLeftColor, TaskHeaderRightColor, LinearGradientMode.Horizontal);
e.Graphics.FillRectangle(brush, this.ClientRectangle);
//base.OnPaintBackground(e);
}
}
By the way, you should replace Bounds with ClientRectangle.
Bounds is the control's rectangle relative to its parent; ClientRectangle is relative to the control itself.
In this particular case, it won't make a difference, since the control is at 0, 0.

RightToLeft property in Form in C#

I want to move the form title, icon and close, and help buttons from left side to right side (change the layout).
I moved the form controls manually to keep background image but now I want to change the form title.
When I set rightToLeft property to yes and rightToLeftLayout to true in the form properties the background image disappears, but it uses the property "BackColor"
My code is as follows:
if (_lang == 'Arabic')
{
this.RightToLeft = RightToLeft.Yes;
this.RightToLeftLayout = true;
}
But it keeps buttons image.
So why is that?
To further Blounty's answer, the MSDN specs clearly state that BackgroundImage, Opacity and others aren't supported when using RightToLeftLayout:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.righttoleftlayout(vs.80).aspx:
Owner draw is not supported when RightToLeftLayout is set to Yes. The owner draw events will still occur, but the behavior of any code you author in these events is not defined. Additionally, BackgroundImage, Opacity, TransparencyKey, and the painting events are not supported.
BackgroundImage, Opacity, TransparencyKey, and the painting events are not supported when RightToLeftLayout is set to yes.
It is pretty easy to replace the lost functionality:
protected override void OnPaintBackground(PaintEventArgs e) {
Rectangle rc = new Rectangle(Point.Empty, this.ClientSize);
e.Graphics.DrawImage(Properties.Resources.SampleImage, rc);
}
You'll need to do a bit more work if you need to tile the image.

Categories