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
- Unit Unit1;
- Interface
- Uses
- Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
- Dialogs, Stdctrls;
- Type
- TForm1 = Class (Tform)
- procedure formcreate (sender:tobject);
- procedure formkeypress (sender:tobject; var key:char);
- procedure Formpaint (sender:tobject);
- Private
- {Private declarations}
- S:string;
- Public
- {public declarations}
- end;
- Var
- Form1:tform1;
- Implementation
- {$R *.DFM}
- Procedure TForm1. Formcreate (Sender:tobject);
- Var
- //ttextmetric Storing font information
- Tm:ttextmetric;
- Begin
- S: = ';
- GetTextMetrics (self. Canvas. HANDLE,TM);
- {
- 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
- The following two parameters are the width and height of the cursor and can be customized
- }
- CreateCaret (self. Handle,hbitmap (nil), TM. Tmavecharwidth Div 2,tm. tmheight);
- ShowCaret (self. Handle);
- //at (10,,10) this point shows
- Setcaretpos (Ten,10);
- End
- 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
- Procedure TForm1. Formkeypress (Sender:tobject; var key:char);
- Begin
- //If it is a backspace key, delete the previous character
- if Ord (Key) = Vk_back Then
- begin
- if (s <> ") Then
- Delete (S,length (s),1);
- End
- Else
- S: = s + Key;
- // Redraw
- Self. Invalidate;
- End
- Procedure TForm1. Formpaint (Sender:tobject);
- Begin
- Self. Canvas. TextOut (ten,10,s);
- //Reset cursor position
- Setcaretpos (self. Canvas. TextWidth (s) +Ten,10);
- End
- End.
VC Code
[CPP]View Plaincopy
- Global string variables
- CString s;
- When initializing, set the cursor
- BOOL Ctest_mfcdlg::oninitdialog ()
- {
- CDialog::OnInitDialog ();
- Showselfcaret ();
- ......
- }
- Add a function to the form, initialize the cursor
- void Ctest_mfcdlg::showselfcaret (void)
- {
- CCLIENTDC DC (this);
- Textmetric TM;
- dc. GetTextMetrics (&TM);
- Createsolidcaret (tm.tmavecharwidth/2,tm.tmheight);
- ShowCaret ();
- Point P;
- p.x = 0;
- P.Y = 0;
- Setcaretpos (P);
- }
- Heavy Duty PreTranslateMessage
- BOOL Ctest_mfcdlg::P retranslatemessage (msg* pMsg)
- {
- //If the button is pressed
- if (pmsg->message = = Wm_keydown)
- {
- //If the backspace key, delete the end character
- if (Pmsg->wparam = = Vk_back)
- {
- if (s.getlength ()! = 0)
- {
- S.delete (S.getlength ());
- }
- }
- Else
- //Append character
- S.insert (S.getlength (), (TCHAR) pmsg->wparam);
- Invalidate (true);
- }
- return CDialog::P retranslatemessage (PMSG);
- }
- Self-painting, drawing s content onto form
- void Ctest_mfcdlg::onpaint ()
- {
- CPaintDC DC (this);
- CRect rect;
- GetClientRect (&rect);
- CSize size = DC. GetTextExtent (s);
- Point P;
- p.x = size.cx;
- P.Y = 0;
- Setcaretpos (P);
- dc. DrawText (S,s.getlength (), rect,dt_left);
- }
Reference:
http://blog.csdn.net/bdmh/article/details/6456353