Winform Interview Questions Page 3
Page 1Page 2Page 3Page 4Page 5


 How do I provide a 2 pixel 3d border in the Non-Client area of my Control derived class?

You can do so as follows by overriding the CreateParams property in your Control.

The advantage with this approach is that drawing is handled by the system as soon as you set the flag below.
protected override CreateParams CreateParams
{
get
{
CreateParams cparams;
cparams = base.CreateParams;
if(this.need3DBorder)
{
cparams.ExStyle &= ~512;
cparams.Style &= ~8388608 /*WS_BORDER*/;
cparams.ExStyle = cparams.ExStyle | 512 /*WS_EX_DLGFRAME*/;
}
return cparams;
}
}

  How do I provide a 1 pixel border in the NonClient area of my Control?

You will have to first provide some space in the NC area by setting the WS_BORDER flag in CreateParams and then draw the border yourself by listening to the WM_NCPAINT message in your Control, as follows:

protected override CreateParams CreateParams

{
get

{
System.Windows.Forms.CreateParams cp = base.CreateParams;
if(this.needFlatBorder)
{
params.ExStyle &= ~512 /*WS_EX_CLIENTEDGE*/;
cparams.Style &= ~8388608 /*WS_BORDER*/;
cp.Style |= 0x800000; // WS_BORDER
}
}
}
protected override void WndProc(ref Message m)
{
if(m.Msg == 133/*WM_NCPAINT*/)
{
this.DrawFlatNCBorder(ref m);
}
base.WndProc(ref m);
}
private void DrawFlatNCBorder(ref Message msg)
{
IntPtr hRgn1 = (IntPtr) msg.WParam;
// The update region is clipped to the window frame.
When wParam is 1, the entire window frame needs to be updated.
IntPtr hdc = NativeMethods.GetDCEx(msg.HWnd, hRgn1, 1/*DCX_WINDOW*/|0x0020/*DCX_PARENTCLIP*/);
if (hdc != IntPtr.Zero)
{
using (Graphics g = Graphics.FromHdc(hdc))
{
Rectangle bounds = new Rectangle(0,0,this.Width,this.Height);
ControlPaint.DrawBorder(g,bounds,this.borderColor,ButtonBorderStyle.Solid);
// create a clipping region for remaining parts to be drawn excluding
// the border we did just drew
bounds.Inflate(-1, -1);
IntPtr hRgn2 = NativeMethods.CreateRectRgn(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom);
if(hRgn2 == (IntPtr)1)



{

// Provide a new clipping region.


msg.WParam = (IntPtr) hRgn2;

}
else

{

// combine with existing clipping region.

NativeMethods.CombineRgn(hRgn1, hRgn1, hRgn2, NativeMethods.RGN_AND);

NativeMethods.DeleteObject(hRgn2);

}
}
msg.Result = (IntPtr) 1;

NativeMethods.ReleaseDC(msg.HWnd, hdc);

}
Invalidate();
}
2.10 How do I invalidate a control including it's NonClient area?

You can do so as follows in your Control:

private void InvalidateWindow()
{
NativeMethods.RedrawWindow(this.Handle, IntPtr.Zero, IntPtr.Zero,
0x0400/*RDW_FRAME*/ | 0x0100/*RDW_UPDATENOW*/
| 0x0001/*RDW_INVALIDATE*/);
}

2.11 How can I implement a scrollable picture box?

See Mike Gold's article on C# Corner for a detailed discussion.

2.12 How can I put Controls, a ProgressBar for example, into a StatusBar?

You cannot place controls into a StatusBar control in the designer. However, you can add any no. of Controls to the StatusBar programatically through it's Controls property. After adding the Controls, set their Visible, Location and Bounds property appropriately.

You could then create a status bar that looks like this, for example:

2.13 How do I implement an ownerdrawn statusbar so I can put a progressbar in it?
Check out the code posted originally in VB by Jacob Grass, and translated to C# by Jason Lavigne on the dotnet.discussion newsgroup at develop.com.