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?
- 3Well, you should decide WPF or WinForms, the solutions will be fundamentally different - Andrey NOP
- 2Spread the questions. WinForms and WPF differ slightly more than completely. - rdorn
|
1 answer
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.
|