⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 littleadderdlg.cpp

📁 计算器以及在对话框添加菜单栏和状态栏
💻 CPP
字号:
// LittleAdderDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LittleAdder.h"
#include "LittleAdderDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
static UINT BASED_CODE indicators[]=
{
	ID_INDICATOR_NISH,
		ID_INDICATOR_TIME
};

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

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_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_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLittleAdderDlg dialog

CLittleAdderDlg::CLittleAdderDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLittleAdderDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLittleAdderDlg)
	m_bAtOnce = FALSE;
	m_fAdd1 = 0.0;
	m_fAdd2 = 0.0;
	m_fResult = 0.0;
	m_dOpr = 0;
	m_strOpr = _T("+");
	m_edit = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLittleAdderDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLittleAdderDlg)
	DDX_Control(pDX, IDC_SLIDER1, m_slide);
	DDX_Control(pDX, IDC_BITMAP, m_cBmp);
	DDX_Check(pDX, IDC_CHECK_ATONCE, m_bAtOnce);
	DDX_Text(pDX, IDC_EDIT_ADD1, m_fAdd1);
	DDX_Text(pDX, IDC_EDIT_ADD2, m_fAdd2);
	DDX_Text(pDX, IDC_EDIT_RESULT, m_fResult);
	DDX_Radio(pDX, IDC_RADIO_ADD, m_dOpr);
	DDX_Text(pDX, IDC_STATIC_OPR, m_strOpr);
	DDX_Text(pDX, IDC_EDIT, m_edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLittleAdderDlg, CDialog)
	//{{AFX_MSG_MAP(CLittleAdderDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_EDIT_ADD1, OnChangeEditAdd1)
	ON_EN_CHANGE(IDC_EDIT_ADD2, OnChangeEditAdd2)
	ON_BN_CLICKED(IDC_RADIO_ADD, OnRadioAdd)
	ON_BN_CLICKED(IDC_RADIO_DIV, OnRadioDiv)
	ON_BN_CLICKED(IDC_RADIO_MULTI, OnRadioMulti)
	ON_BN_CLICKED(IDC_RADIO_SUB, OnRadioSub)
	ON_BN_CLICKED(IDC_BUTTON_RESULT, OnButtonResult)
	ON_BN_CLICKED(IDC_CHECK_ATONCE, OnCheckAtonce)
	ON_COMMAND(IDC_VIEW_OPENCOLORDIALOG, OnViewOpencolordialog)
	ON_COMMAND(IDC_VIEW_OPENFONTDIALOG, OnViewOpenfontdialog)
	ON_BN_CLICKED(IDC_ONCHANGE, OnOnchange)
	ON_WM_TIMER()
	ON_WM_MOUSEMOVE()
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnReleasedcaptureSlider1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLittleAdderDlg message handlers

BOOL CLittleAdderDlg::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 dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_bar.Create(this);
	m_bar.SetIndicators(indicators,2);
	CRect rect;
	GetClientRect(&rect);

	//确定pane的大小
	m_bar.SetPaneInfo(0,ID_INDICATOR_NISH,SBPS_NORMAL,rect.Width()-100);
	m_bar.SetPaneInfo(1,ID_INDICATOR_TIME,SBPS_STRETCH,0);
	m_bar.GetStatusBarCtrl().SetBkColor(RGB(180,180,180));

	//画状态栏在屏幕上
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,ID_INDICATOR_TIME);

	m_bar.GetStatusBarCtrl().SetBkColor(RGB(180,180,180));
	SetTimer(100,1000,NULL);
	
	//设置窗口大小位置不可变
	::SetWindowPos(m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
	m_slide.SetRange(10,255);//设置滑动条的范围
	m_slide.SetPos(128);//设置初始滑动位置
	SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
	HINSTANCE hInst=LoadLibrary("User32.DLL");//显示加载DLL
	if(hInst)
	{
		typedef BOOL(WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
		MYFUNC fun=NULL;
        //取得SetLayeredWindowAttributes函数指针
		fun=(MYFUNC)GetProcAddress(hInst,"SetLayeredWindowAttributes");
		//调用SetLayeredWindowAttributes
		if(fun)fun(this->GetSafeHwnd(),0,128,2);
		FreeLibrary(hInst);

	}

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLittleAdderDlg::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 CLittleAdderDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int 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 icon
		dc.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 CLittleAdderDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLittleAdderDlg::OnChangeEditAdd1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	if(m_bAtOnce)
	{
		OnButtonResult();
	}
	
}

void CLittleAdderDlg::OnChangeEditAdd2() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_bAtOnce)
	{
		OnButtonResult();
	}
	
}

void CLittleAdderDlg::OnRadioAdd() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_strOpr="+";
	UpdateData(false);
}

void CLittleAdderDlg::OnRadioDiv() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_strOpr="/";
	UpdateData(false);
	
}

void CLittleAdderDlg::OnRadioMulti() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_strOpr="*";
	UpdateData(false);
}

void CLittleAdderDlg::OnRadioSub() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_strOpr="-";
	UpdateData(false);
}

void CLittleAdderDlg::OnButtonResult() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	switch(m_dOpr)
	{
	case 0:   //加法
		m_fResult=m_fAdd1 + m_fAdd2;
		break;
	case 1:   //减法
		m_fResult=m_fAdd1 - m_fAdd2;
		break;
	case 2:   //乘法
		m_fResult=m_fAdd1 * m_fAdd2;
		break;	
	case 3:
		m_fResult=m_fAdd1 / m_fAdd2;
		break;
	
	}
	UpdateData(false);
}

void CLittleAdderDlg::OnCheckAtonce() 
{
	// TODO: Add your control notification handler code here
	
}



