WinForms adds a picturebox and a round picture to it, but the container remains rectangular. When I hang onClick on the picturebox, empty areas appear in which the method works, but the images are not there.
With small pictures, this is not critical, but with great difficulty.
If the picture will have a more complex shape, for example a star, can a similar question be solved?
What ways to work around this problem exist in WinForms and WPF?

  • 3
    Well, you should decide WPF or WinForms, the solutions will be fundamentally different - Andrey NOP
  • 2
    Spread the questions. WinForms and WPF differ slightly more than completely. - rdorn

1 answer 1

Want perversions? You are welcome. The solution for WinForms, how to do this in WPF has not yet figured out, this is from archives when it is still kodil for code =)

using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; partial class MyForm : Form { public MyForm() { InitializeComponent();//базовая инициализация Ρ„ΠΎΡ€ΠΌΡ‹ var customPanel = new Panel(); ////ΠŸΠΎΠ΄ΠΏΠΈΡΡ‹Π²Π°Π΅ΠΌΡΡ Π½Π° событиС Click, для ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ Π·ΠΎΠ½ активности ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π° customPanel.Click += CustomPanel_Click; //малСнький Ρ…Π°ΠΊ для получСния Π²ΠΈΠ΄ΠΈΠΌΠΎΠ³ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° Π½Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π½Π½ΠΎΠ³ΠΎ тСкста var strSize = Graphics.FromImage(new Bitmap(1, 1)).MeasureString("Π₯ΠžΠ™!", new Font(FontFamily.GenericMonospace, 100, FontStyle.Bold)).ToSize(); //Π—Π°Π΄Π°Π΅ΠΌ Ρ„ΠΎΡ€ΠΌΡƒ ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»Π° GraphicsPath gp = new GraphicsPath(); gp.AddString("Π₯ΠžΠ™!", FontFamily.GenericMonospace, (int)FontStyle.Bold, 100, new Point(0, 0), StringFormat.GenericDefault); customPanel.Region = new Region(gp); customPanel.Size = strSize; customPanel.BackColor = Color.Black; //ДобавляСм ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ» Π½Π° Ρ„ΠΎΡ€ΠΌΡƒ Controls.Add(customPanel); } private void CustomPanel_Click(object sender, System.EventArgs e) { MessageBox.Show("Попал!"); } } 

I hope this is a rather complicated form of control =)

Only I warn you right away, forget about the standard styles of border design, etc. etc. All of them are designed for standard rectangular shape. As soon as you set about changing the shape of the control, get ready to draw everything, from the selection frames to the shadows, and of course, the content itself. As for the rest, controls with an arbitrary shape are no different from ordinary ones.