-----box----- #程式1擷取自WinObject.cs,針對部份輸入文字型的控制項,可使用WM_SETTEXT指定文字內容 //由類別名稱識別是否為特定的文字輸入控制項物件 public bool IsEditControl { get { string cn=ClassName.ToUpper(); return (cn=="EDIT" || cn=="THUNDERRT5TEXTBOX" || cn=="RICHCNTL" || cn=="RICHEDIT"); } } const uint WM_GETTEXTLENGTH=0x0e; const uint WM_GETTEXT=0x0d; const uint WM_SETTEXT=0x0c; //視窗物件的標題或控制項的文字內容 public string Text { get { if (IsEditControl) { int l=(int) SendMessage(hWnd,WM_GETTEXTLENGTH,0,0); StringBuilder sb=new StringBuilder(l+1); l=(int) SendMessage(hWnd,WM_GETTEXT,sb.Capacity,sb); return sb.ToString(); } else { int length = GetWindowTextLength(hWnd); StringBuilder sb = new StringBuilder(length + 1); GetWindowText(hWnd, sb, sb.Capacity); return sb.ToString(); } } set { if (IsEditControl) SendMessage(hWnd,WM_SETTEXT,0,value); else SetWindowText(hWnd, value); } } -----end----- -----box----- #程式2 與下拉選單互動的範例 [DllImport("USER32.DLL", SetLastError=true)] public static extern long SendMessage(IntPtr hWnd, uint msg, int wparam, StringBuilder sb); private void button2_Click(object sender, System.EventArgs e) { //由Spy++觀察該下拉選單控制項的從屬關係,以FindChild方式鎖定之。 //由於同一層有多個TadvCityCbBox控制項,因此使用FindChildren再由陣列中取出第2個(戶籍地)。 //原理及細節說明請參見前期文章。 WinObject cb = (WinObject) WinObject.FindWindow("TBasicForm","財政部九十二年度綜合所得稅二維條碼結算申報系統") .FindChild("TNotebook","").FindChild("TPage","Basic").FindChild("TPanel","Panel2") .FindChild("TPanel","").FindChildren("TadvCityCbBox",null)[1]; int itemCount = (int) cb.SendMessage(0x0146,0,0); //CB_GETCOUNT=0x146 string[] itemValues=new string[itemCount]; Debug.Write("共有"+itemCount.ToString()+"個選項,包含有:"); int idxTaipei=-1; for (int i=0; isize) throw new ApplicationException("RemoteMemory.Write method error: allocated memory size is not enough!"); GCHandle gc=GCHandle.Alloc(o,GCHandleType.Pinned); uint bytesWritten=0; int ret=WriteProcessMemory(hProcess, lpMem, (uint) gc.AddrOfPinnedObject(), (uint) Marshal.SizeOf(o), ref bytesWritten); } //將記憶體中的內容還原至物件 public void Read(ref object o) { GCHandle gc=GCHandle.Alloc(o,GCHandleType.Pinned); uint bytesRead=0; int ret=ReadProcessMemory(hProcess, lpMem, (uint) gc.AddrOfPinnedObject(), (uint) Marshal.SizeOf(o), ref bytesRead); } //將記憶體中的某一段解析成為字串 public string ReadCString(uint offset, int stringLen) { uint bytesRead=0; byte[] buffer=new byte[stringLen]; int ret=ReadProcessMemory(hProcess, lpMem+offset, buffer, (uint) stringLen, ref bytesRead); return szToString(buffer); } //將整塊記憶體解析成為字串 public string ReadCString() { return ReadCString(0,size); } //將位元組轉成字串 private string szToString(byte[] byteArray) { int length=0; for (int i=0; i