CButton control: MFC How to add a picture to a button, showing three states, namely, click, Hover, the initial state (no click or hover).

Source: Internet
Author: User
How to add a picture to a button, show three states, click, Hover, Initial state (no click or hover).

First, prepare 3 images in BMP format with different statuses. For example:
Below I use vs2013 demonstration.1Create MFC Project (Basic dialog), I'm new here named Mfcapplication5dlg: click on two buttons (OK, cancel these two buttons) to set properties, Onwer drawn property set to True (in VC is "owner draw", tick), The function is a custom button window.2. Create a new class that inherits from the CButton class, and I'll create a new named Cpicbutton here. CPicButton.h
#pragma once

Cpicbutton
Class Cpicbutton:public CButton
{
Declare_dynamic (Cpicbutton)

Public
Cpicbutton ();
Virtual ~cpicbutton ();
The code that------------------------------------------added-------------------------------------------------------
void Setbitmapid (int nover, int nnormal, int npressed, int nfocus);//Set Picture status
int Nover (State of Mouse on button), int nnormal (initial state), int npressed (pressed state), int nfocus (FOCUS)
BOOL M_bover; The value is true when the mouse is above the button, and vice versa is Flase
BOOL m_btracking; True when the mouse button is not released
BOOL m_bselected; True when the button is pressed
BOOL M_bfocus; True if the button is the current focus

CBitmap m_normalbitmap;//Loading a picture of the initial state
CBitmap m_pressedbitmap;//Press the picture of the state
Picture of CBitmap m_focusbitmap;//Focus State
Picture of CBitmap m_overbitmap;//circling state
///----------------------------------------------------------------------------------
Protected
Declare_message_map ()
Public
//3. Adding a message response function in the Class wizard
Here's the message function, in the Class wizard, add 3 message functions: OnMouseMove (), OnMouseLeave (), Onmousehover ().
Note: Some IDE versions of the Class Wizard do not have the OnMouseLeave (), Onmousehover () two message functions. I use the vs2013 (update5) version, so there will be these two functions.
Your IDE (such as vc6.0,vs2012, etc.) does not have these message functions, it can be customized manually, the same effect.
Add Manually:
afx_msg LRESULT OnMouseLeave (WPARAM WPARAM, LPARAM LPARAM);
afx_msg LRESULT onmousehover (WPARAM WPARAM, LPARAM LPARAM);
After adding these two lines of code, it is not finished, to find the line of code in CPciButton.cpp that begins with Begin_mess_map, as follows:
Begin_message_map (Ccontrolbutton, CButton)
{{Afx_msg_map (Ccontrolbutton)
Note-the ClassWizard would add and remove mapping macros here.
On_wm_mousemove ()
----------------------The following code is manually added------------------------------------
On_message (Wm_mouseleave, OnMouseLeave)
On_message (Wm_mousehover, Onmousehover)
///---------------------------------------------------------------------------------------------
}}afx_msg_map
End_message_map ()
-----------------------------------------------The following are the message functions that I added directly to the Class wizard using the vs2013 version, which is the custom Add method. ------------------------
afx_msg void OnMouseMove (UINT nflags, CPoint point);
afx_msg void OnMouseLeave ();
afx_msg void Onmousehover (UINT nflags, CPoint point);
//----------------------------------------------------------------------------------------------------------
//4. Add a virtual function DrawItem in the Class Wizard.
virtual void DrawItem (lpDrawItemStruct/*lpdrawitemstruct*/);
};

PicButton.cpp: Implementing Files
//

#include "stdafx.h"
#include "MFCApplication5.h"
#include "PicButton.h"


Cpicbutton

Implement_dynamic (Cpicbutton, CButton)

Cpicbutton::cpicbutton ()
{
m_bover/* (mouse over button) */= FALSE;
m_bselected/* (Button pressed) */= FALSE;
m_btracking/* (released under mouse press)/= FALSE;
M_bfocus/* button is the current focus */= FALSE;
}

Cpicbutton::~cpicbutton ()
{
}
Begin_message_map (Cpicbutton, CButton)
On_wm_mousemove ()
On_wm_mouseleave ()
On_wm_mousehover ()
End_message_map ()
Cpicbutton message handlers

void Cpicbutton::onmousemove (UINT nflags, CPoint Point)
{
TODO: Add Message Handler code and/or call default values here
if (!m_btracking)
{
TrackMouseEvent tme;
tme.cbsize = sizeof (TME);//cbsize defines the size of the trackmouseevent structure;
Tme.hwndtrack = M_hwnd;//hwndtrack The handle of the window to be traced
Tme.dwflags = Tme_leave | tme_hover;//Service request, mouse move position, leave and hover button event consumption time, that is, change the status of the failure and the effectiveness of
Tme.dwhovertime = 100;//cursor on the button, changing the state of time, in 1 milliseconds
m_btracking = _trackmouseevent (&tme);
} cbutton::onmousemove (nflags, point);

}
Manually added is the
LRESULT Ccontrolbutton::onmouseleave (WPARAM WPARAM, LPARAM LPARAM)
{
M_bover = FALSE; The mouse is not above the button
m_btracking = FALSE; Mouse away button
InvalidateRect (NULL, FALSE); Let the button repaint
Return 0;//note to return a value.
}
void Cpicbutton::onmouseleave ()
{
TODO: Add Message Handler code and/or call default values here
M_bover = FALSE; The mouse is not on top of the button
m_btracking = FALSE; Mouse away button
InvalidateRect (NULL, FALSE); Let the button repaint

Cbutton::onmouseleave ();
}

