In this c++ example hover button with two state images is based on the example
http://www.codeproject.com/KB/buttons/Cool_Buttons.aspx in codeproject.com
I deleted unnecessary codes and I added my stuff to it
I could not use SetStatus function so I wrote it every time I use it( 2 line functions)
Every thing that does not make changes I omitted it
DOWNLOAD The downloads contains complete file plus the exe in the folder of debug
The idea and the theme of this project is to have a button with two state hover
like the one shown in the images below,
The first image is when the mouse cursor on the right side and the second one is when the cursor is over the left side
These depend on the location of mouse cursor
To expand this project you can make multifunction buttons or double function buttons
or multiple hover button or another thing to add is to use 2 tooltips for a single button
1-Start visualc++ project MFC Application wizard let name a weird name tasho or tashi if you like and Chose Dialog and Deselct about Box and activeX controls ( no need for them)
and deselect compiler comments generated by the compiler
2-Rename the Dialog resource ID to IDD_TASHO_DIALOG
3-Add the following bitmap resources to resources go to resource View and Insert Insert Bitmap
and draw 5 bitmaps as shown here
Make the images of size 40 120
chose id for the bitmaps IDB_NORMAL ,IDB_LEFT ,IDB_RIGHT ,IDB CLICKED ,IDB_DOWN
note that IDB CLICKED is currently unused but it will be used in future programs
4- In the insert menu Insert new class MFC class Chose Cbutton as the base class for it and Name it CCoolButton
5- Add the following data members to the newly created class
and make them public
CBitmap m_bmp_normal ,m_bmp_left ,m_bmp_right ,m_bmp_clicked ,m_bmp_down;
enum ButtonStatus { NORMAL ,LEFT ,RIGHT ,CLICKED ,DOWN};
ButtonStatus m_old_status ,m_current_status ;
6-In the newly created class
Add the following constructor . five elements
as follows '
CCoolButton::CCoolButton(int nidNormal, int nidleft, int nidright, int nidclicked, int niddown)
{
m_current_status = NORMAL ;
m_bmp_normal.LoadBitmap(nidNormal);
m_bmp_left.LoadBitmap(nidleft);
m_bmp_right.LoadBitmap(nidright);
m_bmp_clicked.LoadBitmap(nidclicked);
m_bmp_down.LoadBitmap(niddown);
}
7- Add the 2 windows messege handler and edit them as follows
first OnleftbuttonDown
void CCoolButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_old_status = m_current_status ;
m_current_status = DOWN ;
SetCapture();
CButton::OnLButtonDown(nFlags, point);
}
and OnleftbuttonUp
void CCoolButton::OnLButtonUp(UINT nFlags, CPoint point)
{
CRect totalrect ;
GetClientRect(&totalrect);
CRect left = totalrect;
left.DeflateRect(60,0);
if( PtInRect(totalrect,point))
{
if(PtInRect(left ,point))
{
m_old_status = m_current_status ;
m_current_status = RIGHT ;
ReleaseCapture();
}
else {
m_old_status = m_current_status ;
m_current_status = LEFT ;
ReleaseCapture();
}}
else
m_old_status = m_current_status ;
m_current_status = NORMAL ;
ReleaseCapture();
CButton::OnLButtonUp(nFlags, point);
}
and OnMouseMove as follows
void CCoolButton::OnMouseMove(UINT nFlags, CPoint pnt)
{
CRect buttonrect , leftrect ;
GetWindowRect(buttonrect);
leftrect = buttonrect;
leftrect.DeflateRect(60 ,0);
if (m_current_status ==DOWN )
{
if( sRect(buttonrect ,pnt ))
{
Invalidate(FALSE) ; }
else{
m_old_status = m_current_status ;
m_current_status = CLICKED ;
Invalidate(FALSE) ;
}}
else if (m_old_status == DOWN && (PtInRect(buttonrect,pnt)))
{
m_old_status = m_current_status ;
m_current_status = DOWN ;
}
else {
if(sRect ( buttonrect, pnt ))
{if(sRect(leftrect , pnt))
{
m_old_status =m_current_status ;
m_current_status = LEFT ;
Invalidate(FALSE);
}
else
{
m_old_status =m_current_status ;
m_current_status = RIGHT ;
Invalidate(FALSE);
}
}
else {
m_old_status =m_current_status ;
m_current_status = NORMAL ;
Invalidate(FALSE);
}
}
TRACKMOUSEEVENT tm_Event ;
tm_Event.dwFlags = TME_LEAVE ;
tm_Event.hwndTrack = m_hWnd ;
tm_Event.cbSize =sizeof(TRACKMOUSEEVENT) ;
_TrackMouseEvent(& tm_Event );
CButton::OnMouseMove(nFlags, pnt);
}
As you can see I could not use SetStatus in the original example
Also add the virtual function DrawItem
void CCoolButton::DrawItem(LPDRAWITEMSTRUCT lp_di_struct)
{
CDC *DrawDc = CDC::FromHandle(lp_di_struct->hDC);
CBitmap * pOldBitmap ;
CDC membtmDC ;
membtmDC.CreateCompatibleDC(DrawDc) ;
switch( m_current_status)
{
case NORMAL :
pOldBitmap = membtmDC.SelectObject( & m_bmp_normal) ;
break ;
case LEFT:
pOldBitmap = membtmDC.SelectObject(& m_bmp_left);
break ;
case RIGHT :
pOldBitmap = membtmDC.SelectObject(& m_bmp_right);
break ;
case DOWN :
pOldBitmap = membtmDC.SelectObject(& m_bmp_down);
break ;
case CLICKED :
pOldBitmap = membtmDC.SelectObject(& m_bmp_clicked);
break ;
}
DrawDc->BitBlt(0,0, lp_di_struct->rcItem.right -lp_di_struct->rcItem.left , lp_di_struct->rcItem.bottom - lp_di_struct->rcItem.top , &membtmDC,0,0,SRCCOPY);
membtmDC.SelectObject(pOldBitmap);
membtmDC.DeleteDC();
}
As you can see it is almost the same as in the original example but there is 5 switch statements instead of 3 because we have here 5 bimaps notice I altered some names
Then add new function and name it sRect of type BOOL
BOOL CCoolButton::sRect(CRect &rct, CPoint &pnt)
{
return(pnt.x> 0 && pnt.x< (rct.right -rct.left) && pnt.y>0 && pnt.y< (rct.bottom -rct.top) )?TRUE :FALSE ; }
And finally add new function OnMouseLeave and edit as follows
void CCoolButton::OnMouseLeave()
{
m_old_status = m_current_status ;
m_current_status = NORMAL ;
Invalidate();
}
Since this function is part of CWnd class add the following to BeginMessageMap
ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
under
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
in ur class cpp file
Now we finished CCoolButton we move to the dialog class CTashoDlg
class
8-In the dialog resource make In the class wizard Add member variable chose the Ok button chose variable type CCoolButton and let us chose a name for it m_mono or anything you like and do not forget to add #include "CoolButton.h" in your dialog class header file
9-In the InitDialog Add the following code snippet
SetWindowText("hiee");
UpdateData(FALSE);
m_mono.MoveWindow( 30,45,150,90);
10- Delete the original constructor and replace it with the following constructor with no arguments
CTashoDlg::CTashoDlg() :CDialog(IDD_TASHO_DIALOG,NULL), m_mono(IDB_NORMAL ,IDB_LEFT ,IDB_RIGHT ,IDB_CLICKED ,IDB_DOWN)
{
}
11- Build the application and see the result
My next project would be a button with 2 ToolTips one on the left and one on the right I hope that you enjoyed it and benefited from it /.......
for more tutorials see other posts in the blog
It is hard and I made it loll
If you have any comment and any ideas you want to share leave it below :!~
Nice work. Keep it up.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletemoncler, moncler outlet, links of london, bottes ugg, moncler, moncler, doudoune canada goose, sac louis vuitton pas cher, juicy couture outlet, canada goose, moncler, pandora charms, pandora charms, replica watches, louis vuitton, pandora jewelry, lancel, canada goose, louis vuitton, ugg boots uk, swarovski, louis vuitton, canada goose uk, wedding dresses, moncler, ugg,uggs,uggs canada, hollister, supra shoes, juicy couture outlet, toms shoes, canada goose outlet, ugg pas cher, montre pas cher, canada goose, moncler, karen millen, swarovski crystal, louis vuitton, pandora jewelry, ugg,ugg australia,ugg italia, marc jacobs, thomas sabo, coach outlet, moncler, canada goose, canada goose outlet
ReplyDeletenike air max, hermes, oakley pas cher, burberry, coach outlet, coach outlet, lululemon, new balance pas cher, ugg boots, replica handbags, lacoste pas cher, vans pas cher, true religion jeans, michael kors, burberry outlet online, converse pas cher, true religion jeans, michael kors, hogan, sac guess, tn pas cher, coach purses, michael kors outlet, abercrombie and fitch, nike free run uk, michael kors, hollister pas cher, true religion outlet, kate spade handbags, vanessa bruno, timberland, ugg boots, nike air max, michael kors, north face, mulberry, north face, michael kors outlet, nike blazer, michael kors outlet, nike roshe, ralph lauren uk, michael kors outlet, air force, ray ban uk, nike air max, michael kors outlet, hollister, true religion jeans, ray ban pas cher
ReplyDeletejimmy choo shoes, converse outlet, north face outlet, insanity workout, soccer jerseys, wedding dresses, nike air max, ralph lauren, mcm handbags, oakley, hollister, lululemon, mont blanc, valentino shoes, vans shoes, vans, abercrombie and fitch, nike huarache, new balance, hollister, bottega veneta, nike trainers, birkin bag, celine handbags, ghd, soccer shoes, asics running shoes, instyler, timberland boots, beats by dre, nfl jerseys, converse, reebok shoes, nike roshe, hollister, ferragamo shoes, p90x workout, gucci, louboutin, north face outlet, iphone cases, babyliss, chi flat iron, baseball bats, longchamp, ray ban, nike air max, herve leger, mac cosmetics
ReplyDeletehermes online
ReplyDeletesupreme
spongebob kyrie 5
off white shoes
nike lebron shoes
lebron shoes
moncler
hermes birkin
yeezy boost 350
yeezy 700
go now dog dildo,dog dildo,sex toys,dildo,horse dildo,dog dildo,realistic dildo,dildos,wolf dildo useful reference
ReplyDeletepop over to these guys q1e30t7d88 replica bags china replica bags canada v7i23d9q95 try this web-site o4u17s0z95 replica louis vuitton bags replica bags chicago n3m77e2n12 replica bags china pop over to this site b1x28n2s69 replica bags from china free shipping
ReplyDeletekuşadası
ReplyDeletesakarya
sinop
bolu
burdur
B37