Home > Net >  About USER32 API in c # text box handle
About USER32 API in c # text box handle

Time:10-03

 
[DllImport (" user32. DLL, "EntryPoint=" FindWindowEx SetLastError=true)]
Private static extern IntPtr FindWindowEx (IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport (" user32. DLL, "EntryPoint=" SetForegroundWindow SetLastError=true)]
Private static extern void SetForegroundWindow (IntPtr HWND);

[DllImport (" user32. DLL)]
Public static extern int EnumChildWindows (IntPtr hWndParent, CallBack LPFN, int lParam);

///& lt; Summary>
///search form control handle
///& lt;/summary>
///& lt; Param name="HWND" & gt; The parent window handle & lt;/param>
///& lt; Param name="lpszWindow & gt;" Control header (Text) & lt;/param>
///& lt; Param name="bChild & gt;" If set to look for in the child form & lt;/param>
///& lt; Returns> Control handle, didn't find return IntPtr. Zero
Public static IntPtr FindWindowExMy (IntPtr HWND, string lpszWindow, bool bChild)
{
IntPtr iResult=IntPtr. Zero;
//the first lookup controls in the parent form
IResult=FindWindowEx (HWND, 0, lpszClass, "");
//if found directly returns a handle to the control
If (iResult!=IntPtr. Zero)
Return iResult;

//if the setting is not in child form lookup
if (! BChild)
Return iResult;

//the enumeration child form, find the control handle
Int I=EnumChildWindows (HWND, (h, l)=& gt;
{
IntPtr f1=FindWindowEx (h, 0, lpszClass, lpszWindow);
If (f1==IntPtr. Zero)
return true;
The else
{
The StringBuilder title=new StringBuilder (200);
int len;
Len=GetWindowText (HWND, title, 200);

IResult=f1;
return false;
}
},
0);
//return search results
Return iResult;
}




///& lt; Summary>
///set the text box content
///& lt;/summary>
///& lt; Param name="taskName" & gt; Process name & lt;/param>
///& lt; Param name="Text" & gt; The content & lt;/param>
Public static void SetText (string taskName, string Text)
{
IntPtr hwndPhoto=FindWindow (null, taskName);//to find a photo of handle the application name] in [the task manager

If (hwndPhoto!=IntPtr. Zero)
{
Bool isCur=false;
IntPtr hwndText=new IntPtr ();
HwndText=FindWindowExMy (hwndPhoto, "", true);
The SetForegroundWindow (hwndPhoto);//setting UcDemo program as the current active window
If (hwndText!=IntPtr. Zero)
{
SendMessage (hwndText WM_SETTEXT, IntPtr. Zero, "123");
}
A Rectangle rs=new Rectangle ();
GetClientRect (hwndText, ref rs);


}

}


The above code is used to third-party interface text box input to the value of the third-party interface of I, there are two text boxes, for the first time to use "" empty string into method can obtain normal handle, and to change the control input text, but use the second method is to use" "or string into method or handle to get the first time, this time the handle have values in the text box, imagine this is the second time I can get another handle to an empty text box, but hasn't do

CodePudding user response:

  •  Tags:  
  • C#
  • Related