365体育网址

主页 > 风度男人 >

利用字体通用对话框(CFontDialog)设置编辑框中的

2020-07-17 07:48

  最近这个问题一直在困扰着我,网上查找了好多资料,这一部分的代码早就熟悉的快烂掉了,最后发现问题居然是出现在两个函数上,一直没注意CreateFontIndirect与CreatePointFontIndirect的区别:

  CreateFontIndirect:

  When the font is selected by using the

  CDC::SelectObject member function, the GDI’s font mapper

  attempts to match the logical font with an existing physical

  font.

  CreatePointFontIndirect:

  This function is the same as

  CreateFontIndirect except that the lfHeight</B> member of

  the LOGFONT is interpreted in tenths of a point rather than

  device units. This function automatically converts the height in

  lfHeight</B> to logical units using the CDC object

  pointed to by pDC before passing the LOGFONT

  structure on to Windows.

  之前一直在用着CreatePointFontIndirect这个函数创建字体,发现字体的格式能够正确创建,但是字体大小一直不对,不管你选多大的字号,字体都是小的看不清,后面把CreatePointFontIndirect换成CreateFontIndirect就正常了,确实坑爹啊!

  附上我的代码:

  void

  CNetChatDlg::OnBtnFont()

  //创建字体对话框的按钮响应函数

  {

  // TODO: Add your control notification handler

  code here

  GetDlgItem(IDC_EDIT_SEND)->GetFont()->GetLogFont(&lf);

  CFontDialog

  dlg(&lf);

  //初始化字体对话框中的字体信息

  if(IDOK==dlg.DoModal())

  {

  if(m_wordstyle.m_hObject)

  m_wordstyle.DeleteObject();

  LOGFONT logfont;

  dlg.GetCurrentFont(&logfont);

  //获取选择的字体信息

  // int

  add=(((-logfont.lfHeight)%4)>0)? 1:0;

  // int

  fontsize=-logfont.lfHeight-((-logfont.lfHeight)/4)-add;

  // CClientDC dc(this);

  // logfont.lfHeight=-MulDiv(fontsize,

  dc.GetDeviceCaps(LOGPIXELSY), 72);

  //这个注释部分是字体的lfHeight与字号的关系,这个方法试了好久,但是没用

  m_wordstyle.CreateFontIndirect(&logfont);

  //m_wordstyle为类成员,CFont类型

  GetDlgItem(IDC_EDIT_SEND)->SetFont(&m_wordstyle);

  Invalidate();

  }

  }

本文信息
    发布时间:2020-07-17 07:48
    作者:admin
    所属栏目:风度男人
    本文关键词: 利用,字体,通用,对话框,CFontDialog,设置,编辑
所有分类