孫鑫: 第十講 繪圖

 

第十講  繪圖
對話框中的OKBUtton對應的是OnOk函數,這個函數可以認爲默認的會調用UpDateData使變量更新。
 If the dialog box includes automatic data validation and exchange, the default implementation of this member function validates the dialog-box data and updates the appropriate variables in your application.
 
 顏色對話框的創建
   用類CColorDialog。
  
   如果想要設置顏色對話框的初始顏色沒,應該設置CHOOSECOLOR結構體成員變量m_cc的flag設爲CC_RGBINIT,然後將想要初始化的顏色賦給rgbResult。
   見MSDN
    If the CC_RGBINIT flag is set, rgbResult specifies the color initially selected when the dialog box is created.
   可以在對話框構造函數中設置,也可以創建後在成員變量m_cc中設置。
   typedef struct {   // cc
    DWORD        lStructSize;
    HWND         hwndOwner;
    HWND         hInstance;
    COLORREF     rgbResult;
    COLORREF*    lpCustColors;
    DWORD        Flags;    //要初始化顏色必須設置這個
    LPARAM       lCustData;
    LPCCHOOKPROC lpfnHook;
    LPCTSTR      lpTemplateName;
} CHOOSECOLOR;

注意:
 dlg.m_cc.Flags = CC_RGBINIT;  這樣賦值是錯誤的,因爲m_cc已經有了一個標記???
 應該是
  dlg.m_cc.Flags |= CC_RGBINIT;
 
  初始化顏色的兩種方法:
  1. CColorDialog color;
      color.m_cc.Flags |= CC_RGBINIT;
      color.m_cc.rgbResult = initcol;
  2.  CColorDialog color(initcol);
  標記CC_FULLOPEN的意思是讓顏色設置窗口完全展開。
  獲取用戶選擇的顏色可以用m_cc結構體中的rgbResult,也可以用GetColor().
 
字體對話框的創建
  用類CFontDialog
 
  通過對話框返回的m_cc中的LPLOGFONT    lpLogFont;創建一個字體用以下方法
  CFont::CreateFontIndirect (lpLogFont);
 
  一個奇怪的問題
  CFont font;
  CFont *p = (CFont *)pDC->SelectObject(font);
  這樣居然沒有錯,而且能運行一次.
  正確的應該是
   CFont font;
   CFont *p = pDC->SelectObject(&font);
  
   當font已經創建好了,即已經和一種字體資源關聯起了,再次調用font.CreateFontIndirect()就會出錯,應該先釋放這個資源,
 用基類的函數CGdiObject::DeleteObject ()
 
    Deletes the attached Windows GDI object from memory by freeing all system storage associated with the Windows GDI object. The storage associated with the CGdiObject object is not affected by this call. An application should not call DeleteObject on a CGdiObject object that is currently selected into a device context.
   
    判斷一個字體時候已經和一種資源關聯,和以前的一樣,用句柄來判斷,CGdiObject::m_hObject ,從CGdiObject派生的都可以這樣。
    當編輯框上輸入改變的時候發送的是EN_CHANGE消息。
   
  怎樣改變對話框或者控件的背景顏色和文字顏色
 
  一個消息響應函數 繪製控件背景色。
  當要繪製控件時候發送WM_CTRLCOLOR消息
  所有控件繪製時候都會調用,想改變控件就需要在這個函數中進行區分,例如通過ID.
  if(pWnd->GetDlgCtrlID == ID_****)
  {
     CBrush brush;
     brush.CreateSolidBrush(RGB(255,0,0)); //改變背景色的畫刷
     pDC->SetTextColor(RGB(255,0,0)); //將控件文本顏色設置爲紅色
     pDC->SetBkMode(TRANSPARENT);     //文本背景設爲透明
     return brush;
  }
   CWnd::OnCtlColor 
  afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );
  
  Return Value
  
  OnCtlColor must return a handle to the brush that is to be used for painting the control background.
  
  CWnd::GetDlgCtrlID 
  int GetDlgCtrlID( ) const;
  
  Return Value
  
  The numeric identifier of the CWnd child window if the function is successful; otherwise 0.
  
  Remarks
  
  Returns the window or control ID value for any child window, not only that of a control in a dialog box. Since top-level windows do not have an ID value, the return value of this function is invalid if the CWnd is a top-level window.
  GetDlgCtrlID 返回一個子控件ID也可以返回其他子窗口。
  設置文字顏色
   pDC->SetTextColor()'
  設置文字背景
    CDC::SetBkMode 
    int SetBkMode( int nBkMode );
    
    Return Value
    
    The previous background mode.
    
    Parameters
    
    nBkMode
    
    Specifies the mode to be set. This parameter can be either of the following values:
    
    OPAQUE   Background is filled with the current background color before the text, hatched brush, or pen is drawn. This is the default background mode.
    
    
    TRANSPARENT   Background is not changed before drawing.
    
    將文字背景設置爲透明:
     pDC->SetBkMode(TRANSPARENT);
    
  
  如果要改變一個單行編輯框的背景顏色,除了要返回一個顏色畫刷以外,還要調用CDC::SetBkColor函數。
  To change the background color of a single-line edit control, set the brush handle in both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX message codes, and call the CDC::SetBkColor function in response to the CTLCOLOR_EDIT code.
  
  對於一個單行編輯框來說,應該如下來改變背景顏色和文字顏色:
  
 if(pWnd->GetDlgCtrlID == ID_EDIT1)
  {
     CBrush brush;
     brush.CreateSolidBrush(RGB(255,0,0)); //改變背景色的畫刷
     pDC->SetTextColor(RGB(255,0,0)); //將控件文本顏色設置爲紅色

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章