JPanel resize and scroll errors - resize

I use a text area with variable hight and some other things (a combo box in the sample code) filling the window width.
The following error happens:
- When using "innerPanel.setPreferredSize", the scroller does not appear when the text area becomes higher than the window by typing line feeds into the text area or when resizing the window vertically.
- When not using "innerPanel.setPreferredSize", (as shown commented in the sample code below), the used swing elements enlarge horizontally when resizing the window width, but do never shrink horizontally.
JPanel innerPanel = new JPanel(new GridBagLayout());
JTextArea editArea = new JTextArea();
editArea.setLineWrap(true);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.;
innerPanel.add(editArea, gbc);
JComboBox combo = new JComboBox();
gbc.gridy = 1;
innerPanel.add(combo, gbc);
JScrollPane scroller = new JScrollPane(innerPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//innerPanel.setPreferredSize(new Dimension(50, 50));
JPanel outerPanel = new JPanel(new BorderLayout());
outerPanel.add(scroller);
frame.setContentPane(outerPanel);

Related

Place Text in Image on Edges

This tool I wrote in Visual Basic 2010 should add an author text to images. The user is able to set the font opacity and position. To make things easier I wanted some position presets as one can see in the bottom right corner. The calculation I am using is (bottom right in this case:
Dim textSize As Size = TextRenderer.MeasureText(tagString + curText, curFont)
tmpPos = New Point(srcImg.Width - textSize.Width - 10, srcImg.Height - textSize.Height - 10)
As you can see this works perfectly for this example picture. Where as on some the text just clips out.
First One: 1024x768 | Detected Font Size: 680x72
Second One: 1688x1125 | Detected Font Size: 680x72
I suspect this has something to do with the aspect ratio of the images but I do not know how to fix it.
The text is drawn like that:
brush = New SolidBrush(color.FromArgb(alpha, color))
gr = Graphics.FromImage(editImg)
gr.DrawString(tagString + text, font, brush, pos)
HauptBild.Image = editImg
I found this http://www.codeproject.com/Articles/20923/Mouse-Position-over-Image-in-a-PictureBox and it answered my questions.
is this problem only occuring in your preview or also in the converted File? Please post the Code how you save the New Image. I think you have Set a sizemode in your picturebox which is the Problem. Try it without the sizemode.
Will be better to see more your code, but, as i understand by TextRenderer class it is System.Windows.Forms. Just do not use Graphics, created from control (i suppose it is pictureBox with sizemode:Zoom), use Graphics, created from your image instead.
Here is code (sorry, C#), which loads image from file, draws text starting from the same coordinate and places on puctureBox1. Text always starts from Point(100,100).
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Image files|*.jpeg;*.png;*.jpg;*.gif;*.bmp";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
Bitmap orig=(Bitmap)Bitmap.FromFile(openFileDialog1.FileName);
//workaround for images with color table, see remarks here https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage(v=vs.110).aspx
Bitmap bmp=orig.Clone(new Rectangle(0, 0, orig.Width, orig.Height), System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("hello", new Font(this.Font.FontFamily,30,FontStyle.Bold ) , new System.Drawing.SolidBrush(System.Drawing.Color.Yellow ), new Point(100, 100));
this.pictureBox1.Image = bmp;
orig.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("Something goes wrong: " + ex.Message+ "\\n"+ ex.StackTrace );
}
}

Keep Anchored Buttons Below Autosized Label

I have a Windows form with an image, a label that contains an error message, and a "Close" button:
The error message could be very short or very long depending on what the error is. When it's long, I want the window to grow in height to display the whole message. Using the label's Anchor and MaximumSize properties and the form's AutoSize property, I was able to get the Label to expand to the maximum width and then expand vertically until the message is displayed. Setting the button's Anchor property to Bottom, Right locks the button to the bottom right corner as intended but the form only expands far enough to display the label and not enough for the button too. So the button is displayed behind the label and can't be seen:
I'd like to know how I can stretch the form to fully contain the text of the label and still leave room at the bottom for the anchored button to produce something like this:
Add 3 panels to your form docked left, bottom and fill. Set the properties as indicated in the image below.
Then set the Label's maximum width to a fixed value in the properties or you can calculate it at run-time:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Label1.MaximumSize = New Size(Me.panelFill.Width, 0)
End Sub
The easiest way is to build the layout with 3 docked panels, like this (hope you can adjust for your needs):
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form { Padding = new Padding(8), AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true, Font = new Font("Consolas", 9, FontStyle.Bold) };
var contentPanel = new Panel { Dock = DockStyle.Fill, Parent = form, AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true };
var imagePanel = new Panel { Dock = DockStyle.Left, Parent = form };
var buttonPanel = new Panel { Dock = DockStyle.Bottom, Parent = form };
var image = new PictureBox { BackColor = Color.Red, Width = 32, Height = 32, Parent = imagePanel };
imagePanel.Width = image.Width + 8;
var button = new Button { Top = 8, AutoSize = true, BackColor = Color.Green, ForeColor = Color.White, Text = "Test", Parent = buttonPanel };
button.Left = buttonPanel.DisplayRectangle.Right - button.Width;
buttonPanel.Height = button.Height + 8;
button.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
var label = new Label { Dock = DockStyle.Fill, BackColor = Color.Blue, ForeColor = Color.White, MaximumSize = new Size(300, 0), AutoSize = true, Parent = contentPanel };
label.Text = #"The error message could be very short or very long depending on what the error is. When it's long, I want the window to grow in height to display the whole message. Using the label's Anchor and MaximumSize properties and the form's AutoSize property, I was able to get the Label to expand to the maximum width and then expand vertically until the message is displayed. Setting the button's Anchor property to Bottom, Right locks the button to the bottom right corner as intended but the form only expands far enough to display the label and not enough for the button too. So the button is displayed behind the label and can't be seen:";
Application.Run(form);
}
}
}
Result:

