MFC 在一个button上画图画的一些方法
来源:互联网 发布:淘宝牛仔裤女店铺推荐 编辑:程序博客网 时间:2024/05/18 23:28
最近在做MFC方面的一些应用工作,成这个机会把MFC好好学学,学学没什么坏事,尽管MFC已经不是很方便了。
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.
class CMyClass : public CButton{// Constructionpublic:CMyClass();// Attributespublic:// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CMyClass)public:virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);//}}AFX_VIRTUAL// Implementationpublic:virtual ~CMyClass();// Generated message map functionsprotected://{{AFX_MSG(CMyClass)// NOTE - the ClassWizard will add and remove member functions here.//}}AFX_MSGDECLARE_MESSAGE_MAP()};
CMyClass::CMyClass(){}CMyClass::~CMyClass(){}BEGIN_MESSAGE_MAP(CMyClass, CButton)//{{AFX_MSG_MAP(CMyClass)// NOTE - the ClassWizard will add and remove mapping macros here.//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMyClass message handlersvoid CMyClass::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {// TODO: Add your code to draw the specified itemCDC dc;dc.Attach(lpDrawItemStruct->hDC);//Get device context objectCRect rt;rt = lpDrawItemStruct->rcItem;//Get button rectdc.FillSolidRect(rt, RGB(0, 0, 255));//Fill button with blue colorUINT state = lpDrawItemStruct->itemState;//Get state of the buttonif ( (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 yellowCString strTemp;GetWindowText(strTemp);// Get the caption which have been setdc.DrawText(strTemp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE);// Draw out the captionif ( (state & ODS_FOCUS ) ) // If the button is focused{// Draw a focus rect which indicates the user // that the button is focusedint iChange = 3;rt.top += iChange;rt.left += iChange;rt.right -= iChange;rt.bottom -= iChange;dc.DrawFocusRect(rt);}dc.Detach();}
class CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// COwnerDrawButtonDlg dialogCOwnerDrawButtonDlg::COwnerDrawButtonDlg(CWnd* pParent /*=NULL*/): CDialog(COwnerDrawButtonDlg::IDD, pParent){//{{AFX_DATA_INIT(COwnerDrawButtonDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void COwnerDrawButtonDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(COwnerDrawButtonDlg)DDX_Control(pDX, IDC_COLOREDBUTTON, m_MyColoredButton);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(COwnerDrawButtonDlg, CDialog)//{{AFX_MSG_MAP(COwnerDrawButtonDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// COwnerDrawButtonDlg message handlersBOOL COwnerDrawButtonDlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);// Set big iconSetIcon(m_hIcon, FALSE);// Set small icon// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}void COwnerDrawButtonDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void COwnerDrawButtonDlg::OnPaint() {if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSOR COwnerDrawButtonDlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}
- MFC 在一个button上画图画的一些方法
- MFC在VS2012上实现一些基本的界面方法
- MFC 获得控件的尺寸 在控件上画图
- MFC中,怎样在一个特定的控件里画图
- 用ChartCtrl在MFC上画图
- MFC中将一个Button控件失效的方法
- 常用的在CALayer上画图的方法
- matlab画图的一些方法
- 如何在你的IE上添加一个BUTTON
- Android--在一个Button上显示不同类型的字体
- 在MFC的图片控件上用OpenGL的glDrawPixels函数画图时指定起点坐标
- 关于MFC画图的一些总结,MFC (Draw)
- 关于MFC画图的一些总结,MFC (Draw)
- 在view上画一个button,自定义其按下和松开后的样式
- MFC VS2010 改变button上的字体大小
- MFC获得控件大小及在picturecontrol控件上画图
- Android在代码中添加一个Button的方法
- MFC画图方法汇总
- hibernate3 注解配置大全
- Tomcat下的appBase和docBase
- 什么是达芬奇技术?
- 杭电ACM 2003 求绝对值
- C++语言 如QQ般隐藏的窗体
- MFC 在一个button上画图画的一些方法
- LeetCode Binary Tree Inorder Traversal
- 学生类的输入与输出
- AE二次开发技巧之撤销、重做
- Android Handler学习总结
- C++语言 实现登录对话框
- C++语言 在对话框关闭时弹出确认窗口
- c#文件操作(初学者)
- 谷歌公布各版本Android份额 2.3占半壁江山