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

📄 knap1dlg.cpp

📁 非常好用的背包演示系统
💻 CPP
字号:
// knap1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "knap1.h"
#include "knap1Dlg.h"
#include "dialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CKnap1Dlg dialog

CKnap1Dlg::CKnap1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKnap1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKnap1Dlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CKnap1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKnap1Dlg)
	DDX_Control(pDX, IDCANCEL, m_btnST1);
	DDX_Control(pDX, IDOK, m_btnST);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CKnap1Dlg, CDialog)
	//{{AFX_MSG_MAP(CKnap1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DRAWITEM()
	ON_WM_CANCELMODE()
	ON_WM_CAPTURECHANGED()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKnap1Dlg message handlers

BOOL CKnap1Dlg::OnInitDialog()
{

	  CDialog::OnInitDialog();
	 m_brush.CreateSolidBrush(RGB(221,221,221));//画刷颜色
	 //按钮背景色
   	m_btnST.SetActiveFgColor(RGB(250,0,250));
	m_btnST.SetInactiveBgColor(RGB(221,221,221));
	m_btnST.SetInactiveFgColor(RGB(0,0,250));
    m_btnST1.SetActiveFgColor(RGB(255,0,255));
	m_btnST1.SetInactiveBgColor(RGB(221,221,221));
	m_btnST1.SetInactiveFgColor(RGB(0,0,250));
  
	 ((CComboBox*)GetDlgItem(IDC_COMBO2))->AddString("动态规划");
	 ((CComboBox*)GetDlgItem(IDC_COMBO2))->AddString("贪心算法");
	 ((CComboBox*)GetDlgItem(IDC_COMBO2))->AddString("分支限界");
	 ((CComboBox*)GetDlgItem(IDC_COMBO2))->SetCurSel(0);  //GetCurSel();SetCurSel(int nSelect)
    
	m_bmpBackground.LoadBitmap(IDB_BITMAPBACKGROUND);
	// 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_Static1.SubclassDlgItem(IDC_STATIC1,this);	
	m_Static2.SubclassDlgItem(IDC_STATIC2,this);	
	m_Static3.SubclassDlgItem(IDC_STATIC3,this);
	m_Static1.SetTextColor(RGB(0,0,255));
	m_Static2.SetTextColor(RGB(0,0,255));
	m_Static3.SetTextColor(RGB(0,0,255));
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CKnap1Dlg::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 CKnap1Dlg::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();
		CPaintDC dc(this);				//对话框的dc
		CDC dcMem; 
		dcMem.CreateCompatibleDC(&dc);   //创建与对话框dc兼容的内存dc
    	CRect rect;
		GetClientRect(&rect);
    	BITMAP bitMap;
		m_bmpBackground.GetBitmap(&bitMap);
        CBitmap *pbmpOld=dcMem.SelectObject(&m_bmpBackground);		//将背景位图选入内存dc中
		dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth,bitMap.bmHeight,SRCCOPY);   //将内存dc中的位图拉伸显示在对话框的dc中
		dc.BitBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY);
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CKnap1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
	
}
extern int m_KnapNum;
extern int m_Num;
extern CString m_strProgram;
void CKnap1Dlg::OnOK() 
{ 
    int num1,num2;
	char  ch1[10],ch2[10];
    int index;
	dialog  dlg;
    index=((CComboBox*)GetDlgItem(IDC_COMBO2))->GetCurSel();
  ((CComboBox*)GetDlgItem(IDC_COMBO2))->GetLBText(index,m_strProgram);
 	GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
	GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
	num1=atoi(ch1);
	num2=atoi(ch2);	
 	m_KnapNum=num1;
    m_Num=num2;
 	if((num1>1000)||(num1<10))
		{
		AfxMessageBox("背包容量应在10到1000之间选择");
		UpdateData(TRUE);
		GetDlgItem(IDC_EDIT1)->SetWindowText("");
	 	}
  else if ((num2>1000)||(num2<=0))
	{
		AfxMessageBox("物品数应在0到1000之间选择");
		UpdateData(TRUE);
	}     
  //显示模态对话框
   else  dlg.DoModal();
   CDialog::OnOK();
   }

 
 

void CKnap1Dlg::OnCancel() 
{
	// TODO: Add extra cleanup here

	CDialog::OnCancel();
}

 

 

 

HBRUSH CKnap1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	if(pWnd->GetDlgCtrlID()==IDC_COMBO2)
	{
		pDC->SetTextColor(RGB(0,130,255));
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetBkColor(RGB(221,221,221));
		return m_brush;
	}
	
	if(pWnd->GetDlgCtrlID()==IDC_EDIT1)
	{
		pDC->SetTextColor(RGB(0,0,255));
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetBkColor(RGB(221,221,221));
		return m_brush;
	}
	if(pWnd->GetDlgCtrlID()==IDC_EDIT2)
	{
		pDC->SetTextColor(RGB(0,0,255));
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetBkColor(RGB(255,0,255));
		return m_brush;
	}
 
	// TODO: Change any attributes of the DC here
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

⌨️ 快捷键说明

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