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.
Code:
public const int GWL_ID = -12;  
 public const int WM_GETTEXT = 0x000D;  
 [DllImport("User32.dll")]  
 public static extern int GetWindowLong(IntPtr hWnd, int index);  
 [DllImport("User32.dll")]  
 public static extern IntPtr SendDlgItemMessage(IntPtr hWnd, int IDDlgItem, int uMsg, int nMaxCount, StringBuilder lpString);  
 [DllImport("User32.dll")]  
 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,  
     WM_GETTEXT,  
     128, title);  
   return title;  
 } 
For labels and buttons, you use the GetWindowText() function... :)

Greg

0 comments:

Post a Comment