---多种资源在Windows编程中的应用
姓名:
专业:
学号:
2012.4.10
Windows编程实验三
----多种资源在Windows编程中的应用一.实验目的:
1.熟练掌握在Visual C++ 6.0 IDE中编辑、编译、调试和运行一个MFC应用程序的一般步骤和过程;
2.理解利用MFC开发Windows应用程序的基本思想和方法;
3.把握包含多种资源的Windows应用程序的整体构架,提高综合应用知识的能力和技巧,提高分析问题、解决问题和实际动手编制WinApp的能力。
二.实验要求:
1、实验中注意记录实验主要过程及排异处理情况;
2、关注MFC AppWizard和ClassWizard这两个工具的使用方法和主要过程。
3、明确实验正确结果的关键算法和主要使用的函数;
4、实验结束前完成任务实验的编码、调试工作,并提交相应程序代码。
三、实验内容:
题目一:编写一个类似“体彩选号器”的实用小程序;
1.打开Visual C++ 6.0,新建一个MFC文档,选择基本对话框,按照要求画出彩票机的界面并添加代码。
2.在代码编辑窗口内的代码:
#include "stdafx.h"
#include "344.h"
#include "344Dlg.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_DA TA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DA TA
// 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_DA TA_INIT(CAboutDlg)
//}}AFX_DA TA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DA TA_MAP(CAboutDlg)
//}}AFX_DA TA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy344Dlg dialog
CMy344Dlg::CMy344Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy344Dlg::IDD, pParent)
{
//{{AFX_DA TA_INIT(CMy344Dlg)
// NOTE: the ClassWizard will add member initialization here //}}AFX_DA TA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy344Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DA TA_MAP(CMy344Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DA TA_MAP
}
BEGIN_MESSAGE_MAP(CMy344Dlg, CDialog)
//{{AFX_MSG_MAP(CMy344Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy344Dlg message handlers
BOOL CMy344Dlg::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_SEPARA TOR);
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
return TRUE; // return TRUE unless you set the focus to a control }
void CMy344Dlg::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 CMy344Dlg::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 CMy344Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
3.选择单击工具栏上的按钮,进行调试,未出现错误则单击按钮运行程序,查看运行结果。
4.保存程序代码,进行分析。
题目二:独立完成一个类似于“简易文本编辑器”子系统的软件开发(架构形式和具体内容自我设计,原则:架构美观、布局合理,内容实用,具有可编辑性)。要求有明确的系统目标和清楚的设计思路。
1.打开Visual C++ 6.0选择SDI、CMyView:CEditView,即在向导第1步:选SDI;第6步:修改CMyView的基类为CEditView即可;
2.然后往文本框里添加所选要的功能。
3.为所添加的功能增添消息。
4.选择单击工具栏上的按钮,进行调试,未出现错误则单击按钮运行程序,查看运
行结果。
5.验证程序的功能。
四.实验总结:
1.资源使用中应注意工具栏按纽的设置分2步:使用图形工具编辑按纽位图;在其属性对话框中指定按钮的ID。
2.控件的创建分为静态创建(指在对话框模板创建控件,并指定控件属性。);动态创建(指在程序运行中椐需要定义一个控件对象,再通过CreateWindow()/ CreateWindowEx()创建控件,ShowWindow()显示控件。如同操作一个子窗口。
3.静态控件不发送消息;其它控件对于用户的操作都能发送消息。不同类的控件发送的消息通知代码是不相同的。
.....
五.实验心得:
...................