5/25/09

setw example

In this vc++ set example

start new cpp file and name it fano

copy the code below and see the output

#include
#include
using namespace std;

int main () {

int Numa = 1234567890 ;

int NumB = 5432 ;int NumC =2324234 ;
cout << "Numa = " <<><
cout << "Numa = " <<><
<< numb ="\n"><<< endl ;
cout<< " NumC\n " <<<
return 0;
}



set different value in the function and see the outcome

This is compiled using VC++ 6 compiler

try different ones

5/21/09

c++ dll example

In this c++ dll example we used a dlll to perform simple mathematical operation which is to convert from Celsius to Fahrenheit and vice versa





The first step is creating the dll
1 - Start new Win32 Dynamic link library and name it famo chose simple dll project the middle one
2- Open the code and add the following
__declspec(dllexport)void Fehrenhite_Celcius_converter(int n_operation, double input , double &result) ;
// we used reference here since we want to change the value of output inside the function
below #include "stdafx.h"

and at the buttom
add the following

void Fehrenhite_Celcius_converter(int n_operation, double input , double &result)
{
switch (n_operation) {
case 0 :
result= 1.8 * input +32 ;
break ;
case 1 :
result = ( ( double) 5/ (double) 9) * (input -32) ;

break ;
}
// casting needed here bec 5/9 is considered 0
break ; }
}

compile the dlll

Now start new MFC name it mydll_program dialog application and chose not to select the about box and name iy Celcius_Fehrenhite converter

3- Add the 2 static controls and 2 Edit controls a button , group box and 2 radio buttons as shown above

In the group box name it converting and in the static boxes in the properties chose caption input and for the second one chose caption output

Add member variables in the class wizards m_input for the first edit box and m_outputs for the second edit box both of type CString
Add a variable of type int integer for the first radio button m_operation
in the properties chose group

4- after compiling the dlls copy the lib and dll to dialog directory

5- In the project menu Add to projects then chose files as shown here





In the class wizard add member function for the calculate button and name it On_Conversion or anything

Add the following line at the top of the dialog class
__declspec(dllimport)void Fehrenhite_Celcius_converter(int n_operation, double input , double &result) ;
To include the dlls

put in the On_Conversion add follows


void CMydll_programDlg::On_Conversion()
{
UpdateData();
double inputs , outputs ;
inputs = atof(m_input);
// temperature input
Fehrenhite_Celcius_converter(m_operation , inputs ,outputs) ;
// m_operation represents the type of conversion
m_outputs.Format("%2.3f", outputs);
UpdateData(FALSE);
}

build and compile your program

This lesson teaches you how to make simple static dll application for more see other links in the blog

5/19/09

double action mfc c++ button








in this double action mfc c++ button tutorial we created twofold hover button with 2 tooltips


Depending on the location of the mouse cursor it performs different action whether it is addition or subtraction







DOWLOADS

This tutorial use reflected message to handle the onclicked message since it is a custom control the mouse clicked can not be handled in the dialog class

It is the hardest example I have ever made

To start the application


1- Start new MFC exe application and name it Hashi or else accept default settings
2- In the resource View make add the following bitmaps

and name them as IDB_BITMAP_PLUS_DOWN , IDB_MINUS_HOVER , IDB_MINUS_DOWN , IDB_LUS_HOVER ,IDB_BITMAP_PLUS_DOWN in order ;
make of dimension 200 ,60 pixels do u better if you have

3- Insert new class from insert menu and name it C_Double_task derived from CButton
4-Add the following data members
CBitmap m_bmp_normal_pic ,m_bmp_subtract_hover ,m_bitmap_subtract_down,m_bt_addition_hover ,m_bmp_addition_pushed;
enum Btn_Condition{ NORMAL ,MINUS_HOVER ,MINUS_DOWN,PLUS_OVER,PLUS_DOWN} ;
Btn_Condition m_previous_Mode ,m_present_mode ;
// five bitmaps and enumerators for statuses

5-Add new constructor ( five parameters ) as follows

C_Double_Task::C_Double_Task(int nornel, int negative_over, int neg_dowm, int plus_hovrt, int plus_pressed)
{

m_present_mode =NORMAL ;
/

m_bmp_normal_pic.LoadBitmap(nornel) ;
m_bmp_subtract_hover.LoadBitmap(negative_over) ;
m_bitmap_subtract_down.LoadBitmap(neg_dowm) ;
m_bt_addition_hover.LoadBitmap(plus_hovrt);
m_bmp_addition_pushed.LoadBitmap(plus_pressed) ;
// loading pictures
}


5-Add the following message handlers and virtual functions

first with DrawItem virtual function


