zoukankan      html  css  js  c++  java
  • 《转》Owner Draw Button StepbyStep

    原谅链接:http://www.codeguru.com/Cpp/controls/buttonctrl/article.php/c5157

    I think some of you may not like the buttons in Windows. Sometimes, I think they're ugly. Fortunately, we can change the appearance of our buttons by overriding the DrawItem function of the CButton class. I 'm going to demonstrate the steps of the owner drawing button. In this article, I will make a class, which inherits from CButton class.

    Let 's take a dialog-based MFC application as an example. In the File Menu, click New to add a new project. Then, choose MFC Application Wizard (exe). In the project name, type in OwnerDrawButton (just an example), and then click OK. In step one of the MFC App Wizard, choose Dialog Based. After pressing Finish, you are brought to the "New Project Information" page. You'll ignore this page, so press OK.

    The edit window is displaying the IDD_OWNERDRAWBUTTON_DIALOG. We have to make our class first, so we don't have time to look at this dialog. Go to the ClassView, right-click "OwnerDrawButton classes", and choose "New class". For the class type, just leave the default "MFC Class". In the "Name" editbox, type "CMyButton" (just an example). Choose "CButton" from the "Base Class".

    You have added a new class; it's time to override the DrawItem function. Right-click "CMyButton" in ClassView and choose "Add Virtual Function". The "New Virtual Override for class CMyButton" page opens. Double-click "DrawItem" in the left "New Virtual Functions" listbox. Afterward, 'DrawItem" will jump to the right "Existing virtual function overrides" listbox. Finally, press OK to add a new virtual override.

    Go to the implementation of CMyButton::DrawItem, which is in MyButton.cpp. Add draw code in this function. Here is my drawing code to demonstrate how to use CDC to draw it.


    void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
      CDC dc;
      dc.Attach(lpDrawItemStruct->hDC);     //Get device context object
      CRect rt;
      rt = lpDrawItemStruct->rcItem;        //Get button rect
    
      dc.FillSolidRect(rt, RGB(0, 0, 255)); //Fill button with blue color
    
      UINT state = lpDrawItemStruct->itemState; //Get state of the button
      if ( (state & ODS_SELECTED) )            // If it is pressed
      {
        dc.DrawEdge(rt,EDGE_SUNKEN,BF_RECT);    // Draw a sunken face
      }
      else
      {
        dc.DrawEdge(rt,EDGE_RAISED,BF_RECT);    // Draw a raised face
      }
    
      dc.SetTextColor(RGB(255,255,120)); 
                            // Set the color of the caption to be yellow
      CString strTemp;
      GetWindowText(strTemp); 
                            // Get the caption which have been set
      dc.DrawText(strTemp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE); 
                                  // Draw out the caption
      if ( (state & ODS_FOCUS ) )       // If the button is focused
      {
        // Draw a focus rect which indicates the user 
        // that the button is focused
        int iChange = 3;
        rt.top += iChange;
        rt.left += iChange;
        rt.right -= iChange;
        rt.bottom -= iChange;
        dc.DrawFocusRect(rt);
      }
      dc.Detach();
    }
    
    

    Your job isn't finished yet. Go back to the ResourceView and click "IDD_OWNERDRAWBUTTON_DIALOG". Yes, you are right! You are going to edit your dialog box. Drag a button to the dialog box. Now, modify its properties by right-clicking it and choosing "Properities". For its ID, call it "IDC_COLOREDBUTTON" and caption it "Colored Button". You also have to allow it to be owner draw. Go to the Styles tab and check "Owner Draw". Then, close the Properties dialog box.

    You have to link this button to the CMyButton class. Press Ctrl+W to open the MFC Class Wizard. Under the "Member variables" page, double-click "IDC_COLOREDBUTTON", which is in the "Control IDs" listbox. You are then brought to the "Add Member Variable" dialog. For the "Member variable name", type "m_MyColoredButton". For the "Variable Type", choose "CMyButton", and then press OK. VC++ will inform you to check if there is an include statement in "OwnerDrawButtonDlg.h". You won't find it, so add #include "MyButton.h" at the beginning of OwnerDrawButtonDlg.h.

    Press F7 to build your project. Afterward, run it and you will see the same as the picture at the beginning of this article.

    This is the first time for me to write an English article. Feel free to give some bad comments to me. I am glad to read them.


    本人新博客网址为:http://www.hizds.com
    本博客注有“转”字样的为转载文章,其余为本人原创文章,转载请务必注明出处或保存此段。c++/lua/windows逆向交流群:69148232
  • 相关阅读:
    我的算法日志:数据结构之顺序队列与循环队列
    我的算法日志:排序算法之快速排序
    算法:冒泡排序
    算法:桶排序(简易版)
    Android:配置LitePal 3.0
    Android:简单粗暴的二维码生成与扫描
    Linux
    Python
    Linux
    Python
  • 原文地址:https://www.cnblogs.com/zhangdongsheng/p/2388841.html
Copyright © 2011-2022 走看看