borderstyle of a panel

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I seems not to find how to change borderstyle of a panel so that it is not
sunken, but raised. I only find a 3d setting in obejct inspector... Someone
knows this ?
 
Hi WilFried,

I have written the following code in the paint event of the Panel to give it
a raised look. hope this helps

//code starts
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{

Graphics g = panel1.CreateGraphics();

Rectangle panelRect = panel1.ClientRectangle;

Point p1 = new Point(panelRect.Left, panelRect.Top); //top left
Point p2 = new Point(panelRect.Right-1, panelRect.Top); //Top Right
Point p3 = new Point(panelRect.Left, panelRect.Bottom-1); //Bottom Left
Point p4 = new Point(panelRect.Right - 1, panelRect.Bottom -1); //Bottom
Right

Pen pen1 = new Pen(System.Drawing.Color.White);
Pen pen2 = new Pen(System.Drawing.Color.Black);

g.DrawLine(pen1, p1, p2);
g.DrawLine(pen1, p1, p3);
g.DrawLine(pen2, p2, p4);
g.DrawLine(pen2, p3, p4);
}

//code ends

Happy programming!
pradeep_TP
 
As an alternative to painting the window you can always allow the system to
handle the border style and still be part of the non-client area using some
p/invokes. There are other border styles that you can use as well if you
look through the docs. But if you're painting the border yourself then you
might consider using the ControlPaint class to perform the drawing for you.

using System.Runtime.InteropServices;

private const int GWL_EXSTYLE = -20;
private const int WS_EX_DLGMODALFRAME = 0x00000001;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int SWP_FRAMECHANGED = 0x0020;

[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);

IntPtr hWnd = this.panel1.Handle;
int style = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, (style | WS_EX_DLGMODALFRAME));
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, (SWP_FRAMECHANGED | SWP_NOSIZE |
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE));
 
HI Tim,

That was a good tip. The code is importing three DLLs. What kind of
performance issues are there in such calls. I means is is faster than drawing
the windows.

In my VB 6 days I used to do what exactly you have written. But in .net
things have beocme more easier.

Can you please throw some more lights on the performance issues here. I want
to gain more understanding about the same.

thanks
pradeep_TP

Tim Wilson said:
As an alternative to painting the window you can always allow the system to
handle the border style and still be part of the non-client area using some
p/invokes. There are other border styles that you can use as well if you
look through the docs. But if you're painting the border yourself then you
might consider using the ControlPaint class to perform the drawing for you.

using System.Runtime.InteropServices;

private const int GWL_EXSTYLE = -20;
private const int WS_EX_DLGMODALFRAME = 0x00000001;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int SWP_FRAMECHANGED = 0x0020;

[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);

IntPtr hWnd = this.panel1.Handle;
int style = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, (style | WS_EX_DLGMODALFRAME));
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, (SWP_FRAMECHANGED | SWP_NOSIZE |
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE));
 
The code just p/invokes to a system dll ("user32.dll"). To be honest,
without running tests, I would think that the p/invoke method would be a lot
faster than the .Net drawing method since under the covers the drawing
method is going to be p/invoking too, and making more p/invoke calls. But
the .Net drawing method is more flexible since you can basically do whatever
you want. In other words, if you drew the border yourself then you're pretty
much only limited by your imagination when it comes to custom styles. The
way I see it, it all depends on what you're attempting to accomplish.
However, when you look at the manifest, through ildasm, for the
"System.Drawing.dll" you can see that it too relies on the "user32.dll".

--
Tim Wilson
..Net Compact Framework MVP

pradeep_TP said:
HI Tim,

That was a good tip. The code is importing three DLLs. What kind of
performance issues are there in such calls. I means is is faster than drawing
the windows.

In my VB 6 days I used to do what exactly you have written. But in .net
things have beocme more easier.

Can you please throw some more lights on the performance issues here. I want
to gain more understanding about the same.

thanks
pradeep_TP

Tim Wilson said:
As an alternative to painting the window you can always allow the system to
handle the border style and still be part of the non-client area using some
p/invokes. There are other border styles that you can use as well if you
look through the docs. But if you're painting the border yourself then you
might consider using the ControlPaint class to perform the drawing for you.

using System.Runtime.InteropServices;

private const int GWL_EXSTYLE = -20;
private const int WS_EX_DLGMODALFRAME = 0x00000001;
private const int SWP_NOSIZE = 0x0001;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int SWP_FRAMECHANGED = 0x0020;

[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);

IntPtr hWnd = this.panel1.Handle;
int style = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_EXSTYLE, (style | WS_EX_DLGMODALFRAME));
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, (SWP_FRAMECHANGED | SWP_NOSIZE |
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE));

--
Tim Wilson
..Net Compact Framework MVP

message news:D[email protected]...
Hi,

I seems not to find how to change borderstyle of a panel so that it is not
sunken, but raised. I only find a 3d setting in obejct inspector... Someone
knows this ?
 
Back
Top