void C_Double_Task::DrawItem(LPDRAWITEMSTRUCT lp_Draw_Items_Struct)
{

CDC *pMyDC =CDC::FromHandle(lp_Draw_Items_Struct->hDC);
CDC memDC_item ;

memDC_item.CreateCompatibleDC(pMyDC);
CBitmap* p_bitmap_mfc ;
switch(m_present_mode) {
// or use if statements
case NORMAL :
p_bitmap_mfc = memDC_item.SelectObject( &m_bmp_normal_pic);
break ;
case MINUS_HOVER :
p_bitmap_mfc = memDC_item.SelectObject( &m_bmp_subtract_hover);

break ;
case MINUS_DOWN :

p_bitmap_mfc = memDC_item.SelectObject( &m_bitmap_subtract_down);

break ;

case PLUS_OVER :
p_bitmap_mfc = memDC_item.SelectObject( &m_bt_addition_hover);

break ;

case PLUS_DOWN :

p_bitmap_mfc = memDC_item.SelectObject( &m_bmp_addition_pushed);
break ;
}
pMyDC->BitBlt( 0,0, lp_Draw_Items_Struct->rcItem.right -lp_Draw_Items_Struct->rcItem.left ,lp_Draw_Items_Struct->rcItem.bottom -lp_Draw_Items_Struct->rcItem.top ,&memDC_item ,0,0,SRCCOPY); memDC_item.SelectObject(p_bitmap_mfc) ; memDC_item.DeleteDC(); }

void C_Double_Task::OnLButtonDown(UINT nFlags, CPoint point)
{
SetCapture();

CRect ButtonRect ;
GetClientRect(ButtonRect) ;

m_previous_Mode =m_present_mode ;
m_present_mode =PLUS_DOWN ;
if(point.x > ButtonRect.left +ButtonRect.Width()/2 {
m_previous_Mode =m_present_mode ;
m_present_mode =MINUS_DOWN ;
}
CButton::OnLButtonDown(nFlags, point); }

void C_Double_Task::OnLButtonUp(UINT nFlags, CPoint point)
{

CRect upRect ;
GetClientRect(upRect) ;
CRect rect_left = upRect ;

rect_left .right = upRect.right/2 ;
if(PtInRect(upRect,point)) {
if(PtInRect(rect_left,point )) {
m_previous_Mode =m_present_mode ;

m_present_mode =PLUS_OVER ;
ReleaseCapture();

}

else
{
m_previous_Mode =m_present_mode ;
m_present_mode =MINUS_HOVER ;
ReleaseCapture();
}
}
else {

m_previous_Mode =m_present_mode ;
m_present_mode =NORMAL ;
ReleaseCapture();

}
CButton::OnLButtonUp(nFlags, point); }
void C_Double_Task::OnMouseMove(UINT nFlags, CPoint point)
{
CRect rec_total ;
GetClientRect(rec_total) ;
CRect Rec_right = rec_total ;
Rec_right.left = rec_total.right/2 ;
CRect Rct_left = rec_total; Rct_left.right =rec_total.right/2 ;
if( PtInRect( rec_total ,point )){
if( PtInRect( Rec_right ,point )){
m_previous_Mode =m_present_mode ;
m_present_mode =MINUS_HOVER ;
Invalidate(FALSE) ;
} else
{
m_previous_Mode =m_present_mode ;
m_present_mode =PLUS_OVER ;
Invalidate(FALSE) ;
} } else if((m_previous_Mode == PLUS_DOWN || m_previous_Mode ==MINUS_DOWN) && PtInRect(Rec_right ,point)){
m_previous_Mode =m_present_mode ;
m_present_mode =MINUS_DOWN ; } else if((m_previous_Mode == PLUS_DOWN || m_previous_Mode ==MINUS_DOWN) && PtInRect(Rct_left ,point)){ m_previous_Mode =m_present_mode ; m_present_mode =PLUS_DOWN ; } else{ m_previous_Mode =m_present_mode ; m_present_mode =NORMAL ; Invalidate(FALSE) ; } TRACKMOUSEEVENT tn_Events ;
tn_Events.cbSize =sizeof(TRACKMOUSEEVENT) ;

tn_Events.dwFlags = TME_LEAVE ;

tn_Events.hwndTrack= m_hWnd ;
_TrackMouseEvent(&tn_Events) ;
CButton::OnMouseMove(nFlags, point);
}


void C_Double_Task::OnMouseLeave() {
m_previous_Mode =m_present_mode ;
m_present_mode =NORMAL ;
Invalidate() ;
}

and finally add onclicked
afx_msg void OnClicked(); in class header file

ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)

