Create your own cursor on the form and enter text

Source: Internet
Author: User

We know that in the text box, such as can receive input components, we can see the flashing cursor, and can enter text, if we are in, such as the form, because the input is not supported, also cannot display the flashing cursor, then we have a way to do their own input? Of course, we'll show you how to enter text on a form.

The API functions used are as follows

GetTextMetrics: Gets the current font information for the program and stores it in the textmetric structure

CreateCaret: Creates a new shape for the system caret and assigns the owner of the insertion mark to a specific window. Inserts the shape of the marker. Can be a line, block, or bitmap

ShowCaret: Display cursor

Setcaretpos: Setting the position of the cursor

Delphi Code

[Delphi]View Plaincopy
  1. Unit Unit1;
  2. Interface
  3. Uses
  4. Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, Stdctrls;
  6. Type
  7. TForm1 = Class (Tform)
  8. procedure formcreate (sender:tobject);
  9. procedure formkeypress (sender:tobject;  var key:char);
  10. procedure Formpaint (sender:tobject);
  11. Private
  12. {Private declarations}
  13. S:string;
  14. Public
  15. {public declarations}
  16. end;
  17. Var
  18. Form1:tform1;
  19. Implementation
  20. {$R *.DFM}
  21. Procedure TForm1.  Formcreate (Sender:tobject);
  22. Var
  23. //ttextmetric Storing font information
  24. Tm:ttextmetric;
  25. Begin
  26. S: = ';
  27. GetTextMetrics (self. Canvas.  HANDLE,TM);
  28. Note that the second parameter of CreateCaret is the hbitmap type, so you can use your own graphics as the cursor shape, with the default
  29. The following two parameters are the width and height of the cursor and can be customized
  30. }
  31. CreateCaret (self.  Handle,hbitmap (nil), TM. Tmavecharwidth Div 2,tm. tmheight);
  32. ShowCaret (self.  Handle);
  33. //at (10,,10) this point shows
  34. Setcaretpos (Ten,10);
  35. End
  36. Form key event, each time a key is pressed, the value of S is rewritten, and the value of S is drawn on the form in the OnPaint event
  37. Procedure TForm1. Formkeypress (Sender:tobject;  var key:char);
  38. Begin
  39. //If it is a backspace key, delete the previous character
  40. if Ord (Key) = Vk_back Then
  41. begin
  42. if (s <> ") Then
  43. Delete (S,length (s),1);
  44. End
  45. Else
  46. S: = s + Key;
  47. // Redraw
  48. Self.  Invalidate;
  49. End
  50. Procedure TForm1.  Formpaint (Sender:tobject);
  51. Begin
  52. Self. Canvas.  TextOut (ten,10,s);
  53. //Reset cursor position
  54. Setcaretpos (self. Canvas.  TextWidth (s) +Ten,10);
  55. End
  56. End.

VC Code

[CPP]View Plaincopy
  1. Global string variables
  2. CString s;
  3. When initializing, set the cursor
  4. BOOL Ctest_mfcdlg::oninitdialog ()
  5. {
  6. CDialog::OnInitDialog ();
  7. Showselfcaret ();
  8. ......
  9. }
  10. Add a function to the form, initialize the cursor
  11. void Ctest_mfcdlg::showselfcaret (void)
  12. {
  13. CCLIENTDC DC (this);
  14. Textmetric TM;
  15. dc.  GetTextMetrics (&TM);
  16. Createsolidcaret (tm.tmavecharwidth/2,tm.tmheight);
  17. ShowCaret ();
  18. Point P;
  19. p.x = 0;
  20. P.Y = 0;
  21. Setcaretpos (P);
  22. }
  23. Heavy Duty PreTranslateMessage
  24. BOOL Ctest_mfcdlg::P retranslatemessage (msg* pMsg)
  25. {
  26. //If the button is pressed
  27. if (pmsg->message = = Wm_keydown)
  28. {
  29. //If the backspace key, delete the end character
  30. if (Pmsg->wparam = = Vk_back)
  31. {
  32. if (s.getlength ()! = 0)
  33. {
  34. S.delete (S.getlength ());
  35. }
  36. }
  37. Else
  38. //Append character
  39. S.insert (S.getlength (), (TCHAR) pmsg->wparam);
  40. Invalidate (true);
  41. }
  42. return CDialog::P retranslatemessage (PMSG);
  43. }
  44. Self-painting, drawing s content onto form
  45. void Ctest_mfcdlg::onpaint ()
  46. {
  47. CPaintDC DC (this);
  48. CRect rect;
  49. GetClientRect (&rect);
  50. CSize size = DC. GetTextExtent (s);
  51. Point P;
  52. p.x = size.cx;
  53. P.Y = 0;
  54. Setcaretpos (P);
  55. dc. DrawText (S,s.getlength (), rect,dt_left);
  56. }

Reference:

http://blog.csdn.net/bdmh/article/details/6456353

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.