1 using System; 2 using System.Diagnostics; 3 using System.Drawing; 4 using System.Collections; 5 using System.Collections.Generic; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 using System.Runtime.InteropServices; 10 11 namespace CSharpFormLibrary 12 { 13 public class IMECandidateForm : System.Windows.Forms.Form 14 { 15 //private System.Windows.Forms.ListView lbCandidates; 16 private IMEListView lbCandidates; 17 private IContainer components; 18 private int currentPage = 0; //�ثe���� 19 private int totalPage = 0; //�ǤJ�� candidate �@���X�� 20 private ArrayList m_candArray; //�ǤJ�� candi array 21 private string[] m_inputs; 22 private int m_mode = 0; //0:���� 1:�i�} 23 24 //����index 25 //int now_index; 26 //int baseSize; //����(�S��candidate��form���j�p) 27 private IntPtr m_hwnd; 28 private const int WM_MOUSEACTIVATE = 0x0021; 29 private const int MA_ACTIVATE = 0x0001; 30 private const int MA_ACTIVATEANDEAT = 0x0002; 31 private const int MA_NOACTIVATE = 0x0003; 32 private VScrollBar vScrollBar1; 33 private ColumnHeader columnHeader1; 34 private ColumnHeader columnHeader2; 35 private ImageList imageList1; 36 private Label label1; 37 private Panel panel1; 38 private Panel panel2; 39 private const int MA_NOACTIVATEANDEAT = 0x0004; 40 IMECandidateForm()41 public IMECandidateForm() 42 { 43 InitializeComponent(); 44 //baseSize = this.lbCandidates.Location.Y+100; 45 //this.Opacity = 1; 46 this.SetStyle( 47 ControlStyles.UserPaint | 48 ControlStyles.AllPaintingInWmPaint | 49 ControlStyles.OptimizedDoubleBuffer, true); 50 Application.EnableVisualStyles(); 51 } 52 IMECandidateForm(string[] candidates)53 public IMECandidateForm(string[] candidates) 54 { 55 InitializeComponent(); 56 this.SetStyle( 57 ControlStyles.UserPaint | 58 ControlStyles.AllPaintingInWmPaint | 59 ControlStyles.OptimizedDoubleBuffer, true); 60 } 61 WndProc(ref Message m)62 protected override void WndProc(ref Message m) 63 { 64 if (m.Msg == WM_MOUSEACTIVATE) 65 { 66 m.Result = (IntPtr)MA_NOACTIVATEANDEAT; 67 //m.Result = (IntPtr)MA_NOACTIVATE; 68 return; 69 } 70 71 base.WndProc(ref m); 72 } 73 Dispose(bool disposing)74 protected override void Dispose(bool disposing) 75 { 76 if (disposing) 77 { 78 if (components != null) 79 { 80 components.Dispose(); 81 } 82 } 83 base.Dispose(disposing); 84 } 85 86 #region Windows Form Designer generated code 87 /// <summary> 88 /// Required method for Designer support - do not modify 89 /// the contents of this method with the code editor. 90 /// </summary> InitializeComponent()91 private void InitializeComponent() 92 { 93 this.components = new System.ComponentModel.Container(); 94 //this.lbCandidates = new System.Windows.Forms.ListView(); 95 this.lbCandidates = new IMEListView(); 96 this.imageList1 = new System.Windows.Forms.ImageList(this.components); 97 this.columnHeader1 = new System.Windows.Forms.ColumnHeader(); 98 this.columnHeader2 = new System.Windows.Forms.ColumnHeader(); 99 this.vScrollBar1 = new System.Windows.Forms.VScrollBar(); 100 this.label1 = new System.Windows.Forms.Label(); 101 this.panel1 = new System.Windows.Forms.Panel(); 102 this.panel2 = new System.Windows.Forms.Panel(); 103 this.panel1.SuspendLayout(); 104 this.panel2.SuspendLayout(); 105 this.SuspendLayout(); 106 // 107 // lbCandidates 108 // 109 this.lbCandidates.Alignment = System.Windows.Forms.ListViewAlignment.SnapToGrid; 110 this.lbCandidates.BackColor = System.Drawing.SystemColors.Window; 111 this.lbCandidates.Dock = System.Windows.Forms.DockStyle.Fill; 112 this.lbCandidates.Font = new System.Drawing.Font("PMingLiU", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136))); 113 this.lbCandidates.ForeColor = System.Drawing.SystemColors.WindowText; 114 this.lbCandidates.GridLines = true; 115 this.lbCandidates.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; 116 this.lbCandidates.HideSelection = false; 117 this.lbCandidates.Name = "lbCandidates"; 118 this.lbCandidates.Bounds = new Rectangle(0, 0, 105, 266); 119 this.lbCandidates.Scrollable = false; 120 this.lbCandidates.SmallImageList = this.imageList1; 121 this.lbCandidates.View = System.Windows.Forms.View.Details; 122 this.lbCandidates.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lbCandidates_KeyDown); 123 this.lbCandidates.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.lbCandidates_KeyPress); 124 // 125 // imageList1 126 // 127 this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; 128 this.imageList1.ImageSize = new System.Drawing.Size(1, 25); 129 this.imageList1.TransparentColor = System.Drawing.Color.Transparent; 130 // 131 // columnHeader1 132 // 133 //this.columnHeader1.Width = 25; 134 this.columnHeader1.Width = -1; 135 // 136 // columnHeader2 137 // 138 //this.columnHeader2.Width = 80; 139 this.columnHeader2.Width = -1; 140 // 141 // vScrollBar1 142 // 143 this.vScrollBar1.Dock = System.Windows.Forms.DockStyle.Right; 144 this.vScrollBar1.Maximum = 11; 145 this.vScrollBar1.Minimum = 1; 146 this.vScrollBar1.Name = "vScrollBar1"; 147 this.vScrollBar1.Bounds = new Rectangle(105, 0, 24, 266); 148 this.vScrollBar1.Value = 1; 149 // 150 // label1 151 // 152 this.label1.AutoSize = true; 153 this.label1.BackColor = System.Drawing.SystemColors.ButtonFace; 154 this.label1.Font = new System.Drawing.Font("PMingLiU", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136))); 155 this.label1.Name = "label1"; 156 this.label1.Bounds = new Rectangle(3, 4, 52, 15); 157 this.label1.Text = "�Կ�r"; 158 // 159 // panel1 160 // 161 this.panel1.BackColor = System.Drawing.SystemColors.ButtonFace; 162 this.panel1.Controls.Add(this.label1); 163 this.panel1.Dock = System.Windows.Forms.DockStyle.Top; 164 this.panel1.Name = "panel1"; 165 this.panel1.Bounds = new Rectangle(0, 0, 129, 21); 166 // 167 // panel2 168 // 169 this.panel2.AutoSize = true; 170 this.panel2.Controls.Add(this.lbCandidates); 171 this.panel2.Controls.Add(this.vScrollBar1); 172 this.panel2.Dock = System.Windows.Forms.DockStyle.Fill; 173 this.panel2.Name = "panel2"; 174 this.panel2.Bounds = new Rectangle(0, 21, 129, 266); 175 // 176 // IMECandidateForm 177 // 178 this.AutoSize = true; 179 this.BackColor = System.Drawing.SystemColors.Window; 180 this.Controls.Add(this.panel2); 181 this.Controls.Add(this.panel1); 182 this.ForeColor = System.Drawing.SystemColors.WindowText; 183 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 184 this.Name = "IMECandidateForm"; 185 this.ShowInTaskbar = false; 186 this.Deactivate += new System.EventHandler(this.IMECandidateForm_Deactivate); 187 this.Enter += new System.EventHandler(this.IMECandidateForm_Enter); 188 this.Activated += new System.EventHandler(this.IMECandidateForm_Activated); 189 this.Validated += new System.EventHandler(this.IMECandidateForm_Validated); 190 this.Click += new System.EventHandler(this.IMECandidateForm_Click); 191 this.Validating += new System.ComponentModel.CancelEventHandler(this.IMECandidateForm_Validating); 192 this.Leave += new System.EventHandler(this.IMECandidateForm_Leave); 193 this.panel1.ResumeLayout(false); 194 this.panel1.PerformLayout(); 195 this.panel2.ResumeLayout(false); 196 this.ResumeLayout(false); 197 this.PerformLayout(); 198 } 199 #endregion 200 201 #region public methods 202 SetCandidates(string inputs)203 public void SetCandidates(string inputs) 204 { 205 //input 206 //string[] a_inputs = inputs.Split(' '); 207 string[] a_inputs; 208 if ((inputs.IndexOf('\t')) > 0) 209 { 210 a_inputs = inputs.Split('\t'); 211 } 212 else 213 { 214 a_inputs = inputs.Split(' '); 215 } 216 217 if (a_inputs == null) return; 218 string[] fakeInputs ={ "1.�@", "2.�G", "3.�T", "4.�|", "5.��", "6.��", "7.�C", "8.�K", "9.�E","(1/9)", 219 "1.�Q�@", "2.�Q�G", "3.�Q�T", "4.�Q�|", "5.�Q��", "6.�Q��", "7.�Q�C", "8.�Q�K", "9.�Q�E","(2/9)" }; 220 221 //this.ShowListView(a_inputs); // not show, only setstring 222 m_mode = 0; 223 224 //�ǤJcand List 225 //m_inputs = fakeInputs; 226 m_inputs = a_inputs; 227 this.ShowListView(m_mode); // not show, only setstring 228 } 229 SwitchCandiMode()230 public void SwitchCandiMode() //expand candidate window if possible 231 { 232 if (m_mode == 0) 233 { 234 if (totalPage > 1) //expandable 235 { 236 m_mode = 1; 237 } 238 } 239 else if (m_mode == 1) 240 { 241 m_mode = 0; 242 } 243 this.ShowListView(m_mode); 244 } 245 ResetCandi()246 public void ResetCandi() 247 { 248 this.lbCandidates.Columns.Clear(); 249 250 } SetLocation(int x, int y)251 public void SetLocation(int x, int y) 252 { 253 this.Location = new Point(x, y); 254 } 255 DisposeForm()256 public void DisposeForm() 257 { 258 this.Dispose(true); 259 } 260 261 /*public int GetValue() 262 { 263 return this.lbCandidates.SelectedIndex; 264 }*/ ClearCandidates()265 public void ClearCandidates() 266 { 267 this.lbCandidates.Items.Clear(); 268 } 269 SetHWND(IntPtr hwnd)270 public void SetHWND(IntPtr hwnd) 271 { 272 m_hwnd = hwnd; 273 } 274 275 #endregion 276 277 #region a protected override method for WS_POPUP 278 279 protected override CreateParams CreateParams 280 { 281 get 282 { 283 CreateParams cp = base.CreateParams; 284 cp.Style = unchecked((int)0x80000000); //WS_POPUP 285 return cp; 286 /*const int WS_EX_NOACTIVATE = 0x08000000; 287 CreateParams cp = base.CreateParams; 288 cp.ExStyle = cp.ExStyle | WS_EX_NOACTIVATE; 289 return cp;*/ 290 } 291 } 292 293 #endregion 294 295 #region private methods 296 ShowListView(int mode)297 private void ShowListView(int mode) //mode 0:���� 1:�i�} 298 { 299 ListViewItem li; 300 totalPage = 0; 301 302 //parse & save to arraylist 303 m_candArray = new ArrayList(); 304 for (int i = 0; i < m_inputs.Length; i++) 305 { 306 string cand = m_inputs[i]; 307 if (i == m_inputs.Length - 1) 308 m_candArray.Add(cand); 309 else 310 { 311 string no = cand.Substring(0, 1); 312 string candText = cand.Substring(2); 313 m_candArray.Add(candText); 314 } 315 } 316 317 //page info 318 string pageInfo = m_inputs[m_inputs.Length - 1]; 319 currentPage = Int32.Parse(pageInfo.Substring(1, pageInfo.IndexOf('/') - 1)); 320 string foo = pageInfo.Substring(pageInfo.IndexOf('/') + 1); 321 totalPage = Int32.Parse(foo.Substring(0, foo.Length - 1)); 322 323 //set scrollbar 324 this.vScrollBar1.Enabled = (totalPage > 1); 325 326 //init listView 327 this.lbCandidates.Columns.Clear(); 328 this.lbCandidates.Items.Clear(); 329 330 switch (mode) 331 { 332 case 0: 333 { 334 //columns 335 this.lbCandidates.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { 336 this.columnHeader1, 337 this.columnHeader2}); 338 339 //set scrollbar 340 this.vScrollBar1.Maximum = totalPage * 9; 341 this.vScrollBar1.TabIndex = totalPage; 342 this.vScrollBar1.Value = (currentPage - 1) * 9 + 1; 343 344 //new & set items 345 for (int i = 0; i < m_candArray.Count; i++) 346 { 347 string cand = m_inputs[i]; 348 li = new ListViewItem(); 349 li.UseItemStyleForSubItems = false; 350 string no = cand.Substring(0, 1); 351 string candText = cand.Substring(2); 352 if (i == m_candArray.Count - 1) 353 li.SubItems[0] = new ListViewItem.ListViewSubItem(li, "", SystemColors.WindowText, Color.GhostWhite, this.lbCandidates.Font); 354 else 355 li.SubItems[0] = new ListViewItem.ListViewSubItem(li, no, SystemColors.WindowText, Color.GhostWhite, this.lbCandidates.Font); 356 li.SubItems.Add(m_candArray[i].ToString(), SystemColors.WindowText, SystemColors.Window, this.lbCandidates.Font); 357 this.lbCandidates.Items.Add(li); 358 } 359 this.columnHeader1.Width = -1; 360 this.columnHeader2.Width = -1; 361 break; 362 } 363 case 1: 364 { 365 //set columns 366 for (int i = 0; i < m_candArray.Count; i++) 367 { 368 if (i % 9 == 0) 369 { 370 this.lbCandidates.Columns.Add("no", 25, HorizontalAlignment.Left); 371 this.lbCandidates.Columns.Add("cand", 60, HorizontalAlignment.Left); 372 } 373 } 374 375 //new items 376 for (int i = 0; i < 9; i++) 377 { 378 if (this.lbCandidates.Items.Count <= i) 379 { 380 this.lbCandidates.Items.Add(new ListViewItem()); 381 } 382 else 383 { 384 this.lbCandidates.Items[i] = new ListViewItem(); 385 } 386 this.lbCandidates.Items[i].UseItemStyleForSubItems = false; 387 } 388 389 //set scrollbar 390 this.vScrollBar1.Enabled = false; 391 392 //set items 393 for (int i = 0; i < m_candArray.Count; i++) 394 { 395 int columnIndex = (i / 9) * 2; //��s�������� Column �� index 396 li = this.lbCandidates.Items[i % 9]; 397 if (li.SubItems.Count == 1) //�Ĥ@��subitem No. 398 { 399 if (columnIndex == (currentPage - 1) * 2) 400 li.SubItems[0] = new ListViewItem.ListViewSubItem(li, (i % 9 + 1).ToString(), SystemColors.WindowText, Color.GhostWhite, this.lbCandidates.Font); 401 else 402 li.SubItems[0] = new ListViewItem.ListViewSubItem(li, "", SystemColors.WindowText, SystemColors.Window, this.lbCandidates.Font); 403 } 404 else //��Lsubitem No. 405 { 406 if (columnIndex == (currentPage - 1) * 2) 407 li.SubItems.Add((i % 9 + 1).ToString(), SystemColors.WindowText, Color.GhostWhite, this.lbCandidates.Font); 408 else 409 li.SubItems.Add("", SystemColors.WindowText, SystemColors.Window, this.lbCandidates.Font); 410 } 411 li.SubItems.Add(m_candArray[i].ToString(), SystemColors.WindowText, SystemColors.Window, this.lbCandidates.Font); 412 } 413 break; 414 } 415 416 default: 417 { 418 break; 419 } 420 } 421 422 //set form size 423 int width = this.vScrollBar1.Width; 424 int height= this.panel1.Height; 425 width += this.lbCandidates.Items[0].Bounds.Right; 426 height += this.lbCandidates.Items[this.lbCandidates.Items.Count-1].Bounds.Bottom; 427 this.Width = width ; 428 this.Height = height + 5; //+5���F�n�� 429 } 430 431 #endregion 432 GetWidth()433 public int GetWidth() 434 { 435 return this.Width; 436 } 437 GetHeight()438 public int GetHeight() 439 { 440 return this.Height; 441 } 442 ShowNoActive()443 public void ShowNoActive() 444 { 445 if (!this.Visible) 446 UtilFuncs.SetVisibleNoActivate(this, true); // true to show. 447 } 448 HideNoActive()449 public void HideNoActive() 450 { 451 if (this.Visible) 452 UtilFuncs.SetVisibleNoActivate(this, false); // false to hide. 453 } 454 IMECandidateForm_Click(object sender, System.EventArgs e)455 private void IMECandidateForm_Click(object sender, System.EventArgs e) 456 { 457 System.Diagnostics.Debug.WriteLine("Click"); 458 } 459 lbCandidates_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)460 private void lbCandidates_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 461 { 462 //MessageBox.Show(this.lbCandidates.Parent.ToString()); 463 //SendKeys.SendWait((this.lbCandidates.SelectedIndex+1).ToString()); 464 } 465 IMECandidateForm_Activated(object sender, System.EventArgs e)466 private void IMECandidateForm_Activated(object sender, System.EventArgs e) 467 { 468 //ShowNoActive(); 469 //if(!isTouched) 470 { 471 //UtilFuncs.GiveBackFocus(m_hwnd); 472 System.Diagnostics.Debug.Write("Cand Active(); give back focus to "); 473 System.Diagnostics.Debug.WriteLine(m_hwnd); 474 // isTouched = true; 475 }//else 476 // isTouched = false; 477 } 478 IMECandidateForm_Deactivate(object sender, System.EventArgs e)479 private void IMECandidateForm_Deactivate(object sender, System.EventArgs e) 480 { 481 //HideNoActive(); 482 System.Diagnostics.Debug.WriteLine("Cand DeActive();"); 483 } 484 lbCandidates_Enter(object sender, System.EventArgs e)485 private void lbCandidates_Enter(object sender, System.EventArgs e) 486 { 487 System.Diagnostics.Debug.WriteLine("lbCandidates_Enter"); 488 } 489 lbCandidates_Leave(object sender, System.EventArgs e)490 private void lbCandidates_Leave(object sender, System.EventArgs e) 491 { 492 System.Diagnostics.Debug.WriteLine("lbCandidates_Leave"); 493 } 494 IMECandidateForm_Enter(object sender, System.EventArgs e)495 private void IMECandidateForm_Enter(object sender, System.EventArgs e) 496 { 497 System.Diagnostics.Debug.WriteLine("Cand Enter"); 498 } 499 IMECandidateForm_Leave(object sender, System.EventArgs e)500 private void IMECandidateForm_Leave(object sender, System.EventArgs e) 501 { 502 System.Diagnostics.Debug.WriteLine("Cand Leave"); 503 } 504 IMECandidateForm_Validated(object sender, System.EventArgs e)505 private void IMECandidateForm_Validated(object sender, System.EventArgs e) 506 { 507 508 System.Diagnostics.Debug.WriteLine("Cand Validated"); 509 } IMECandidateForm_Validating(object sender, System.ComponentModel.CancelEventArgs e)510 private void IMECandidateForm_Validating(object sender, System.ComponentModel.CancelEventArgs e) 511 { 512 System.Diagnostics.Debug.WriteLine("Cand Validating"); 513 514 } 515 lbCandidates_KeyDown(object sender, KeyEventArgs e)516 private void lbCandidates_KeyDown(object sender, KeyEventArgs e) 517 { 518 /* if (totalPage > 1 && e.KeyCode==Keys.Right) //�W�L�@���~���i�}�\�� 519 { 520 //expandable = true; 521 MessageBox.Show("fuck"); 522 }*/ 523 } 524 lbCandidates_KeyPress(object sender, KeyPressEventArgs e)525 private void lbCandidates_KeyPress(object sender, KeyPressEventArgs e) 526 { 527 /* if (totalPage > 1) 528 { 529 e.Handled = true; 530 MessageBox.Show("fuck"); 531 }*/ 532 } 533 } 534 } 535 536 537