Visual C++——《可视化编程技术》实验报告——MFC编程
生活随笔
收集整理的這篇文章主要介紹了
Visual C++——《可视化编程技术》实验报告——MFC编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、實驗目的和要求
使用MFC類庫編制應用程序:按鈕控件、靜態控件及編輯框的應用。
二、實驗內容
三、程序代碼
// 2018329621200_申屠志剛_可視化編程技術B_實驗四Dlg.cpp : implementation file //#include "stdafx.h" #include "2018329621200_申屠志剛_可視化編程技術B_實驗四.h" #include "2018329621200_申屠志剛_可視化編程技術B_實驗四Dlg.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif/ // CAboutDlg dialog used for App Aboutclass 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_MSGDECLARE_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()/ // CMy2018329621200__B_Dlg dialogCMy2018329621200__B_Dlg::CMy2018329621200__B_Dlg(CWnd* pParent /*=NULL*/): CDialog(CMy2018329621200__B_Dlg::IDD, pParent) {//{{AFX_DATA_INIT(CMy2018329621200__B_Dlg)//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); }void CMy2018329621200__B_Dlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CMy2018329621200__B_Dlg)DDX_Control(pDX, IDC_EDIT2, m_Edit2);DDX_Control(pDX, IDC_EDIT1, m_Edit1);DDX_Control(pDX, IDC_UNDO_BUTTON, m_undo);DDX_Control(pDX, IDC_SHOW2_BUTTON, m_show2);DDX_Control(pDX, IDC_SHOW1_BUTTON, m_show1);DDX_Control(pDX, IDC_EXIT_BUTTON, m_exit);DDX_Control(pDX, IDC_COPY_BUTTON, m_teansfer);DDX_Control(pDX, IDC_CLEAR2_BUTTON, m_clear2);DDX_Control(pDX, IDC_CLEAR1_BUTTON, m_clear1);DDX_Control(pDX, IDC_STATIC_BMP, m_bmp);//}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CMy2018329621200__B_Dlg, CDialog)//{{AFX_MSG_MAP(CMy2018329621200__B_Dlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_STATIC_BMP, OnStaticBmp)ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)ON_BN_CLICKED(IDC_SHOW1_BUTTON, OnShow1Button)ON_BN_CLICKED(IDC_SHOW2_BUTTON, OnShow2Button)ON_BN_CLICKED(IDC_UNDO_BUTTON, OnUndoButton)ON_BN_CLICKED(IDC_CLEAR1_BUTTON, OnClear1Button)ON_BN_CLICKED(IDC_CLEAR2_BUTTON, OnClear2Button)ON_BN_CLICKED(IDC_COPY_BUTTON, OnCopyButton)ON_COMMAND(ID_Clear1, OnClear1Button)ON_COMMAND(ID_Clear2, OnClear2Button)ON_COMMAND(ID_Copy, OnCopyButton)ON_COMMAND(ID_Show1, OnShow1Button)ON_COMMAND(ID_Show2, OnShow2Button)ON_COMMAND(ID_Undo, OnUndoButton)//}}AFX_MSG_MAP END_MESSAGE_MAP()/ // CMy2018329621200__B_Dlg message handlersBOOL CMy2018329621200__B_Dlg::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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small iconm_Menu.LoadMenu(IDR_MENU); // IDR_MENU1SetMenu(&m_Menu);// TODO: Add extra initialization herem_bmp.ModifyStyle(0,SS_BITMAP);HBITMAP hBmp=LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1));m_bmp.SetBitmap(hBmp);return TRUE; // return TRUE unless you set the focus to a control }void CMy2018329621200__B_Dlg::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 CMy2018329621200__B_Dlg::OnPaint() {if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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 icondc.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 CMy2018329621200__B_Dlg::OnQueryDragIcon() {return (HCURSOR) m_hIcon; }void CMy2018329621200__B_Dlg::OnStaticBmp() {// TODO: Add your control notification handler code hereBITMAP bmp;GetObject(m_bmp.GetBitmap(),sizeof(BITMAP),&bmp);CString msg;msg.Format("Image Size %d*%d",bmp.bmWidth,bmp.bmHeight);AfxMessageBox(msg); }void CMy2018329621200__B_Dlg::OnExitButton() {// TODO: Add your control notification handler code hereOnOK(); }void CMy2018329621200__B_Dlg::OnShow1Button() {// TODO: Add your control notification handler code herem_Edit1.SetSel(0,-1); // 選中編輯框IDC_EDIT1中的全部內容m_Edit1.ReplaceSel("This is the first EditBox."); }void CMy2018329621200__B_Dlg::OnShow2Button() {// TODO: Add your control notification handler code herem_Edit2.SetSel(0,-1); m_Edit2.ReplaceSel("This is the second EditBox."); }void CMy2018329621200__B_Dlg::OnUndoButton() {// TODO: Add your control notification handler code herem_Edit1.Undo(); m_Edit2.Undo(); }void CMy2018329621200__B_Dlg::OnClear1Button() {// TODO: Add your control notification handler code herem_Edit1.SetSel(0,-1); //選中IDC_EDIT1中的內容m_Edit1.ReplaceSel(""); //用空串代替所選文本,即把所選的文本刪除 }void CMy2018329621200__B_Dlg::OnClear2Button() {// TODO: Add your control notification handler code herem_Edit2.SetSel(0,-1);m_Edit2.ReplaceSel(""); }void CMy2018329621200__B_Dlg::OnCopyButton() {// TODO: Add your control notification handler code herem_Edit1.SetSel(0,-1);m_Edit1.Copy(); m_Edit2.SetSel(0,-1);m_Edit2.ReplaceSel("");m_Edit2.Paste(); }void CMy2018329621200__B_Dlg::OnClear1() {// TODO: Add your command handler code here}void CMy2018329621200__B_Dlg::OnClear2() {// TODO: Add your command handler code here}void CMy2018329621200__B_Dlg::OnCopy() {// TODO: Add your command handler code here}void CMy2018329621200__B_Dlg::OnShow1() {// TODO: Add your command handler code here}void CMy2018329621200__B_Dlg::OnShow2() {// TODO: Add your command handler code here}void CMy2018329621200__B_Dlg::OnUndo() {// TODO: Add your command handler code here}四、運行結果及分析
?
五、實驗心得
1、使用MFC類庫編制應用程序:按鈕控件、靜態控件及編輯框的應用。
?
總結
以上是生活随笔為你收集整理的Visual C++——《可视化编程技术》实验报告——MFC编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue + Element UI——搜索
- 下一篇: s3c2440移植MQTT