先贴代码
Graphics gph(hdc);
Pen pen(Color::Green);
SolidBrush br(Color::DarkCyan);
gph.FillRectangle(&br,50,50,250,25);
TCHAR str[] = L"fghrt"; Gdiplus::Font font(L"Arial",12); StringFormat stringFormat1;
SolidBrush textBrush(Color::Black); RectF rtf;
gph.DrawString(str,-1,&font,PointF(50.0,50.0),&stringFormat1,&textBrush);
gph.MeasureString(str,wcslen(str)-1,&font,PointF(50.0,50.0),&rtf); ShowCaret(hWnd);
SetCaretPos((int)(rtf.X+rtf.Width),52);
但插入符的位置偏差很大,有办法解决么GDI+
Graphics gph(hdc);
Pen pen(Color::Green);
SolidBrush br(Color::DarkCyan);
gph.FillRectangle(&br,50,50,250,25);
TCHAR str[] = L"fghrt"; Gdiplus::Font font(L"Arial",12); StringFormat stringFormat1;
SolidBrush textBrush(Color::Black); RectF rtf;
gph.DrawString(str,-1,&font,PointF(50.0,50.0),&stringFormat1,&textBrush);
gph.MeasureString(str,wcslen(str)-1,&font,PointF(50.0,50.0),&rtf); ShowCaret(hWnd);
SetCaretPos((int)(rtf.X+rtf.Width),52);
但插入符的位置偏差很大,有办法解决么GDI+
StringFormat strFormat = StringFormat::GenericTypographic();
strFormat.SetAlignment(StringAlignmentNear);
RectF rt(50,50,250,25);
Gdiplus::Font font(L"宋体",12);
FontFamily fontFamily;
font.GetFamily(&fontFamily);
RectF rcBou;
gph.MeasureString(str,str.GetLength()-9,&font,rt,&strFormat,&rcBou);
Pen pen(Color::Blue,1);
SolidBrush brush(Color::Red);
gph.DrawRectangle(&pen,rcBou);
gph.DrawString(str,str.GetLength(),&font,PointF(50,50),&strFormat,&brush);
StringFormat strFormat = StringFormat::GenericTypographic();
gph.MeasureString(str,str.GetLength()-9,&font,rt,&strFormat,&rcBou);
这两行代码是关键