Answer

专注于Mobile,WinCE
  首页  :: 新随笔  :: 联系 :: 管理

[转]SelectObject() 装载字体 VC EVC

Posted on 2011-02-21 10:50  answer  阅读(557)  评论(0编辑  收藏  举报
用API 在创建的窗口上写字的时候发现一个问题,FONT   的句柄必须声明为全局变量,否则在选入字体的时候会返回失败。
VC 和EVC在创建字体的方法有点不大一样。VC版如下:

LRESULT CALLBACK    WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{ HDC hdc=NULL;
PAINTSTRUCT ps;
RECT rt;
HBRUSH hbrBkgnd;
static HPEN hpenDot;     // handle of dotted pen
static HFONT hFont;
LOGFONT lf;

switch(message){
case WM_CREATE:
        hpenDot = CreatePen(PS_DOT, 2, #ff0000); //创建虚线画笔

   hFont=CreateFont(                            // Create Font
    15,                      // nHeight
    0,                         // nWidth
    0,                         // nEscapement
    0,                         // nOrientation
    FW_NORMAL,                 // nWeight
    FALSE,                     // bItalic
    FALSE,                     // bUnderline
    0,                         // cStrikeOut
    ANSI_CHARSET,              // nCharSet
    OUT_DEFAULT_PRECIS,        // nOutPrecision
    CLIP_DEFAULT_PRECIS,       // nClipPrecision
    DEFAULT_QUALITY,           // nQuality
    DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
    _T("Arial"));              // lpszFacename


        break;
case WM_COMMAND:
       break; // do nothing
case WM_PAINT:
       SetWindowText(hWnd,"Text test");
       hdc = BeginPaint(hWnd, &ps);
       if(NULL==SelectObject(hdc,hFont))
           MessageBox(NULL,"fail font","debug",0);   
       if(NULL==SelectObject(hdc,hpenDot))
           MessageBox(NULL,"fail pen","debug",0);
       GetClientRect(hWnd, &rt);
       _stprintf(szMsg,"((%d,%d),(%d,%d))",rt.left,rt.top,rt.right,rt.bottom);
       //SetBkColor(hdc,#ff0000);
      // DrawText(hdc, szMsg, strlen(szMsg), &rt, DT_CENTER);
       TextOut(hdc,10,10,szMsg,strlen(szMsg));
       Rectangle(hdc,100, 100, 550,250);
       EndPaint(hWnd, &ps);
       UpdateWindow(ghWnd);
       break;
case WM_CLOSE:
         PostQuitMessage(0);
        break;
default:
      return DefWindowProc(hWnd,message,wParam,lParam);
}
return DefWindowProc(hWnd,message,wParam,lParam);
}


EVC 版:

LRESULT CALLBACK DlgProc_Panel(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDC     hdc;
static PAINTSTRUCT   ps;
    static RECT     rt;
    static HBRUSH    hBrush;
static COLORREF    wColor=#000000;
static int     nDrawTimes=0;
static HFONT    hFont;
static LOGFONT          m_logfnt;
TCHAR lfFaceName[32]= TEXT("Times New Roman");
m_logfnt.lfCharSet=DEFAULT_CHARSET;
m_logfnt.lfClipPrecision=CLIP_DEFAULT_PRECIS;
m_logfnt.lfEscapement=0;
m_logfnt.lfHeight=80;
m_logfnt.lfItalic=false;
m_logfnt.lfOrientation=0;
m_logfnt.lfPitchAndFamily=FF_SWISS;
m_logfnt.lfQuality=DEFAULT_QUALITY;
m_logfnt.lfStrikeOut=false;
m_logfnt.lfUnderline=true;
m_logfnt.lfWeight=800;
m_logfnt.lfWidth=20;
m_logfnt.lfOutPrecision=OUT_DEFAULT_PRECIS;


switch (message)
{
case WM_INITDIALOG:
   hFont=CreateFontIndirect(&m_logfnt);
   nDrawTimes=0;
   wColor=#ff0000;
   MoveWindowFullScreen(hDlg);
   break;
case WM_PAINT:
   hdc = BeginPaint(hDlg, &ps);
   GetClientRect(hDlg, &rt);
   hBrush=CreateSolidBrush(wColor);
   FillRect(hdc,&rt,hBrush);
   //TextOut(hdc, rt.left+10, rt.top+100, "Test", 4);
   SelectObject(hdc,hFont);
   SetTextColor(hdc,#0000ff);
   rt.top += rt.bottom/2;
   DrawText(hdc,TEXT("Test\n"),-1,&rt,DT_CENTER);
   DeleteObject(hBrush);
   EndPaint(hDlg, &ps);
   break;
case WM_LBUTTONDOWN:
case WM_KEYUP:
   InvalidateRect(hDlg,&rt,TRUE);
   EndDialog(hDlg,TRUE);
   break;
}
    return FALSE;
}