Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I have this code. The Image and text are in left side but I want that they be in right side (Right to Left). How?

C#
private void menuSubItemContextQYes_DrawItem(object sender, DrawItemEventArgs e)
   {
       Rectangle rcImage = e.Bounds;
       Image image = Image.FromFile(Application.StartupPath + @"\Icons\DeleteIcon.jpg");
       rcImage.Width = image.Width/8;
       rcImage.Height = image.Height/8;
       e.Graphics.DrawImage(image, rcImage);
       MenuItem mi = (MenuItem)sender;
       Font menuFont = SystemInformation.MenuFont;
       SolidBrush menuBrush = new SolidBrush(SystemColors.MenuText);
       StringFormat strfmt = new StringFormat();
       strfmt.LineAlignment = System.Drawing.StringAlignment.Near;
       Rectangle rcText = e.Bounds;
       rcText.X += rcImage.Width;
       e.Graphics.DrawString(mi.Text, menuFont, menuBrush, e.Bounds.Left - image.Width/8, e.Bounds.Bottom + ((e.Bounds.Height - menuFont.Height) / 2), strfmt);
   }

   private void menuSubItemContextQYes_MeasureItem(object sender, MeasureItemEventArgs e)
   {
       MenuItem mi = (MenuItem)sender;
       Font menuFont = SystemInformation.MenuFont;
       StringFormat strfmt = new StringFormat();
       strfmt.LineAlignment = System.Drawing.StringAlignment.Near;
       SizeF sizef = e.Graphics.MeasureString(mi.Text, menuFont, 1000, strfmt);
       Image image = Image.FromFile(Application.StartupPath + @"\Icons\DeleteIcon.jpg");
       e.ItemWidth = (int)Math.Ceiling(sizef.Width) + image.Width / 8;
       e.ItemHeight = (int)Math.Ceiling(sizef.Height) + image.Height / 8;
   }
Posted
Updated 24-Jul-15 0:58am
v2
Comments
Ralf Meier 24-Jul-15 6:36am    
The command e.graphice.drawimage draws the Image at the Position, defined by the coordinates of the rectangle rcImage. Here ypu only havbe to change the X-Position of the rectangle to e.bounds.width-image.Width/8.

The same is it with the e.graphics.drawstring-command.
Perhaps you try it by yourself ...
Ralf Meier 25-Jul-15 11:59am    
... posted as Solution ...
aazam rafiee zade 24-Jul-15 12:21pm    
Thank you. I change my code: e.Graphics.DrawImage(image, (e.Bounds.Width - (image.Width / 8)), 0, image.Width / 8, image.Height / 8); AND e.Graphics.DrawString(mi.Text, menuFont, menuBrush, e.Bounds.Width - image.Width / 8 - 15, e.Bounds.Top + ((e.Bounds.Height / 2)), strfmt);
Are they correct?
Ralf Meier 25-Jul-15 11:58am    
Are they working correct ?
The DrawImage-Command looks good - the DrawString-Command I can't evaluate ...
aazam rafiee zade 26-Jul-15 9:26am    
Yes.e.Graphics.DrawString has true result. I put "-15" to put the text at correct place.

1 solution

The command e.graphice.drawimage draws the Image at the Position, defined by the coordinates of the rectangle rcImage. Here ypu only havbe to change the X-Position of the rectangle to e.bounds.width-image.Width/8.

The same is it with the e.graphics.drawstring-command.
Perhaps you try it by yourself ...
 
Share this answer
 
Comments
aazam rafiee zade 26-Jul-15 9:26am    
Thank you
Ralf Meier 26-Jul-15 13:44pm    
Would you please formally accept my suggestion as answer ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900