第 6 章 mfc 编程

92
1 第 6 第 MFC 第第 6.1Windows 第第第第第第

Upload: rowa

Post on 27-Jan-2016

103 views

Category:

Documents


0 download

DESCRIPTION

第 6 章 MFC 编程. 6.1Windows 编程基础知识. 输入姓名. 输入第一次成绩. 启动. 启动. 输入第二次成绩. 输入第三次成绩. 输入姓名. 计算平均成绩. 消息处理. 输入第一次测试成绩. 输入第二次测试成绩. 输入第三次测试成绩. 结束. 计算平均成绩. 结束. 6.1.1 事件驱动原理. 事件驱动 方法计算平均成绩. 过程驱动 方法计算平均成绩. 图 6-2 应用程序、操作系统、输入输出设备交互图. 箭头 1 表示操作系统能操纵输出设备以执行特定的功能,例如让声卡发出声音,让显卡发出图形。 - PowerPoint PPT Presentation

TRANSCRIPT

  • 6MFC6.1Windows

  • 6.1.1

  • 126-2

  • 3 4 WindowsWindowsWindows

  • 6.l.21.WindowsWindows

  • windowswindows2.

  • HWNDHDCHBITMAPHCURSORHICONHFONTHMENUHPENHFILEHBRUSHHINSTANCEmfc.bsc

  • 3. MFC VC++6.0dswLesson09.dsw

  • 6.1.3 typedef

  • 6.2MFC MFC(Microsoft Foundation ClassWindowsAPIC++C++ Class Library)MFC6.0200Windows APIWindowWindowsMFCWindowsAPIMFCMFCWindowsMFCAPI MFCWindowsMFCVisual C++

  • MFCMFCWindows MFCWindowsMFCMFC MFCMFC

  • 6.3MFC

    MFCCObjectCObjectMFC CObject

    6.3.1

  • 1.CWinApp CWinApp CWinApp CWinAppMFCCWinAPP

  • 2.CDocument

  • 6.3.2 windowsMFCCWnd

  • 6.3.3 MFCMFC(CDC)WindowsCObjectCDCMFCCDC

  • MFCApplication Framework

  • 6.4AppwizardMFC6.4.1MFC Visual C++MFC: Windows CWnd

  • 6.4.2MFCAppwizrd6-1MFCAppwizardex6_1

  • 6.4.3ex6_1ClassView CmainFrameCEx6_1AppCEx6_1Doc CEx6_1Viewex6_14

    1.ex6_1CEx6_1AppCWinApp theAppCEx6_1AppWindows CEx6_1APPex6_1App.hex6_1App.cpp

  • 2.ex6_1CMainFrameCWnd CFramewnd CMainFrame MainFrame.h MainFrame.cppCMainFrame CMainFrame 3.ex6_1CEx6_1Doc,CDocument ex6_1Doc.h ex6_1Doc.cpp

  • 4.ex6-1CEx6_1Vicw CView CView CWnd CEx6_1View ex6_1View.h ex6_1View.cppCViewCView MFC6Baseclass CEditView 5.Appwizard StdAfx.cpp StdAfx.h MFC MFC afxwin.hafxext.hafxdtctl.hafxcmn.hafxdisp.h stdafx.h

  • 6.4.4Visual C++ 6.0ClassView()ResourceView FileView 1.ClassViewClassView2.ResourceView3.FileViewFileView

  • 6.56.5.1MSGtypedef struct tagMSG{HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; }MSG;hwndnullmessageWindows.hwParamlParamwParamlParamtimeptPOINT

  • 6.5.21.WindowsmessageWM_WM_COMMANDWM_CREATEWM_CREATE Windows WM_DESTROYWM_PAINTWM_KEYDOWN WM_KEYUP WM_CHAR WM_MOUSEMOVE WM_LBUTTONDOWN WM_LBLITTONUP WM_LBUTTONDBLCLK WM_RBUTTONDOWN

  • MFC CWnd Windows afx_msgOn Windows WM_WM_LBUTTONDOWNafx_msg void OnLButtonDown(UINT nFlags, CPoint point);CWndCFrameWndCMDIFrameWndCMDIhildWndCViewCDialogAfxMFCMFC"Afx"Afx*.hMFC afxwin.h MFCafxext.h MFCafxdisp.h MFCafxdb.h MFCODBC

  • 2.Control notificationsCB_ EM_ LB_ SBM_ BM_ ClassWizard

  • 3.Command message)WM_COMMANDWM_COMMAND IDIDID ID ID_FILE_OPEN afx_msg void OnFileOpen(); //MainFrm.hvoid CMainFrame::OnFileOpen( ) //MainFrm.cpp{// TODO: Add your command handler code here}

  • 6.5.3MFCWindows 1.MFC: MFC ClassWizardMFCVisual C++6.0OLE Automation()ActiveXCtrl+WMFC6-17MFC ClassWizard

  • MFC ClassWizard5Message MapsMember VariablesCDialogCPropertyPageCRecordViewCDaoRecordViewAutomationActiveXActiveX Events(ActiveX)ActiveXClass Info

  • 2.VC6.0(1)VC6.0ViewClasswizardMFC ClasswizardMessag Maps(2)ProjectClass Name(3)Object IDSIDID(4)MessagesAdd FunctionAdd Member FunctionOKMember functions (5)OKEdit Code

  • 6-2ex6_26-18

  • (1)ex6_2Ctrl+WMFC ClassWizard(2)Class nameCEx6_2ViewObject IDsMessages(3)Object IDsCEx6_2ViewMessagesWM_LBUTTOMDOWN(4)MessagesWM_LBUTTOMDOWNAdd FunctionsCEx6_2ViewOnLButtonDownMember functions(5)Edit CodeMFC ClassWizardOnLButtonDown(6)void CEx6_2View::OnLButtonDown(UINT nFlags,CPoint point){//TDDO:Add your message handler code here and/or call defaultMessageBox()CView::OnLButtonDown(nFlags,point);

  • 3.ex6_2MFC ClasswizardWM_LBUTTOMDOWN(1)ex6_2View.hOnLButtonDown: //{{AFX_MSG(CEx6_2View) afx_msg void OnLButtonDown(UINT nFlags,CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP( )//AFX_MSG(CEx_SDIView)//AFX_MSGMFC ClassWizard ClassWizard //{{//}} ClassWizard

  • (2)ex6_2View.cpp BEGIN_MESSAE_MAP(CEx6_2View,CView)//{{AFX_MSG_MAP(CEx6_2View)ON_WM_LBUTTONDOWN();//}}AFX_MSG_MAPEND_MESSAGE_MAP()(3)ex6_2View.cppvoid CEx6_2View::OnLButtonDown(UINT nFlags,CPoint point){//TODO:Add your message handler code here and/or call defaultMessageBox()CView::OnLButtonDown(nFlags,point);}

  • (1)DECLARE_MESSAGE_MAP BEGIN_MESSAGE_MAP END_MESSAGE_MAP DECLARE_MESSAGE_MAPh BEGIN _MESSAGE_MAP END_MESSAGE_MAP .cpp BEGIN_MESSAGE_MAP END_MESSAGE_MAP (2) afx_msg void OnLButtonDown(UINT nFlags,CPoint point);OnLButtonDown()afx_msg void ();

  • (3)cppON_WM_LBUTTONDOWN();.cpp BEGIN_MESSAGE_MAP END_MESSAGE_MAP ON_COMMAND ( ID_FILE_OPEN ,CWinApp ::OnFileOpen );ID_FILE_OPENIDON_ COMMANDCWinApp ::OnFileOpenFile |Open WM_COMMAND OpenIDID_FILE_OPEN CWinApp : : OnFileOpen()

  • WindowsON_WM_Windows Windows 6-10ON_WM_LBUTTONDOWN();CEx6_2ViewOnLButtonDown()

  • ON_ON_NOTIFY_ON_CONTROL_ON_Windows ON_NOTIFY_ON_CONTROL_ON_COMMAND ON_UPDATE_COMMAND ON_COMMANDID,memberfxn;ON_COMMAND_RANGEID,memberfxn;Memberfxn

  • 4.ClassWizard MFC ClassWizard (1)DECLARE_MESSAGE_MAP (2)BEGIN_MESSAGE_MAP END_MESSAGE_MAP(3)

    6-2void CEx6_2View::OnRButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultMessageBox("");CView::OnRButtonDown(nFlags, point);}

  • 6.6Visusl C++1.MFC (1)Ctrl+WMFCAdd ClassNew6-19New Class(2)NameCMFCFile nameChangeBase classDialog IDCDialogIDAutomation(3)OK

  • ...bug ... ... ..

  • 2.(1)Visual C++(2)FileViewSource FilesHeader Files.h.cppDelete(3)(4).h.cpp.clw

  • 3CEx6_2Viewvoid DoDemo(int nDemol),(1)ex6_2(2)ClassViewCEx6_2View (3)Add Member FunctionvoidDoDemo(int nDemol)Access (4)

  • 4.CEx6_2Viewint *m_nDemo(1)ClassView(2)CEx6_2ViewAdd Member Variable,int*m_nDemo*6-23(3).h

  • 1 Build|Start Debug|GoGOBuild|Start Debug|Step IntoBuild|Start Debug|Step OverBuild|Start Debug|Run to CursorBuildDebug

  • 2

  • WatchVariablesQuitchWatch1View|Debug Windows|Watch2View|Debug Windows|Variables3Debug|QuitchWatch

  • 3 WatchWatch1Watch2Watch3nameDelete

  • VariablesAutoLocalsthisthis Debug|QuitchWatch

  • ExpressionRecalculateAdd WatchWatchClose VC:View|Debug Windows|Registers (CPU) View|Debug Windows|Memory ()View|Debug Windows|Call Stack ()View|Debug Windows|Disassembly (VC)

  • VCVCVCDockingVC(Docking)WorkSpaceOutputWatchVariables

  • View|WorkspaceView|OutputView|Debug WindowsWindow|Next WindowWindow|PreviousWindow|Cascade

  • VCCtrl+]{(Ctrl+[})Ctrl+Shift+]{(})Ctrl+Shift +[()

  • C++Tools|OptionsOptionsFormat

    VCSmart Indent Tools|OptionsOptionsTabsAuto indentSmart

  • VCAlt+F7 Project SettingsF7BuildCtrl+F7 CompileCtrl+F5 RunCtrl+BreakStops the buildF5GoCtrl+F10Run to cursorF11step intoAlt+F10 Apply codes changesCtrl+F9 Enable/Disable a breakpointAlt+F11 Memory Window Alt+Shift+F11 Memory Window Ctrl+Shift+F9 Ctrl+Shift+F5 Restarts the programCtrl+Shift+F10 Alt+Num*Shift+F11F9F10step overShift+F5 Debugging

  • Win32int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ // TODO: Place code here.MSG msg;HACCEL hAccelTable;// Initialize global stringsLoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_WINAPPDEMO, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);// Perform application initialization:if (!InitInstance (hInstance, nCmdShow)) {return FALSE;}hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WINAPPDEMO);// Main message loop:while (GetMessage(&msg, NULL, 0, 0)) {if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {TranslateMessage(&msg);DispatchMessage(&msg);}}return msg.wParam;}

  • win32P1106-11,6-12

  • 2GUI

  • library(Static Link Library)(Dynamic Link Library)

  • DLL(Dynamic Linkable Library)import library.LibDLLDLL

  • EXEDLL DLL1)2)3)WindowsGetSystemDirectory 4)WindowsGetWindowsDirectory DLL

  • DLLHMODULE LoadLirary(LPCTSTR lpszLibFile)BOOL FreeLibrary(HMODULE hLibModule)FARPROC GetProcAddressHMODULE hModule,LPCTSTR lpszProc

  • DLL GCClib*.a-l*math.h-lmlibm.aVisual C++*.lib#pragma comment(lib,"*")

  • .libproject-setting-link-object/library modules .libtools/optionslibproject-add to project-files.lib

  • DEMO #include "../StaticLib/RandInt.h"#include #include//setwusing namespace std;#pragma comment(lib,"../StaticLib/Debug/StaticLib.lib")#include "../DynLinkLib/DynLinkLib.h"#include using namespace std;#pragma comment(lib,"../DynLinkLib/Debug/DynLinkLib.lib")int main(void){CDynLinkLib cdll;cout
  • / MS DOSXubuntuDOS OSWindowsDOSWindowsROM BIOSWin32 APIsDOSBIOS16(10h)WindowsDOS33h

  • 1225X80

  • GUIGUI/ FLTK/MFC(GDI) GUI

  • VC++DLLC++COMcategory

  • DLL

  • DLL1Win32APILoadLibrary()GetProcAddress()FreeLibrary()pCreatClass pcc;HMODULE hDll= ::LoadLibrary("../ExpClass/Debug/ExpClass.dll");//step1pcc = (pCreatClass)GetProcAddress(hDll,"CreatAddClass");//step2Expclass* ac2 = (pcc)(2,3); cout
  • DLL2vftable#ifndef Expclass_H#define Expclass_H

    //#ifdef Expclass_H_EXPORTS//#define Expclass_H_API __declspec(dllexport)//in dll//#else//#define Expclass_H_API __declspec(dllimport)//out dll//#endif

    class /*Expclass_H_API*/ Expclass{int x, y;public:Expclass(int xx, int yy);virtual int addcfun();};#endif

  • DLL3templateDest force_cast(Src src){union{Dest d;Src s;} convertor;

    convertor.s = src;return convertor.d;}#endif

  • DLL4#ifndef _CLASSLDR_H_#define _CLASSLDR_H_

    template struct ClassLoader{static HMODULE s_hMod;};

    template HMODULE ClassLoader::s_hMod ;

    #endif //_EXPCLASS_H_

    #ifdef STRICT typedef void *HANDLE; #define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name #else typedef PVOID HANDLE; #define DECLARE_HANDLE(name) typedef HANDLE name #endif

    DECLARE_HANDLE(HMODULE); DECLARE_HANDLE(HINSTANCE); DECLARE_HANDLE(HLOCAL); DECLARE_HANDLE(HGLOBAL); DECLARE_HANDLE(HDC); DECLARE_HANDLE(HRGN); DECLARE_HANDLE(HWND); DECLARE_HANDLE(HMENU); DECLARE_HANDLE(HACCEL); DECLARE_HANDLE(HTASK); mfcmfc vcvc++6.0 mfc.bscvcmfcvcmfc vc mfc vc++6.0projectFile/Open Browse Info File(.bsc),vc++6.0 \vc98\mfc\src\mfc.bsc Tool/Source Browser Identifier Definitions and ReferencesOK File OutlineOK Base Classes and MembersDerived Classes and Members OK Call GraphCaller GraphOK

    AFX1989ApplicationFrameworkAFXC++WindowsAFXXAFX X,activexutilize the latest in object oriented technology to provide tools and libraries for developers writing the most advanced GUI applications onthemarketWindowsAFX--Windows --deliver the power of object-oriented solutions to programmers to enable them to build world-class Windows based applications in C++. Windows 3.0afxMFCMFClinuxwindowsafxMFCMFCafxAPIAFX MFC C++ C++WindowsMFC WinMainRegisterClassWindow Procedure AfxAPI,VCAfxMessageBox,,MessageBoxAFXAfxMFCMFC"Afx"Afx*.hMFC afxwin.h MFCafxext.h MFCafxdisp.h MFCafxdb.h MFCODBC F1 Ctrl+O Open Ctrl+P Print Ctrl+N New Ctrl+Shift+F2 F2 Shift+F2 Alt+F2 CtrlF2/ F12Goto definition Shift+F12Goto reference Ctrl+'Num+'Displays the next symbol definition or reference Ctrl+'Num-'Displays the previous symbol definition or reference Ctrl+J/K/ Ctrl+Shift+J/K / Ctrl+End Ctrl+Shift+End Ctrl+Home Ctrl+Shift+Home Ctrl+B/Alt+F9 Alt+F3/Ctrl+F F3 Shift+F3 Ctrl+]/Ctrl+E Ctrl+Shift+] Ctrl+Shift+E F4/ Shift+F4/ Shift+Home Shift+End Ctrl+L Ctrl+Shift+L Alt+Shift+T Ctrl+Alt+TBrings up the completion list box Shift+PageDown Shift+PageUp Ctrl+Shift+SpaceTooltip Ctrl+Z/Alt+Backspace Undo Ctrl+Shift+Z/Ctrl+Y Redo F8/ Ctrl+Shift+F8 / Alt+F8 Ctrl+G Goto Ctlr+X/Shift+DelCut Ctrl+C/Ctrl+Ins Copy Ctrl+V/Shift+InsPaste Ctrl+U Ctrl+Shift+U Ctrl+F8 / Ctrl+Shift+L Ctrl+Shift+8 Tab` Ctrl+T Ctrl+ Ctrl+ Ctrl+Del Ctrl+Backspace Ctrl+ Ctrl+ Ctrl+Shift+ Ctrl+Shift+ Ctrl+Shift+T Alt+0 Workspace Window Alt+2 Output Window Alt+3 Watch Window Alt+4 Variables Window Alt+5 Registers Window Alt+6 Memory Window Alt+7 CallStack Window Alt+8 Disassembly Window Ctrl+W ClassWizard Alt+EnterAlt+F7 Project SettingsF7Build Ctrl+F7 Compile Ctrl+F5 Run Ctrl+BreakStops the build F5Go Ctrl+F10Run to cursor F11step into Alt+F10 Apply codes changes Ctrl+F9 Enable/Disable a breakpoint Alt+F11 Memory Window Alt+Shift+F11 Memory Window Ctrl+Shift+F9 Ctrl+Shift+F5 Restarts the program Ctrl+Shift+F10 Alt+Num* Shift+F11 F9 F10step over Shift+F5 Debugging Ctrl+F11Switches between the source view and the disassembly view for this instruction Alt+F12 Queries on the selected object or current contextAlt+F6 Toggles the docking feature for the window on/off Shift+EscCtrl + F3 Ctrl + Shift + F3 Ctrl+Shift+G Ctrl+* Ctrl+Space Ctrl+F3 Ctrl+Shift+F3 Ctrl+D Ctrl+I Ctrl+Shift+I F6Activates the next pane Shift+F6Activates the previous pane Ctrl+M Detects duplicate mnemonics in the resourceCtrlTAB Ctrlu: CtrlSHIFT+u: Ctrl} Ctrl + 1 New Dialog Ctrl + 2 New Menu Ctrl + 3 New Cursor Ctrl + 4 New Icon Ctrl + 5 New Bitmap Ctrl + 6 New Toolbar Ctrl + 7 New Accelerator Ctrl + 8 Open String Table Ctrl + 9 Open Version InfoCtrl + F3 Ctrl + Shift + F3 Ctrl + D ComboBox Ctrl + I Ctrl + Shift + I Ctrl + Space . :Tools->CustomizeKeyBoard F12 Goto definition Shift+F12 Goto reference Ctrl+'Num+' Displays the next symbol definition or reference Ctrl+'Num-' Displays the previous symbol definition or reference Ctrl+J/K / Ctrl+Shift+J/K / Ctrl+End Ctrl+Shift+End Ctrl+Home Ctrl+Shift+Home Ctrl+B/Alt+F9 Alt+F3/Ctrl+F F3 Shift+F3 Ctrl+]/Ctrl+E Ctrl+Shift+] Ctrl+Shift+E F4 / Shift+F4 / Shift+Home Shift+End Ctrl+L Ctrl+Shift+L Alt+Shift+T Ctrl+Alt+T Brings up the completion list box Shift+PageDown Shift+PageUp Ctrl+Shift+SpaceTooltip Ctrl+Z/Alt+Backspace Undo Ctrl+Shift+Z/Ctrl+Y Redo F8 / Ctrl+Shift+F8 / Alt+F8 Ctrl+G Goto Ctlr+X/Shift+DelCut Ctrl+C/Ctrl+Ins Copy Ctrl+V/Shift+InsPaste Ctrl+U Ctrl+Shift+U Ctrl+F8 / Ctrl+Shift+L Ctrl+Shift+8 Tab` Ctrl+T Ctrl+ Ctrl+ Ctrl+Del Ctrl+Backspace Ctrl+ Ctrl+ Ctrl+Shift+ Ctrl+Shift+ Ctrl+Shift+T Alt+Num* Shift+F11 F9 F10 step over Shift+F5 Debugging Ctrl+F11 Switches between the source view and the disassembly view for this instruction Alt+F12 Queries on the selected object or current contextAlt+F6 Toggles the docking feature for the window on/off Shift+Esc Ctrl+Shift+G Ctrl+* string table Ctrl+Space Ctrl+F3 Ctrl+Shift+F3 Ctrl+D Ctrl+I Ctrl+Shift+I F6 Activates the next pane Shift+F6 Activates the previous pane Ctrl+M Detects duplicate mnemonics in the resource Alt +O : cppDPIDots Per Inchdpi6090 dpi1200 dpi9600 dpi6001200 dpidpcmdots per centimeterdpcmdpiCSS31(inch)=2.54(centimetre) 1(foot)=30.48