1
Answer

Problem with draggable panels containing labels

Adamo

Adamo

15y
2.8k
1
Hi, I'm working on my first C# project with Windows Forms, and so have used the internet to find solutions to a lot of my questions. One such problem was originally how to get a Panel to be movable within a given area.

One of the solutions which I found, was to detect Windows Messages (using the hex codes of WM_LBUTTONDOWN, WM_MOUSEHOVER, etc.) on a MovablePanel, which would be a child class of Panel, and to act on these messages accordingly. This would be handled in an overridden WndProc function...

protected override void WndProc(ref Message m)

While this was working fine, I also have a windows Label which I added to the MovablePanel, and it seems that if the mouse is hovering over or clicking directly on that Label, the MovablePanel does not detect the Windows Messages I check for, which I suppose makes sense. It was problematic when I first noticed it, but I figured I'd deal with it later.

But now I've been trying to make sure the label in the panel is always centered, and did so by:

m_label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
m_label.Dock = System.Windows.Forms.DockStyle.Fill;
m_label.AutoSize = false;


Of course, this means that the label now fills up the entirety of the MovablePanel, so none of the Windows Messages are being detected whatsoever, and I can no longer drag it.

I'm simply looking for some way to go around this that might hopefully be simple. I suppose I could make the label movable as opposed to the panel itself, but that just seems silly...all help is very greatly appreciated! Thanks so much :)
Answers (1)