以下是Delphi代码:
1 const 2 EM_SETCUEBANNER = $1501; 3 EM_GETCUEBANNER = $1502; 4 5 procedure SetCueText(control: TWinControl; text: string); 6 var 7 info: TComboBoxInfo; 8 begin 9 if (control is TComboBox) then 10 begin 11 info.cbSize := SizeOf(info); 12 GetComboBoxInfo(control.Handle, info); 13 SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, Integer(PChar(text))); 14 end 15 else 16 begin 17 SendMessage(control.Handle, EM_SETCUEBANNER, 0, Integer(PChar(text))); 18 end; 19 end; 20 21 procedure TForm2.FormCreate(Sender: TObject); 22 begin 23 SetCueText(Edit1, 'hello kugou'); 24 SetCueText(Edit2, 'hello kugou 2'); 25 SetCueText(ComboBox1, 'hello kugou 3'); 26 end;
以下是c#代码:
1 [DllImport("user32.dll", CharSet = CharSet.Auto)] 2 private static extern Int32 SendMessage(IntPtr hWnd, int msg, 3 int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); 4 5 [DllImport("user32.dll")] 6 private static extern bool SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder lParam); 7 8 [DllImport("user32.dll")] 9 private static extern bool GetComboBoxInfo(IntPtr hwnd, ref COMBOBOXINFO pcbi); 10 11 [StructLayout(LayoutKind.Sequential)] 12 private struct COMBOBOXINFO 13 { 14 public int cbSize; 15 public RECT rcItem; 16 public RECT rcButton; 17 public IntPtr stateButton; 18 public IntPtr hwndCombo; 19 public IntPtr hwndItem; 20 public IntPtr hwndList; 21 } 22 23 [StructLayout(LayoutKind.Sequential)] 24 private struct RECT 25 { 26 public int left; 27 public int top; 28 public int right; 29 public int bottom; 30 } 31 32 private const int EM_SETCUEBANNER = 0x1501; 33 private const int EM_GETCUEBANNER = 0x1502; 34 35 public static void SetCueText(Control control, string text) 36 { 37 if (control is ComboBox) 38 { 39 COMBOBOXINFO info = GetComboBoxInfo(control); 40 SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, text); 41 } 42 else 43 { 44 SendMessage(control.Handle, EM_SETCUEBANNER, 0, text); 45 } 46 } 47 48 private static COMBOBOXINFO GetComboBoxInfo(Control control) 49 { 50 COMBOBOXINFO info = new COMBOBOXINFO(); 51 //a combobox is made up of three controls, a button, a list and textbox; 52 //we want the textbox 53 info.cbSize = Marshal.SizeOf(info); 54 GetComboBoxInfo(control.Handle, ref info); 55 return info; 56 } 57 58 public static string GetCueText(Control control) 59 { 60 StringBuilder builder = new StringBuilder(); 61 if (control is ComboBox) 62 { 63 COMBOBOXINFO info = new COMBOBOXINFO(); 64 //a combobox is made up of two controls, a list and textbox; 65 //we want the textbox 66 info.cbSize = Marshal.SizeOf(info); 67 GetComboBoxInfo(control.Handle, ref info); 68 SendMessage(info.hwndItem, EM_GETCUEBANNER, 0, builder); 69 } 70 else 71 { 72 SendMessage(control.Handle, EM_GETCUEBANNER, 0, builder); 73 } 74 return builder.ToString(); 75 }
c#调用:
SetCueText(textBox1, " Enter the source file or source folder"); SetCueText(textBox2, " Enter the destination folder");