void CLittleAdderDlg::OnViewOpencolordialog() 
{
	// TODO: Add your command handler code here
	
		COLORREF colorRef;
		BYTE cRValue,cGValue,cBValue;
		int nCustColorNo;
		CString strDisplay;
		int nFlags=CC_FULLOPEN|CC_SHOWHELP;
		CColorDialog aDlg(RGB(0,255,255),nFlags);

	if(aDlg.DoModal()==IDOK)
	{
		//用户按了对话框确定按钮时的处理
		//得到并显示所选择的颜色
		colorRef=aDlg.GetColor();
		cRValue=GetRValue(colorRef);
		cGValue=GetGValue(colorRef);
		cBValue=GetBValue(colorRef);
		strDisplay.Format("所选择的颜色是:(%d,%d,%d)",cRValue,cGValue,cBValue);
		AfxMessageBox(strDisplay);

		//得到并显示自定义的颜色
        COLORREF *pColor=aDlg.GetSavedCustomColors();
		
		strDisplay="用户自定义的颜色:";
        CString strTemp;
		nCustColorNo=0;
		for(int i=0;i<16;i++)
		{
			cRValue=GetRValue(pColor[i]);
			cGValue=GetGValue(pColor[i]);
			cBValue=GetBValue(pColor[i]);
			if(cRValue!=255||cGValue!=255||cBValue!=255)
			{
				strTemp.Format("(&d,&d,&d)",cRValue,cGValue,cBValue);
				if(nCustColorNo>0)
					strDisplay+=",";
				strDisplay+=strTemp;
				nCustColorNo++;

			}
		}
		AfxMessageBox(strDisplay);
	}
	

	     

else
{
	//在这儿增加用户按了“取消”按钮的处理
	
}


	
}

	


void CLittleAdderDlg::OnViewOpenfontdialog() 
{
	// TODO: Add your command handler code here

	CString strFaceName,strStyleName;
	int nSize,nWeight;
	BYTE cRColor,cGColor,cBColor;
	COLORREF colorRef;
	BOOL bStrikeOut,bUnderline,bIsBold,bIsItalic;
	CString strDisplay1,strTemp1;
	CFontDialog fDlg(NULL,CF_USESTYLE|CF_EFFECTS|CF_SCREENFONTS);
	if(fDlg.DoModal()==IDOK)
	{
		//在这儿增加用户按了对话框“确定”按钮时的处理
		strFaceName=fDlg.GetFaceName();
		strStyleName=fDlg.GetStyleName();
        nSize=fDlg.GetSize();
		colorRef=fDlg.GetColor();
		cRColor=GetRValue(colorRef);
		cGColor=GetGValue(colorRef);
		cBColor=GetBValue(colorRef);
		nWeight=fDlg.GetWeight();
		bStrikeOut=fDlg.IsStrikeOut();
		bIsBold=fDlg.IsBold();
		bUnderline=fDlg.IsUnderline();
		bIsItalic=fDlg.IsItalic();
		strDisplay1.Format("字体名:%s,字体样式:%s,",strFaceName,strStyleName);
		strTemp1.Format("大小:%d号",nSize/10);
		strDisplay1+=strTemp1;
		strTemp1.Format("颜色:(%d,%d,%d),",cRColor,cGColor,cBColor);
		strDisplay1+=strTemp1;
		strTemp1.Format("浓度:%d,",nWeight);
	    strDisplay1+=strTemp1;
		strDisplay1+=bStrikeOut?"删除线,":"无删除线,";
		strDisplay1+=bUnderline?"下划线,":"无下划线,";
		strDisplay1+=bIsBold?"粗体,":"非粗体,";
		strDisplay1+=bIsItalic?"斜体":"非斜体";
		AfxMessageBox(strDisplay1);

	     
	}
else
{
	//在这儿增加用户按了“取消”按钮的处理
	
}


	
}

void CLittleAdderDlg::OnOnchange() 
{
	// TODO: Add your control notification handler code here
	if(i%2!=0)
	{
		cBmp.DeleteObject();
	    cBmp.LoadBitmap(IDB_BITMAP1);
	    m_cBmp.SetBitmap(HBITMAP(cBmp));
	}
	else
	{
		cBmp.DeleteObject();
	    cBmp.LoadBitmap(IDB_BITMAP2);
	    m_cBmp.SetBitmap(HBITMAP(cBmp));

	}
	i++;
	
}



void CLittleAdderDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	

	if(nIDEvent==100)
	{
		CTime t1;
		t1=CTime::GetCurrentTime();
		m_bar.SetPaneText(1,t1.Format("%H:%M:%S"));

	}
	CDialog::OnTimer(nIDEvent);
}

void CLittleAdderDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CString s;
	s.Format("X=%d Y=%d",point.x,point.y);
	m_bar.SetPaneText(0,s);
	
	
	CDialog::OnMouseMove(nFlags, point);
 
}

void CLittleAdderDlg::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	BYTE eff=(BYTE)m_slide.GetPos();//得到滑动条的位置
	HINSTANCE hInst=LoadLibrary("User32.DLL");
	if(hInst)
	{
		typedef BOOL(WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
		MYFUNC fun=NULL;

		fun=(MYFUNC)GetProcAddress(hInst,"SetLayeredWindowAttributes");
		//调用SetLayeredWindowAttributes
		if(fun)fun(this->GetSafeHwnd(),0,eff,2);
		FreeLibrary(hInst);
	}
	CString str;
	str.Format("%d",100*eff/255);
	GetDlgItem(IDC_EDIT)->SetWindowText(str);//显示透明值
	*pResult = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -