-1

I have the plan to use a panel as a button same as Visual Studio installer.

the problem is that I added some text to the panel and when mouse going on top of the text, it seems that it is out of panel area.

Do you have any suggestion? is it appropriate to use a panel instead of the button?

enter image description here

Amir
  • 1,919
  • 8
  • 53
  • 105
  • 1
    Are you using Winforms or WPF? – Steve Apr 02 '18 at 19:51
  • _when mouse going on top of the text, it seems that it is out of panel area._ I don't understand. – TaW Apr 02 '18 at 22:02
  • @TaW: it means that in those places the text is there colour panel change to the time mouse leave the panel. it seems that for all the items on the panel same event should be written. is it clear now? – Amir Apr 03 '18 at 02:24
  • 1
    Truely sorry, but no, I still have no idea what is going on and what you want instead. Basically there should be no problem using a Panel like a Button. Note: You should tag the question as Winforms or whatever it is! Also you should show or at least mention all relevant code! – TaW Apr 03 '18 at 07:34
  • @TaW: I also thought same, but mouse pointer will change on text on windowsform. try it. – Amir Apr 03 '18 at 13:11
  • 1
    If the "text" is a label control, then yes, you won't get a MouseDown event for the panel. But you will for the Label. It's better to just draw the text though. – LarsTech Apr 03 '18 at 14:28
  • _mouse pointer will change on text on windowsform_ Um, Form? or Panel? You need to give us all relevant facts. How is the text created? Drawn? How? a Label? We're not here to guess!! Any MouseEnter events coded? How does the pointer change? From what shape to what shape? We all do have crystall balls but you ought to help us helping you!! – TaW Apr 03 '18 at 15:44
  • @TaW: they are lables – Amir Apr 03 '18 at 16:32
  • In that case you simply have to code a MouseEnter and a MouseLeave event to show the cursor you want, as you probably have done for the Panel as well. You also should use the Panel's Click event for the Labels! Note: All Labels can share the same events! Also note that coding the Panel.Paint event and DrawString the Texts may be even better/simpler.. – TaW Apr 03 '18 at 17:09
  • 1
    @TaW TextRenderer, not DrawString. – LarsTech Apr 03 '18 at 17:28
  • @Lars, hehe, yes, I was thinking of it but decided against it; newbie overload panic at the horizon. (Plus I have never __seen__ any difference; but I know under some circumstances it is better..) – TaW Apr 03 '18 at 18:59
  • 1
    @TaW `e.Graphics.DrawString("Hiiiiiiiiiiiiiiiiiiiiiiiiiiiii", ....);` – LarsTech Apr 03 '18 at 19:07
  • @TaW: I solved the problem, check the solution. – Amir Apr 03 '18 at 19:34

2 Answers2

0

I solve my problem by extend a class from button by using below link: Ref Link

public class MYButton:Button
    {
        public MYButton()
        {
            UseVisualStyleBackColor = false;
            TextImageRelation = TextImageRelation.ImageAboveText;

        }
        public override string Text
        {
            get { return ""; }
            set { base.Text = value; }
        }
        public string TextCenter { get; set; }
        public string TextDetails { get; set; }

        Font fontTextCenter { get; set; }
        Font fontTextDetails { get; set; }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            fontTextCenter = new Font("Microsoft Sans Serif", 22F, FontStyle.Bold);
            fontTextDetails = new Font("Microsoft Sans Serif", 8F);
            base.OnPaint(pevent);
            Rectangle rect = ClientRectangle;
            rect.Inflate(-5, -5);

            StringFormat sf = new StringFormat();
            sf.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip;

            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;

            Brush brush = new SolidBrush(ForeColor);
            pevent.Graphics.DrawString(TextCenter, fontTextCenter, brush, rect, sf);

            sf.Alignment = StringAlignment.Near;
            sf.LineAlignment = StringAlignment.Far;
            sf.FormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoClip;

            pevent.Graphics.DrawString(TextDetails, fontTextDetails, brush, rect, sf);


        }


    }
Amir
  • 1,919
  • 8
  • 53
  • 105
  • OK. Note: a) you are leaking the Brsuh and the Fonts. b) do use TextRenderer instead og DrawString, as suggested by Lars. – TaW Apr 03 '18 at 19:37
  • @TaW: I didnt get you properly. can you modify my code by TextRenderer? I read somewhere "The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. Instead, use the DrawString methods of the Graphics class." – Amir Apr 04 '18 at 05:52
  • Where woul one read that?? It is not true at all. `TextRenderer.DrawText(pevent.Graphics, TextCenter, fontTextCenter,..);` There are several overloads, similar to DrawString. One even offeres a BackColor.. – TaW Apr 04 '18 at 06:27
  • @TaW: the source was MSDN ;), just google the sentence. – Amir Apr 04 '18 at 07:45
  • Whoops I didn't read carefully. Indeed it is not suitable for __printing__. Showing text on controls is what it is meant for! - Btw, why have Fonts as Properites (which is nice) and then not use them? Also: You need to dispose of all GDI+ resources you create, best by creating them in a `using` clause. – TaW Apr 04 '18 at 08:28
-2

What about this? Overlay a transparent panel over the panel with the text and respond to the mouse based on the transparent panel.

Jeff
  • 646
  • 1
  • 7
  • 13
  • nice trick but my screen is resizable. how to handle that. if I want to put a transparent panel than I should use null dock am I right? – Amir Apr 03 '18 at 02:25