in the begin message map section and add this to your cpp file void C_Double_Task::OnClicked() { DWORD dwpos = GetMessagePos() ;
CPoint pt ;

pt.x = LOWORD(dwpos );
pt.y = HIWORD(dwpos) ;
CRect client_rectangle ;
GetClientRect( client_rectangle) ;
CString strA , strB ;
double db_a ,db_b ;
GetParent()->GetDlgItemText(IDC_NUM1,strA) ; GetParent()->GetDlgItemText(IDC_NUM2,strB) ;
db_a = atof(strA);
db_b= atof(strB);
double result ;
result = db_b + db_a ;
CRect leftRect ;
leftRect = client_rectangle ;
leftRect.right = client_rectangle .left +client_rectangle.Width()/2;
if(leftRect.PtInRect(pt)) result = db_b - db_a ;
CString Str_result ;
Str_result.Format("%3.2f",result);
GetParent()->SendMessage(UDM_REGION_CLICKED, result);
}
lastly add this to your class header ;
static const UINT UDM_REGION_CLICKED = ::RegisterWindowMessage(_T("UDM_REGION_CLICKED-{23C118F8-01}"));

now we finished with the custom class Derived from Cbutton

Now we are in the dialog class
6- In the class wizard add the following data member
7- edit the constructor as follows
CHashiDlg::CHashiDlg(CWnd* pParent /*=NULL*/) : CDialog(CHashiDlg::IDD, pParent),m_two_actions_botn(IDB_NORMAL ,IDB_MINUS_HOVER ,IDB_MINUS_DOWN , IDB_LUS_HOVER , IDB_BITMAP_PLUS_DOWN)
{
//{{AFX_DATA_INIT(CHashiDlg) m_numB = _T(""); //}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); }


8-as pretranslate message handler as follows
C_Double_Task m_two_actions_botn;
and for edit controls add member variable with names like m_numA , m_numB ,m_results of type string

in the dialog class header add afx_msg LRESULT OnRegionClicked( WPARAM wParam, LPARAM lParam );

in the begin message map section in the cpp file ON_REGISTERED_MESSAGE(UDM_REGION_CLICKED, OnRegionClicked)
and at the end of the cpp add
LRESULT CHashiDlg::OnRegionClicked( WPARAM wParam, LPARAM param ) {
CString resl ;

resl.Format("%3.3",wParam);

SetDlgItemText( IDC_RESULT , resl) ;
return (0);
}

finally build the application and trying clicking on the right half and left half
BOOL CHashiDlg::PreTranslateMessage(MSG* pMsg) {
if( m_vc_tooltip.m_hWnd) m_vc_tooltip.RelayEvent(pMsg) ;
return CDialog::PreTranslateMessage(pMsg); }

Note this is a draft not working

5/9/09

c++ hover button with two tooltips

In this c++ hover button with two tooltips tutorial we create a c++ button control with two tooltips one on the left half and the other on the right side

The tooltips text on the leftis different from one on the right and change in
the event of mouse move in the mouse rectangle



To Get the project along the exe click here note that exe is in the debug folder


With the help of codeproject I made this application


To Create the application

1- Start new MFC appWizard( Exe) Dialog based application and give it a name like Tache or any other weird name you want !!?

2- In The Resource editor add the follow images for hover animation

and name them as follows IDB_NORMAL ,IDB_OVER and IDB_DOWE respectively ( the ids of the bitmaps

3-Insert new class in the insert menu or from class view and name it CoolButton
4- Add the following data members to the newly created class ( public)
CBitmap m_bm_Normal ,m_bmp_over ,m_bmp_pushed ;
// three bitmaps and status enumerators
enum Buttom_status { NORMAL ,Hoverimage ,CLICKED} ;
Buttom_status m_previous_state ,m_current_status ;
5- Add new constructor in the new class and edit it as follows

CoolButton::CoolButton(int regular, int hover, int clicjed)
{


m_current_status = NORMAL ;

m_bm_Normal.LoadBitmap(regular) ,
m_bmp_over.LoadBitmap(hover) ,
m_bmp_pushed.LoadBitmap(clicjed) ;
// associate each variable ( Cbitmap with a bitmap )
}

6- Add the following windows messege handlers and virtual functions

Onleftmousedown , onleftmouseup , onmouse_move and Drawitem and edit them as follows

void CoolButton::OnLButtonDown(UINT nFlags, CPoint point)
{


m_previous_state = m_current_status ;
m_current_status = CLICKED ;
// now the button is clicked
SetCapture();

CButton::OnLButtonDown(nFlags, point);
}

and
void CoolButton::OnLButtonUp(UINT nFlags, CPoint point)
{

CRect totals_Rect ;

GetClientRect(totals_Rect) ;


if(On_Recto(totals_Rect ,point))
{

m_previous_state = m_current_status ;
m_current_status = Hoverimage ;
ReleaseCapture();

// if you release the left button while you are inside the button boundaries
}
else {
m_previous_state = m_current_status ;
m_current_status = NORMAL ;
ReleaseCapture();


}





CButton::OnLButtonUp(nFlags, point);
}

void CoolButton::OnMouseMove(UINT nFlags, CPoint point)
{



CRect mybutton_rect ;
GetClientRect(mybutton_rect) ;

if( m_previous_state == CLICKED && (On_Recto(mybutton_rect ,point)))

{

m_previous_state = m_current_status ;
m_current_status = CLICKED ;


}

else if(On_Recto(mybutton_rect ,point))


{

m_previous_state = m_current_status ;
m_current_status = Hoverimage ;
Invalidate(FALSE) ;

}

else
{
m_previous_state = m_current_status ;
m_current_status = NORMAL ;
Invalidate(FALSE) ;
}



TRACKMOUSEEVENT TME_STRUCT ;
TME_STRUCT.cbSize = sizeof(TRACKMOUSEEVENT) ;
TME_STRUCT.dwFlags = TME_LEAVE ;
TME_STRUCT.hwndTrack =m_hWnd ;
_TrackMouseEvent(&TME_STRUCT) ;






CButton::OnMouseMove(nFlags, point);
}




To draw the button
void CoolButton::DrawItem(LPDRAWITEMSTRUCT l_DrawItem_Struct)
{

CDC *pDC_Own = CDC::FromHandle(l_DrawItem_Struct->hDC) ;
CBitmap *pOldBitmap ;
CDC memi_DC
; memi_DC.CreateCompatibleDC (pDC_Own) ;


switch(m_current_status)
{
case NORMAL :
pOldBitmap = memi_DC.SelectObject( &m_bm_Normal ) ;
break ;
case Hoverimage :


pOldBitmap = memi_DC.SelectObject(& m_bmp_over ) ;
break ;


case CLICKED :

pOldBitmap = memi_DC.SelectObject( &m_bmp_pushed ) ;
break ;


}

pDC_Own->BitBlt(0,0, l_DrawItem_Struct->rcItem.right -l_DrawItem_Struct->rcItem.left ,l_DrawItem_Struct->rcItem.bottom- l_DrawItem_Struct->rcItem.top ,&memi_DC ,0,0,SRCCOPY);

memi_DC.SelectObject(pOldBitmap) ;

memi_DC.DeleteDC() ;



}
7- Add the following functioon to Coolbutton class name it On_Recto of type BOOL edit as follows
BOOL CoolButton::On_Recto(CRect &rct, CPoint cpt)
{

return(cpt.x> 0 && cpt.x< (rct.right -rct.left) && cpt.y>0 && cpt.y< (rct.bottom -rct.top) )?TRUE :FALSE ;

to check whether the mouse inside the botton rect
}

8- Add the following virtual function to the new class of type void
void CoolButton::OnMouseLeave()
{

m_previous_state = m_current_status ;
m_current_status = NORMAL ;

Invalidate() ;


}
and add ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave) inside begin messege map

Now we are finished from CoolButton Class

9- In the dialog resource editor set Ok button properties as owner_draw
10 Open the class wizard and add a member variable chose IDOK in the dialog class and let us name it m_double_tooltip_button or something else and do not forget to put
#include "CoolButton.h" in your dialog class header file

11- Replace the old constructor in the dialgo class with the following one
CTasheDlg::CTasheDlg(): CDialog(IDD_TASHE_DIALOG,NULL), m_double_tooltips_button(IDB_NORMAL ,IDB_OVER ,IDB_DOWE)
{
//loading the bitmaps
}

12- Add the CToolTipCtrl variable in the dialog class and name it like m_mytooltip or else

add the following code to OnItDialog
SetWindowText("helo");
UpdateData(FALSE);
m_double_tooltips_button.MoveWindow( 60,60,240,140) ;
// buton new position
CRect button_rectangle ;
m_double_tooltips_button.GetClientRect(button_rectangle) ;



m_mytooltip.Create( this);

CRect leftRect = button_rectangle ;
CRect Right_button = button_rectangle ;

leftRect.right = Right_button.left = button_rectangle.right/2 ;
m_mytooltip.AddTool(&m_double_tooltips_button, " Right half ", &Right_button ,1) ;
m_mytooltip.AddTool(&m_double_tooltips_button, " you are on the left part of the custom control botton",&leftRect ,2) ;
// or you can replace last addtool with updateTooltip function

13 - In your dialog class add the PreTranslateMessage messege handler as follows

BOOL CTasheDlg::PreTranslateMessage(MSG* pMsg)
{

if(m_mytooltip.m_hWnd) m_mytooltip.RelayEvent(pMsg) ;


return CDialog::PreTranslateMessage(pMsg);
}


14- Build application compile it and move the mouse cursor inside the botton and see the effect of that


I hope that you liked this tutorial ( tache) for more tutorials see the blog for more

My next project would be A button with 2 functions

Thanks for reading the tache program ....