How do I capture a bitmap of my form

You can import the BitBlt API to handle this problem. Here is a solution offered by Simon Murrell and Lion Shi in the microsoft.public.dotnet.windows.forms newsgroup. The number used below, 13369376, is Int32 SRCCOPY = 0xCC0020; You can use Gdi32 dll. You can define the BitBlt method found within the Gdi32 dll with the code below. [System.Runtime.InteropServices.DllImportAttribute(‘gdi32.dll’)] private static extern bool BitBlt( ntPtr hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner System.Int32 dwRop // raster operation code ); And you can then use the copy below in a button click event to save the form to an image. Graphics g1 = this.CreateGraphics(); Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1); Graphics g2 = Graphics.FromImage(MyImage); IntPtr dc1 = g1.GetHdc(); IntPtr dc2 = g2.GetHdc(); BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376); g1.ReleaseHdc(dc1); g2.ReleaseHdc(dc2); MyImage.Save(@’c:\Captured.bmp’, ImageFormat.Bmp); You will need to use the System.Drawing.Imaging namespace. Here is VB code posted by Armin Zingler in the microsoft.public.dotnet.languages.vb newsgroup. Public Class Win32 Public Declare Function BitBlt Lib ‘gdi32’ Alias ‘BitBlt’ _ (ByVal hDestDC As Integer, ByVal x As Integer, _ ByVal y As Integer, ByVal nWidth As Integer, _ ByVal nHeight As Integer, ByVal hSrcDC As Integer, _ ByVal xSrc As Integer, ByVal ySrc As Integer, _ ByVal dwRop As Integer) As Integer Public Declare Function GetWindowDC Lib ‘user32’ Alias ‘GetWindowDC’ _ (ByVal hwnd As Integer) As Integer Public Declare Function ReleaseDC Lib ‘user32’ Alias ‘ReleaseDC’ _ (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer Public Const SRCCOPY As Integer = &HCC0020 End Class Public Class Hardcopy Public Shared Function CreateBitmap( _ ByVal Control As Control) _ As Bitmap Dim gDest As Graphics Dim hdcDest As IntPtr Dim hdcSrc As Integer Dim hWnd As Integer = Control.Handle.ToInt32 CreateBitmap = New Bitmap(Control.Width, Control.Height) gDest = gDest.FromImage(CreateBitmap) hdcSrc = Win32.GetWindowDC(hWnd) hdcDest = gDest.GetHdc Win32.BitBlt( _ hdcDest.ToInt32, 0, 0, Control.Width, Control.Height, _ hdcSrc, 0, 0, Win32.SRCCOPY _ ) gDest.ReleaseHdc(hdcDest) Win32.ReleaseDC(hWnd, hdcSrc) End Function End Class ’In your Form: Private Sub Button1_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click Dim bmp As Bitmap bmp = Hardcopy.CreateBitmap(Me) bmp.Save(‘c:\test.bmp’) End Sub

How can I add a control to a Window Form at runtime

To add a control at runtime, you do three steps: 1. Create the control 2. Set control properties 3. Add the control to the Form’s Controls collection In general, if you need help on exactly what code you need to add, just look at the code generated by the designer when you add the control at design time. You can generally use the same code at runtime. Here are code snippets that create a textBox at runtime. [C#] //step 1 TextBox tb = new TextBox(); //step2 tb.Location = new Point( 10, 10); tb.Size = new Size(100, 20); tb.Text = ‘I was created at runtime’; //step3 this.Controls.Add(tb); [VB.NET] ’step 1 Dim tb as TextBox = New TextBox() ’step2 tb.Location = New Point( 10, 10) tb.Size = New Size(100, 20) tb.Text = ‘I was created at runtime’ ’step3 Me.Controls.Add(tb)

How do I make the TreeView scroll when I drag an item to the top or bottom?

When you drag an item within the TreeView, you can handle the DragOver event for a drag-drop. If you want to drag-drop into a spot that’s not currently visible, you can scroll the TreeView by handling the DragOver event: [C#] private void treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e) { TreeView tv = sender as TreeView; Point pt = tv.PointToClient(new Point(e.X,e.Y)); int delta = tv.Height – pt.Y; if ((delta < tv.Height / 2) && (delta > 0)) { TreeNode tn = tv.GetNodeAt(pt.X, pt.Y); if (tn.NextVisibleNode != null) tn.NextVisibleNode.EnsureVisible(); } if ((delta > tv.Height / 2) && (delta < tv.Height)) { TreeNode tn = tv.GetNodeAt(pt.X, pt.Y); if (tn.PrevVisibleNode != null) tn.PrevVisibleNode.EnsureVisible(); } } [VB.NET] Private Sub treeView1_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) If TypeOf sender is TreeView Then Dim tv As TreeView = CType(sender, TreeView) Dim pt As Point = tv.PointToClient(New Point(e.X, e.Y)) Dim delta As Integer = tv.Height – pt.Y If delta < tv.Height / 2 And delta > 0 Then Dim tn As TreeNode = tv.GetNodeAt(pt.X, pt.Y) If Not (tn.NextVisibleNode Is Nothing) Then tn.NextVisibleNode.EnsureVisible() End If End If If delta > tv.Height / 2 And delta < tv.Height Then Dim tn As TreeNode = tv.GetNodeAt(pt.X, pt.Y) If Not (tn.PrevVisibleNode Is Nothing) Then tn.PrevVisibleNode.EnsureVisible() End If End If End If End Sub ’treeView1_DragOver

How can I programmatically position the cursor on a given line and character of my richtextbox

There are a couple different methods that can be used here. The first changes focus, so may not be possible if you have controls that fire validation. The second uses interop, which requires full trust. Method 1: Eric Terrell suggested this solution in an email to [email protected]. The richtextbox control contains a Lines array property, one entry for every line. Each line entry has a Length property. With this information, you can position the selection cursor using code such as: private void GoToLineAndColumn(RichTextBox RTB, int Line, int Column) { int offset = 0; for (int i = 0; i < Line – 1 && i < RTB.Lines.Length; i++) { offset += RTB.Lines[i].Length + 1; } RTB.Focus(); RTB.Select(offset + Column, 0); } (Note: you may want to store this.ActiveControl to be retrieved after calling Select()). Method 2: const int SB_VERT = 1; const int EM_SETSCROLLPOS = 0x0400 + 222; [DllImport(‘user32’, CharSet=CharSet.Auto)] public static extern bool GetScrollRange(IntPtr hWnd, int nBar, out int lpMinPos, out int lpMaxPos); [DllImport(‘user32’, CharSet=CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, POINT lParam); [StructLayout(LayoutKind.Sequential)] public class POINT { public int x; public int y; public POINT() { } public POINT(int x, int y) { this.x = x; this.y = y; } } // Example — scroll the RTB so the bottom of the text is always visible. int min, max; GetScrollRange(richTextBox1.Handle, SB_VERT, out min, out max); SendMessage(richTextBox1.Handle, EM_SETSCROLLPOS, 0, new POINT(0, max – richTextBox1.Height));