How can I flip/rotate the label in C# Windows Forms?
I set the background image to my label.
At every time interval it moves three pixels to the right side. When it reaches the form end position I need the label to be flipped and turned back.
I have tried the following way, but I didn't get the solution.
private void timer1_Tick(object sender, EventArgs e){
if (label2.Location.X < this.Width)
label2.Location = new Point(label2.Location.X + incr, label2.Location.Y);
else
{
incr = -2;
label2.Location = new Point(label2.Location.X - 50, label2.Location.Y);
label1.Image.RotateFlip();
}
this.Refresh();
}
Create a class, newlabel, which can rotate its Text on any angle specified by the user.
extend label class& override paint method
You can use it by code or simply dragging from the ToolBox.
using System.Drawing;
class newLabel : System.Windows.Forms.Label
{
public int RotateAngle { get; set; }
public string NewText { get; set; }
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Brush b =new SolidBrush(this.ForeColor);
e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
e.Graphics.RotateTransform(this.RotateAngle);
e.Graphics.DrawString(this.NewText, this.Font,b , 0f, 0f);
base.OnPaint(e);
}
}
Now drag this custom control to be used into your form.
You have to set the below properties.
newlbl.Text = "";
newlbl.AutoSize = false;
newlbl.NewText = "ravindra";
newlbl.ForeColor = Color.Green;
newlbl.RotateAngle = -90;
Change angle as required by simply changing the RotateAngle property.
So...You can do this way:
1.Download this dll file : http://www.mediafire.com/download/hc16qezty0k6qnv/RotateLabel.dll
2.Go on your Visual Studio and open your solution
3.Now you need to go on Projects tab -> Add references... -> Then browse the
file you downloaded and simply add that file
4.Next step is to right click on ToolBox
5.After you done that you need to click on Choose Items
6.Again browse your downloaded file and add VerticalLabel
7.Then you can drag VerticalLabel from the Toolbox to your form.
That is it, its simple.
Hope that helped you i just translated this answer and made it simpler :)
Good luck,
Stralz
Related
I have this idea of this world map image and when I click a country on it, it shows information of that country in a MessageBox for example does anyone have an idea how to do that?
I have a rectangle and a button and when i click the button it shows the image in the rectangle but i thought if i use polygons to shape the country's but I'm a little stuck.
I would like to have every country apart and maybe that the borders light up when clicked
You can do this pretty easily using WPF:
Find a nice World Map in SVG format. I used this one from Wikipedia:
Download and install Inkscape then open the SVG you've just downloaded. Inkscape has a nice feature that makes it possible to save an SVG as a XAML.
Import the ViewBox from the XAML file into your WPF window/etc:
For each Path in the XAML you can add a MouseEnter/MouseLeave event handler or you can use the same one for all the Paths
Sample code:
private void CountryMouseEnter(object sender, MouseEventArgs e)
{
var path = sender as Path;
if (path != null)
{
path.Fill = new SolidColorBrush(Colors.Aqua);
}
}
private void Country_MouseLeave(object sender, MouseEventArgs e)
{
var path = sender as Path;
if (path != null)
{
path.Fill = new SolidColorBrush(Colors.Black);
}
}
Now it's just a matter of changing colors/showing MessageBoxes etc.
GitHub
My first thought: You could bind a command to the view that will be triggered by a click on a position. If you're using WPF you can bind command parameters to the command to get the x and y of your click...
After that you have to handle the content of your messagebox and the highlighting of the borders depending on the position xy.
have fun :D
Option 1
There is a project on Code Project that someone created that defines hotspots that are clickable with events. You could use that to overlay your map and define the hotspots where you need them.
C# Windows Forms ImageMap Control
Option 2
You could bind to the MouseUp Event on the Image and use the following to see if it is in a Rectangle
private void mapMouseUp(object sender, MouseEventArgs e) {
Rectangle rect1 = new Rectangle(0, 0, 100, 100);
Rectangle rect2 = new Rectangle(0, 100, 100, 100);
if (rect1.Contains(e.Location)) {
// Do your stuff
} else if (rect2.Contains(e.Location)) {
// Do your stuff
}
}
For a project, I have to make a Windows Forms Application that makes a Mandelbrotfigure. Now i want to add a vertical separator between the UI on the left and the Figure on the right (See Picture). Any idea on how to do that?
This is too late to answer this, but for the ease of anyone who ends up here searching:
As Line control deprecated, just use a Label control with AutoSize = false and Size = 1,xx with any BackColor you like your line to be.
Replace xx in the Size with actual height.
public class VertSep : Control
{
private Color lineColor;
private Pen linePen;
public VertSep()
{
this.LineColor = Color.LightGray;
SetStyle( ControlStyles.SupportsTransparentBackColor, true );
}
public Color LineColor
{
get
{
return this.lineColor;
}
set
{
this.lineColor = value;
this.linePen = new Pen( this.lineColor, 1 );
this.linePen.Alignment = PenAlignment.Inset;
Refresh();
}
}
protected override void Dispose( bool disposing )
{
if( disposing && this.linePen != null )
{
this.linePen.Dispose();
this.linePen = null;
}
base.Dispose( disposing );
}
protected override void OnPaint( PaintEventArgs e )
{
var g = e.Graphics;
int x = this.Width / 2;
g.DrawLine( linePen, x, 0, x, this.Height );
base.OnPaint( e );
}
}
You'll have to move all your contents inside a SplitContainer. This container contains two panels and a divider. You'll have to place the controls on one side and the image on the other panel.
In your particular case you'll probably have to start over.
Using the designer you should set the SplitContainer's Dock property on Fill. You can move the divider by selecting it and dragging (this is only possible if you have the SplitContainer selected). Once the divider is in the right place, it might be wise to set the FixedPanel property to the left panel as you probably don't want this panel to grow when maximizing.
It's still possible to move the divider in the application as long as the property IsSplitterFixed is set to False.
I wrote a custom Separator control just for this purpose. You can install it via the NuGet Package Manager Console:
Install-Package ALMSTWKND -Version 1.0.0
Just drag and drop it onto your Form from the Toolbox after installation.
There is a DataGridView in my form. When I click a cell in this DataGridView, it would show a dialog.
Form_Para formPara = new Form_Para();
formPara.ShowDialog();
In general, I hope this dialog just within screen and below this cell.
If showing below this cell will out of screen, then change its position to above the cell.
I know I can get the size and position of dialog and range of screen to calculate new dialog position. However, is there a simpler method?
For example, when I set 'StartPosition' of dialogs as 'CenterParent'.
It will show in the center of parent form and within screen automatically. I don't need to calculate the new position for avoiding out of screen.
Thanks a lot.
Finally, I think setting 'StartPosition' of dialogs as 'CenterParent' is simpler.
And then change only the Location.Y for avoiding to cover current cell.
Point currCellPosTop, currCellBottom;
currCellPosTop = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, true).Location;
currCellPosTop = dataGridView1.PointToScreen(currCellPosTop);
currCellBottom = new Point(currCellPosTop.X, currCellPosTop.Y );
currCellBottom.Y += dataGridView1.CurrentCell.Size.Height;
Form_Para formPara = new Form_Para(currCellPosTop, currCellBottom);
formPara.StartPosition = FormStartPosition.CenterParent;
formPara.ShowDialog();
Dialog Form:
private Point _TriggerControlPosTop;
private Point _TriggerControlPosBottom;
public Form_Para(Point triggerControlPosTop, Point triggerControlPosBottom)
{
_TriggerControlPosTop = triggerControlPosTop;
_TriggerControlPosBottom = triggerControlPosBottom;
InitializeComponent();
}
And visible change event of dialog:
private void Form_Para_VisibleChanged(object sender, EventArgs e)
{
this.Location = new Point(this.Location.X, _TriggerControlPosBottom.Y);
if (_TriggerControlPosBottom.Y + this.Height > Screen.PrimaryScreen.Bounds.Height)
{
this.Location = new Point(this.Location.X, _TriggerControlPosTop.Y - this.Height);
}
}
I am trying to make an application that is drawing Dendrograms like this one.
So I added a PictureBox to the winform, and for start, I wanted to wrote all labels like in the picture with this code:
foreach (var line1 in lines)
{
i++;
gpx.DrawString(line1, myFont, Brushes.Green, new PointF(2, 10 * i));
}
But the problem is that I have a lot of labels so it writes only a few of them on 800x600 px. I wanted to add scrolling bars, but it doesn't work at all. It works only when I am setting an Image to PictureBox.
Is there any other way, with or without PictureBox?
PictureBox is a very simple control, it is only good to display a picture. The one capability it doesn't have that you need is the ability to scroll the content. So don't use it.
Creating your own control is very simple in Winforms. A basic starting point is to begin with Panel, a control that supports scrolling, and derive your own class for it so you customize it to be suitable for the task. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto a form. Note how you can set the Lines property, either with the designer or your code. Use the Paint event to draw the dendrogram. Or extend the OnPaint() method in the class, you can make it as fancy as you want.
using System;
using System.Drawing;
using System.Windows.Forms;
class DendrogramViewer : Panel {
public DendrogramViewer() {
this.DoubleBuffered = this.ResizeRedraw = true;
this.BackColor = Color.FromKnownColor(KnownColor.Window);
}
public override System.Drawing.Font Font {
get { return base.Font; }
set { base.Font = value; setSize(); }
}
private int lines;
public int Lines {
get { return lines; }
set { lines = value; setSize(); }
}
private void setSize() {
var minheight = this.Font.Height * lines;
this.AutoScrollMinSize = new Size(0, minheight);
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
base.OnPaint(e);
}
}
Working in .NET 3.5.
Summary:
Trying to replicate functionality of an existing third party component, which breaks in Windows 7.
Until now the user could select a bunch of image files to print, specify a page size for each image and then send them off to print all in one go. I am in dire need of a conceptual explanation of how to go about printing switching the page size on the fly when printing each page.
Details
So far I have figured out how to print multiple images all with the same page size. I use a list of images and use a PrintDocument object, setting the HasMorePages property of the PrintPageEventArgs to true until I reach the end of the list.
Here's a class I quickly threw together to test this:
public partial class Form1 : Form
{
private List<Image> images { get; set; }
private PrintDocument printDocument { get; set; }
public Form1()
{
InitializeComponent();
this.images = new List<Image>();
this.images.Add(Image.FromFile(#"C:\test60.bmp"));
this.images.Add(Image.FromFile(#"C:\SuperLargeTest.jpg"));
this.printDocument = new PrintDocument()
{
PrinterSettings = new PrinterSettings()
};
this.printDocument.PrintPage += printDocument_PrintPage;
}
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
e.PageSettings.PaperSize = this.paperSizes[this.currentImageIndex];
RectangleF marginBounds = e.MarginBounds;
RectangleF printableArea = e.PageSettings.PrintableArea;
int availableWidth = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Width : (e.PageSettings.Landscape ? printableArea.Height : printableArea.Width));
int availableHeight = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Height : (e.PageSettings.Landscape ? printableArea.Width : printableArea.Height));
g.DrawRectangle(Pens.Red, 0, 0, availableWidth - 1, availableHeight - 1);
g.DrawImage(this.images[currentImageIndex], printableArea);
e.HasMorePages = ++currentImageIndex < this.images.Count();
}
private void button1_Click(object sender, EventArgs e)
{
this.printDocument.OriginAtMargins = false;
this.printDocument.Print();
}
}
The thing that I really can't figure out is how to go about changing the page size for, say, the second image.
If I wanted the first image to print in A4 and then the second one to print on A3, how would I go about doing that?
I found this SO question here which seemed to suggest changing the PageSize in the PrintPageEventArgs, but had no joy there.
I also tried to use the QueryPageSettingsEventArgs event and set the PageSettings there, but that didn't seem to work either...
What I would like to achieve is print multiple pages of different size as a single document. Any suggestions, links, explanations, sample code would be very much appreciated.
Anything in C# or VB.NET is fine.
That's work for me too.
Translated to C#:
private bool SetPaperSize(PrintDocument pd, PaperKind nKind)
{
foreach(System.Drawing.Printing.PaperSize ps in pd.PrinterSettings.PaperSizes)
{
if (ps.Kind == nKind)
{
pd.DefaultPageSettings.PaperSize = ps;
return true;
}
}
return false;
}
In VB.NET .. You can use this Sub ..
DocPrint is PrintDocument var
Sub SetPaperSize(ByVal nKind As PaperKind)
Dim ps As PaperSize
For ix As Integer = 0 To DocPrint.PrinterSettings.PaperSizes.Count - 1
If DocPrint.PrinterSettings.PaperSizes(ix).Kind = nKind Then
ps = DocPrint.PrinterSettings.PaperSizes(ix)
DocPrint.DefaultPageSettings.PaperSize = ps
End If
Next
End Sub
Hope this help ..
If you want all the pages to appear as one job (in short avoid being interleaved with other jobs), you can set the page size for the next page inside the PrintPage event handler by changing the default page size of the PrintDocument object.