Manually added is the
LRESULT Ccontrolbutton::onmousehover (WPARAM WPARAM, LPARAM LPARAM)
{

M_bover = TRUE; Hover the mouse over the button
InvalidateRect (NULL); Re-painting.
return 0;
}
void Cpicbutton::onmousehover (UINT nflags, CPoint Point)
{
TODO: Add Message Handler code and/or call default values here

M_bover = TRUE; Hover the mouse over the button
InvalidateRect (NULL); Re-painting.

Cbutton::onmousehover (nflags, point);
}


void Cpicbutton::D rawitem (lpdrawitemstruct lpdrawitemstruct)
{
CRect rect = lpdrawitemstruct->rcitem;
CDC *PDC = Cdc::fromhandle (LPDRAWITEMSTRUCT->HDC);
int nsavedc = Pdc->savedc ();
UINT state = lpdrawitemstruct->itemstate;

Based on the state map of the button
(The following post-button state diagram, in the following cases to paste,, and we usually paste background image of the same practice)
The state of the mouse hover button
if (m_bover)
{

(The following button map, can be used in our own previous method to paste the background map to do)

CDC MEMDC;
Memdc.createcompatibledc (PDC);
CBitmap *poldbmp;
Poldbmp = Memdc.selectobject (&M_OVERBITMAP);//Load Dwell State
Pdc->bitblt (0, 0, rect. Width (), Rect. Height (), &MEMDC, 0, 0, srccopy);

}

Else
{

Initial state
CDC MEMDC;
Memdc.createcompatibledc (PDC);
CBitmap *poldbmp;
Poldbmp = Memdc.selectobject (&M_NORMALBITMAP);//Load Initial state
BITMAP bmp;
M_normalbitmap.getobject (sizeof (BMP), &bmp);
Pdc->bitblt (0, 0, rect. Width (), Rect. Height (), &MEMDC, 0, 0, srccopy);
}

button is pressed (selected)
if (State & ods_selected)
{
CDC MEMDC;
Memdc.createcompatibledc (PDC);
CBitmap *poldbmp;
Poldbmp = Memdc.selectobject (&m_pressedbitmap);
BITMAP bmp;
Poldbmp->getbitmap (&bmp);
M_pressedbitmap.getobject (sizeof (BMP), &bmp);
Pdc->bitblt (0, 0, rect. Width (), Rect. Height (), &MEMDC, 0, 0, srccopy);
}

TODO: Add your code to draw the specified item
}
The ID load function of the button picture, which is called in the main dialog box of the following steps
void Cpicbutton::setbitmapid (int nover, int nnormal, int npressed, int nfocus)
{
M_normalbitmap.loadbitmap (Nnormal);
M_pressedbitmap.loadbitmap (npressed);
M_focusbitmap.loadbitmap (Nfocus);
M_overbitmap.loadbitmap (Nover);

}
6.
In the resource view, introduce bitmap six pictures:
3 pictures of the "OK" button, where I set their ID to be IDB_OK (initial state), IDB_OK2 (circled State), IDB_OK2 (pressed state).
3 pictures of the "Cancel" button, which I set here their ID is idb_cancel1 (initial state), Idb_okcancel2 (circled State), IDB_CANCEL3 (pressed state).
7.
In the main dialog mfcapplication5dlg, in the Build Class Wizard to add member variables to the two buttons, I set the "OK" button variable to M_OK, set the "Cancel" button to M_cancel.
In MFCApplication5Dlg.h, add # include "PicButton.h" This line of code, and then add the previously added button member variable class name, CButton changed to Cpicbutton.
For example:
Public
Cpicbutton M_cancel;
Cpicbutton M_OK;
8.
In MFCApplication5Dlg.cpp, add the following code in OnInitDialog ():

BOOL Cmfcapplication5dlg::oninitdialog ()
{
Cdialogex::oninitdialog ();

Add the "About ..." menu item to the System menu.
......
......
SetIcon (M_hicon, TRUE); Set Large Icons
SetIcon (M_hicon, FALSE); Set Small Icons

TODO: Add additional initialization code here
-------------------------the button to paint the added code-----------------------------------------
M_ok. Setbitmapid (Idb_ok2, IDB_OK, IDB_OK3, IDB_OK);
M_cancel. Setbitmapid (Idb_cancel2, Idb_cancel1, Idb_cancel3, Idb_cancel1);
-------------------------------------------------------------------------------------
return TRUE; Returns TRUE unless focus is set to the control
}
After completing the above steps, run the program as shown below:

Code DOWNLOAD Link:
http://download.csdn.net/detail/chen1083376511/9872130

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.