Adding Image to Xaml not rendering with given margin

for (int i = 0; i < length; i++)
{
Image img1 ;
img1 = null;
img1= new Image();
img1.Height = snhei;
img1.Width = snwid;
BitmapImage BitImg = new BitmapImage(new Uri("/Assets/midtail.png", UriKind.Relative));`
img1.VerticalAlignment = VerticalAlignment.Top;
img1.HorizontalAlignment = HorizontalAlignment.Left;
img1.Source = BitImg;
img1.Stretch = Stretch.Fill;
img1.Name = "mid" + i.ToString() ;
img1.Margin = new Thickness(image_width*i, 0, 0, 0);
stackp.Children.Add(img1);
}
after running i get first image at 0,0
then it is render the one image_width below
The problem is your container. The StackPanel will, as it name indicates, try to stack the images. Then you apply the margin, moving the picture farther away.
You have two solutions, depending on what you want:
If you just want the images to be displayed side by side, set the Orientation property of your StackPanel to Horizontal. Then, remove your line of code that sets the margin, since the positioning is automatically handled by the StackPanel
If you still want to position the pictures manually, then you have to use another type of container. Replace your StackPanel by a Canvas or a Grid.

WP RichTextBox Vertical Alignment

I'm dynamically parsing data and adding text as Run, Hyperlinks and images as InlineUIContainer into a Windows Phone 8.0 RichTextBox. Somehow I can't manage that the images vertically align centered with the text.
Images are added like this:
Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);
var img = new Image
{
Stretch = Stretch.Uniform,
Source = imageSource,
VerticalAlignment = VerticalAlignment.Center,
Height = inlineImageSize,
};
paragraph.Inlines.Add(new InlineUIContainer {Child = img});
And text like that:
Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);
paragraph.Inlines.Add(new Run { Text = text });
I tried to set a few values for alignment on the RichTextBox as well, but the text is never centered with the images. The text is always bottom aligned.
Any chance getting the inline images vertically centered with the inline text in the WP RichTextBox?
I think what you are looking for is the BaselineAlignment Property.
try the following :
Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);
var img = new Image
{
Stretch = Stretch.Uniform,
Source = imageSource,
BaselineAlignment = BaselineAlignment.Center,
Height = inlineImageSize,
};
paragraph.Inlines.Add(new InlineUIContainer {Child = img});
Sorry for late reply. Try to set Margin for your Inline Images:
Paragraph paragraph = new Paragraph();
richTextBox.Blocks.Add(paragraph);
var img = new Image
{
Stretch = Stretch.Uniform,
Source = imageSource,
Height = inlineImageSize,
Margin = new Thickness(0,0,0,-5);
};
paragraph.Inlines.Add(new InlineUIContainer {Child = img});

How to position tabs on a tabcontrol vb.net

VB.Net, TabStrip control,
Tabs are Align Left or Right and Top or Bottom
I need to start tabs from my own position On Top of the Tab control
Like Internet Explorer, Tabs are starting after HTTP address box, but it will cover full page and Align on the left=0.
To display right-aligned tabs
Add a TabControl to your form.
Set the Alignment property to Right.
Set the SizeMode property to Fixed, so that all tabs are the same width.
Set the ItemSize property to the preferred fixed size for the tabs. Keep in mind that the ItemSize property behaves as though the tabs were on top, although they are right-aligned. As a result, in order to make the tabs wider, you must change the Height property, and in order to make them taller, you must change the Width property.
In the code example below, Width is set to 25 and Height is set to 150.
Set the DrawMode property to OwnerDrawFixed.
Define a handler for the DrawItem event of TabControl that renders the text from left to right.
C#
public Form1()
{
// Remove this call if you do not program using Visual Studio.
InitializeComponent();
tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
}
private void tabControl1_DrawItem(Object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Brush _textBrush;
// Get the item from the collection.
TabPage _tabPage = tabControl1.TabPages[e.Index];
// Get the real bounds for the tab rectangle.
Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
if (e.State == DrawItemState.Selected)
{
// Draw a different background color, and don't paint a focus rectangle.
_textBrush = new SolidBrush(Color.Red);
g.FillRectangle(Brushes.Gray, e.Bounds);
}
else
{
_textBrush = new System.Drawing.SolidBrush(e.ForeColor);
e.DrawBackground();
}
// Use our own font.
Font _tabFont = new Font("Arial", (float)10.0, FontStyle.Bold, GraphicsUnit.Pixel);
// Draw string. Center the text.
StringFormat _stringFlags = new StringFormat();
_stringFlags.Alignment = StringAlignment.Center;
_stringFlags.LineAlignment = StringAlignment.Center;
g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags));