Delphi creates an instance of its own cursor on a form
We know that in components such as text boxes that can receive input, we can see the blinking cursor and enter text. If we are on, for example, a form, because input is not supported and the blinking cursor cannot be displayed, then we Is there a way to do my own input? Of course you can. Below we demonstrate how to enter text on the Form.
The API functions used are as follows:
Delphi code
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 stores font information tm:TTextMetric; begin s := ''; GetTextMetrics(Self.Canvas. Handle,tm); { NoteCreateCaret The second parameter is the HBITMAP type, so you can use your own graphic as the cursor shape. The following two parameters are the default width and height of the cursor, which can be customized} CreateCaret(Self.Handle,HBITMAP(nil),tm .tmAveCharWidth div 2,tm.tmHeight); ShowCaret(Self.Handle); //Display SetCaretPos(10,10) at the point (10,,10); end; //Form key event, each time a key is pressed, the value of s is rewritten, and the value of s is changed in the OnPaint event. The value is drawn on the form procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char); begin //If it is the 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(10,10,s); //Reset the cursor position SetCaretPos(Self.Canvas.TextWidth(s)+10,10); end; end.VC code
//Global string variable CString s; //When initializing, set the cursor BOOL CTest_MFCDlg::OnInitDialog() { CDialog::OnInitDialog(); ShowSelfCaret(); ... } //Add functions to the form, Initialize cursor void CTest_MFCDlg::ShowSelfCaret(void) { CClientDC dc(this); TEXTMETRIC tm; dc.GetTextMetrics(&tm); CreateSolidCaret(tm.tmAveCharWidth/2,tm.tmHeight); ShowCaret(); POINT p; px = 0; py = 0; SetCaretPos(p); } //Overload PreTranslateMessage BOOL CTest_MFCDlg:: PreTranslateMessage(MSG* pMsg) { //If the key is pressed if (pMsg->message == WM_KEYDOWN) { //If it is the backspace key, delete the last character if (pMsg->wParam == VK_BACK) { if (s.GetLength() != 0 ) { s.Delete(s.GetLength() - 1,1); } } else //Append characters s.Insert(s.GetLength(),(TCHAR)pMsg->wParam); Invalidate(true); } return CDialog::PreTranslateMessage(pMsg); } //Self-draw, draw the content of s to the window void CTest_MFCDlg::OnPaint() { CPaintDC dc(this); CRect rect; GetClientRect(&rect); CSize size = dc.GetTextExtent(s); POINT p; px = size.cx; py = 0; SetCaretPos(p); dc.DrawText(s,s.GetLength(),rect,DT_LEFT); }If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help everyone. Thank you for your support of this site!