Tuesday, December 18, 2012

Get Textbox or RichtextBox value using pInvoke (Windows API)

Assuming you already get the pointer handle of the textbox/richtextbox control from source calling function, the snippet to get the content of the edit control is as follows.
public const int GWL_ID = -12;  
 public const int WM_GETTEXT = 0x000D;  
 public static extern int GetWindowLong(IntPtr hWnd, int index);  
 public static extern IntPtr SendDlgItemMessage(IntPtr hWnd, int IDDlgItem, int uMsg, int nMaxCount, StringBuilder lpString);  
 public static extern IntPtr GetParent(IntPtr hWnd);  
 private StringBuilder GetRichEditText(IntPtr hWndRichEdit)  
   Int32 dwID = GetWindowLong(hWndRichEdit, GWL_ID);  
   IntPtr hWndParent = GetParent(hWndRichEdit);  
   StringBuilder title = new StringBuilder(128);  
   SendDlgItemMessage(hWndParent, dwID,  
     128, title);  
   return title;  
For labels and buttons, you use the GetWindowText() function... :)



Post a Comment