afx.h和window.h

时间: 2023-05-08 09:02:04 浏览: 105
afx.h和window.h是Windows C/C++编程中常用的头文件,其中afx.h是由Microsoft Visual Studio提供的,而window.h则是由Windows API提供的。两个头文件的主要作用都是定义了许多编写Windows程序所需的常量、宏、结构体、函数、变量等。 afx.h是一组MFC类库的头文件,它包括MFC应用程序开发所需的所有头文件,比如CWnd、CFrameWnd、CView、CString等类的定义。这些类可使C/C++开发人员更便利地编写Windows GUI程序。在使用afx.h时,应注意: 1.afx.h应该放在所有其他头文件的前面,此举可以避免出现由于预编译头文件引起的编译错误; 2.使用afx.h时必须将预编译头文件置于全部#include指令之前,否则会出现编译错误。 window.h包含了许多Windows操作系统提供的API函数和宏的定义,开发人员可以通过这些API函数和宏来实现Windows GUI程序的开发,也可基于这些API函数和宏开发更高级的应用程序。 总的来说,afx.h和window.h是Windows编程中不可或缺的头文件,开发人员需要熟练掌握两个头文件的使用方法,并在编写Windows程序时做好头文件的引用和使用。
相关问题

在MFC++6.0中用鼠标选择圆心和半径实现椭圆的扫描转换的代码

以下是一个简单的MFC++ 6.0示例程序,可以使用鼠标选择椭圆的中心和半径,并使用扫描转换算法绘制椭圆: ```cpp // EllipseScanConversionDlg.h #if !defined(AFX_ELLIPSESCANCONVERSIONDLG_H__E7BD3A2A_6A5E_4D8F_AE82_8F1B44C55D98__INCLUDED_) #define AFX_ELLIPSESCANCONVERSIONDLG_H__E7BD3A2A_6A5E_4D8F_AE82_8F1B44C55D98__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ///////////////////////////////////////////////////////////////////////////// // CEllipseScanConversionDlg dialog class CEllipseScanConversionDlg : public CDialog { // Construction public: CEllipseScanConversionDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CEllipseScanConversionDlg) enum { IDD = IDD_ELLIPSESCANCONVERSION_DIALOG }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CEllipseScanConversionDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; int m_nXCenter, m_nYCenter, m_nXRadius, m_nYRadius; // Generated message map functions //{{AFX_MSG(CEllipseScanConversionDlg) virtual BOOL OnInitDialog(); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_ELLIPSESCANCONVERSIONDLG_H__E7BD3A2A_6A5E_4D8F_AE82_8F1B44C55D98__INCLUDED_) ``` ```cpp // EllipseScanConversionDlg.cpp #include "stdafx.h" #include "EllipseScanConversion.h" #include "EllipseScanConversionDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CEllipseScanConversionDlg dialog CEllipseScanConversionDlg::CEllipseScanConversionDlg(CWnd* pParent /*=NULL*/) : CDialog(CEllipseScanConversionDlg::IDD, pParent) { //{{AFX_DATA_INIT(CEllipseScanConversionDlg) //}}AFX_DATA_INIT m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_nXCenter = m_nYCenter = m_nXRadius = m_nYRadius = 0; } void CEllipseScanConversionDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CEllipseScanConversionDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CEllipseScanConversionDlg, CDialog) //{{AFX_MSG_MAP(CEllipseScanConversionDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_LBUTTONDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CEllipseScanConversionDlg message handlers BOOL CEllipseScanConversionDlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 CEllipseScanConversionDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here if (m_nXRadius > 0 && m_nYRadius > 0) { int x, y; double dAngle; const double PI = 3.1415926536; const double dStep = PI / 180.0; CPoint ptCenter(m_nXCenter, m_nYCenter); for (int i = 0; i < 360; i++) { dAngle = i * dStep; x = m_nXRadius * cos(dAngle) + 0.5; y = m_nYRadius * sin(dAngle) + 0.5; dc.SetPixel(ptCenter.x + x, ptCenter.y + y, RGB(255, 0, 0)); } } } HCURSOR CEllipseScanConversionDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CEllipseScanConversionDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default m_nXCenter = point.x; m_nYCenter = point.y; m_nXRadius = m_nYRadius = 0; Invalidate(); CDialog::OnLButtonDown(nFlags, point); } ``` 在这个示例程序中,我们使用`OnLButtonDown`处理程序来记录鼠标单击事件的位置,并使用`OnPaint`处理程序来绘制椭圆。 在`OnPaint`处理程序中,我们使用扫描转换算法来绘制椭圆的每个像素。 您可以更改此示例程序以适应您的需求。

MFC 自定义控件demo

好的,以下是一个简单的 MFC 自定义控件的 demo: 1. 首先创建一个 MFC 应用程序项目,命名为 CustomControlDemo。 2. 在项目中创建一个新的 C++ 类,命名为 CCustomControl,继承自 CWnd。 3. 在 CCustomControl.h 中添加以下代码: ``` #pragma once class CCustomControl : public CWnd { public: CCustomControl(); protected: afx_msg void OnPaint(); DECLARE_MESSAGE_MAP() }; ``` 4. 在 CCustomControl.cpp 中添加以下代码: ``` #include "stdafx.h" #include "CustomControlDemo.h" #include "CCustomControl.h" CCustomControl::CCustomControl() { // 初始化代码 } void CCustomControl::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不要在调用 CWnd::OnPaint() 之前绘制 // ... // 结束绘制后,调用 CWnd::OnPaint() CWnd::OnPaint(); } BEGIN_MESSAGE_MAP(CCustomControl, CWnd) ON_WM_PAINT() END_MESSAGE_MAP() ``` 5. 在 CMainFrame.h 中添加以下代码: ``` #pragma once #include "CCustomControl.h" class CMainFrame : public CFrameWnd { public: CMainFrame(); protected: CCustomControl m_customControl; // 自定义控件 DECLARE_MESSAGE_MAP() }; ``` 6. 在 CMainFrame.cpp 中添加以下代码: ``` #include "stdafx.h" #include "CustomControlDemo.h" #include "CMainFrame.h" CMainFrame::CMainFrame() { Create(NULL, _T("Custom Control Demo"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 640, 480)); // 创建自定义控件 m_customControl.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, CRect(50, 50, 200, 200), this, 1000); } BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) END_MESSAGE_MAP() ``` 7. 现在编译并运行程序,你应该能够看到一个窗口和一个矩形自定义控件。 这只是一个简单的 demo,你可以根据自己的需求添加更多的代码来实现更复杂的自定义控件。

相关推荐

doc
用MFC VC++实现的时钟源代码 // MFCFrame1View.cpp : implementation of the CMFCFrame1View class // #include "stdafx.h" #include "MFCFrame1.h" #include "MFCFrame1Doc.h" #include "MFCFrame1View.h" #include "PointDialog.h" #include "math.h" GLUquadricObj *objCylinder = gluNewQuadric(); #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View IMPLEMENT_DYNCREATE(CMFCFrame1View, CView) BEGIN_MESSAGE_MAP(CMFCFrame1View, CView) //{{AFX_MSG_MAP(CMFCFrame1View) ON_WM_CREATE() ON_WM_DESTROY() ON_WM_SIZE() ON_COMMAND(IDM_ZIXUAN, OnZixuan) ON_WM_TIMER() ON_COMMAND(IDM_ChangDirect, OnChangDirect) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View construction/destruction CMFCFrame1View::CMFCFrame1View() { // TODO: add construction code here this->m_GLPixelIndex = 0; this->m_hGLContext = NULL; Angle1=0.0; Angle2=30.0; Timer=0; x=0.0; z=0.0; juli=40.0; } CMFCFrame1View::~CMFCFrame1View() { } BOOL CMFCFrame1View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS); return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View drawing ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View printing BOOL CMFCFrame1View::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMFCFrame1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMFCFrame1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View diagnostics #ifdef _DEBUG void CMFCFrame1View::AssertValid() const { CView::AssertValid(); } void CMFCFrame1View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMFCFrame1Doc* CMFCFrame1View::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCFrame1Doc))); return (CMFCFrame1Doc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMFCFrame1View message handlers BOOL CMFCFrame1View::SetWindowPixelFormat(HDC hDC) { PIXELFORMATDESCRIPTOR pixelDesc= { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL| PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI, PFD_TYPE_RGBA, 24, 0,0,0,0,0,0, 0, 0, 0, 0,0,0,0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc); if(this->m_GLPixelIndex==0) { this->m_GLPixelIndex = 1; if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0) { return FALSE; } } if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE) { return FALSE; } return TRUE; } int CMFCFrame1View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here HWND hWnd = this->GetSafeHwnd(); HDC hDC = ::GetDC(hWnd); if(this->SetWindowPixelFormat(hDC)==FALSE) { return 0; } if(this->CreateViewGLContext(hDC)==FALSE) { return 0; } return 0; } BOOL CMFCFrame1View::CreateViewGLContext(HDC hDC) { this->m_hGLContext = wglCreateContext(hDC); if(this->m_hGLContext==NULL) {//创建失败 return FALSE; } if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE) {//选为当前RC失败 return FALSE; } return TRUE; } void CMFCFrame1View::OnDestroy() { CView::OnDestroy(); // TODO: Add your message handler code here if(wglGetCurrentContext()!=NULL) { wglMakeCurrent(NULL,NULL); } if(this->m_hGLContext!=NULL) { wglDeleteContext(this->m_hGLContext); this->m_hGLContext = NULL; } } void CMFCFrame1View::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); // TODO: Add your message handler code here GLsizei width,height; GLdouble aspect; width = cx; height = cy; if(cy==0) { aspect = (GLdouble)width; } else { aspect = (GLdouble)width/(GLdouble)height; } glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40.0,aspect,5.0,1000.0); } void CMFCFrame1View::OnDraw(CDC* pDC) { CMFCFrame1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here CPaintDC dc(this); glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0); glRotatef(-90.0,1.0,0.0,0.0);/*返回原坐标*/ glTranslatef(-3.0,0.0,0.0); SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } void CMFCFrame1View::OnZixuan() { // TODO: Add your command handler code here Timer=1; SetTimer(1,100,NULL); } void CMFCFrame1View::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default CPaintDC dc(this); if (Timer==1) { Angle1=Angle1-1; Angle2=Angle2-1; glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW);/**/ glLoadIdentity(); gluLookAt(0.0,20.0,0.000000001,0.0,0.0,0.0,0.0,1.0,0.0); glPushMatrix(); glColor3f(0.7,0.7,0.7); glTranslatef(0.0,0.0,1.1); glRotatef(-90.0,1.0,0.0,0.0); glutSolidCone(5.0,0.0,60.0,60.0); /*底盘*/ glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(3.9,0.99,1.0); /*刻度*/ glRotatef(90.0,0.0,1.0,0.0); gluCylinder(objCylinder, 0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(-4.7,0.99,1.0); /*刻度*/ glRotatef(90.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(0.0,0.99,-2.9); /*刻度*/ glRotatef(180.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05, 0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.0); glTranslatef(0.0,0.99,5.8); /*刻度*/ glRotatef(180.0,0.0,1.0,0.0); gluCylinder(objCylinder,0.05, 0.05,0.8, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,1.0,0.0); glRotatef(45.0,0.0,1.0,0.0); glTranslatef(-0.67,0.99,0.7); /*时针*/ glRotatef(Angle1/129600,0.0,1.0,0.0); gluCylinder(objCylinder, 0.07, 0.02, 2.5, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,0.0); glTranslatef(0.0,0.99,1.0); /*分针*/ glRotatef(Angle2/360,0.0,1.0,0.0); gluCylinder(objCylinder, 0.05, 0.02, 3.5, 9999, 9); glPopMatrix(); glPushMatrix(); glColor3f(0.0,0.0,0.5); glTranslatef(0.0,0.99,1.0); /*秒针*/ glRotatef(Angle1,0.0,1.0,0.0); gluCylinder(objCylinder, 0.07, 0.02, 4.5, 9999, 9); glPopMatrix(); SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } else if(Timer==2) { glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); if (juli>12.0) { glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/ glLoadIdentity(); gluLookAt(0.0,8.0,juli,0.0,0.0,0.0,0.0,1.0,0.0); juli=juli-0.1; glPushMatrix(); glColor3f(0.0,0.0,0.0); glRotatef(-90.0,1.0,0.0,0.0); glutWireCone(40.0,0.0,30.0,30.0); /*画高度为0的圆锥*/ glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glLineWidth(4.0); glTranslatef(4.0,1.0,0.0); glutWireOctahedron(); /*画八面体*/ glLineWidth(1.0); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glTranslatef(0.0,1.1,0.0); glRotatef(Angle2,0.0,1.0,0.0); gluCylinder(objCylinder, 1.0, 1.0, 10.0, 9999, 9); /*画壶*/ glPopMatrix(); } else if(juli<=12.0) { Angle2=Angle2+0.01; if (Angle2==360.0) Angle2=0.0; glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/ glLoadIdentity(); x=12.0*sin(Angle2); z=12.0*cos(Angle2); gluLookAt(x,5.0,z,0.0,0.0,0.0,0.0,1.0,0.0); glPushMatrix(); glColor3f(0.0,0.0,0.0); glRotatef(-90.0,1.0,0.0,0.0); glutWireCone(40.0,0.0,30.0,30.0); /*画高度为0的圆锥*/ glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glLineWidth(4.0); glTranslatef(4.0,1.0,0.0); glutWireOctahedron(); /*画八面体*/ glLineWidth(1.0); glPopMatrix(); glPushMatrix(); glColor3f(1.0,0.0,1.0); glTranslatef(0.0,1.1,0.0); gluCylinder(objCylinder, 1.0, 1.0, 10.0, 9999, 9); /*画壶*/ glPopMatrix(); } SwapBuffers(dc.m_ps.hdc); glDrawBuffer (GL_BACK); glFlush(); } CView::OnTimer(nIDEvent); } void CMFCFrame1View::OnChangDirect() { // TODO: Add your command handler code here Timer=2; SetTimer(1,100 ,NULL); }

VC mfc单文档中代码如下void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); for (int i=0;iSelectObject(&br); pDC->Ellipse(points[i].x-r,points[i].y-r,points[i].x+r,points[i].y+r); br.DeleteObject(); } // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center=point; r=rand()%46+5;//r=5~50 color=RGB(rand()%256,rand()%256,rand()%256); points.push_back(center); SetTimer(1,200,NULL); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { for(int i=0;i<points.size();i++) { points[i].y-=5; if(points[i].y<-r) { points.erase(points.begin()+i); i--; } } } void CMyView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent==1){ RedrawWindow(); rise(); } CView::OnTimer(nIDEvent); },运行效果中圆在上升过程中颜色和大小不停的变换,应怎么修改此代码使得圆在上升过程中的大小和颜色不会变换,完整步骤及代码

要使圆在上升过程中大小和颜色不会变换,可以将颜色和大小的变化单独提取出来,放到循环外面进行定义,然后再在循环内部进行圆的上升操作。具体实现方式如下: 1. 在头文件中定义变量: cpp // 头文件中定义变量 COLORREF color; // 圆的颜色 int r; // 圆的半径 2. 在OnDraw函数中修改绘制圆的代码: cpp void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // 绘制圆 for (int i = 0;i < points.size();i++) { // 保持颜色和大小不变 color = RGB(0, 0, 255); // 蓝色 r = 20; // 绘制圆 CPen pen(PS_SOLID, 1, color); CBrush brush(color); pDC->SelectObject(&pen); pDC->SelectObject(&brush); pDC->Ellipse(points[i].x - r, points[i].y - r, points[i].x + r, points[i].y + r); } } 在这个代码中,我们将颜色和大小的变化都删除了,直接将颜色设置为蓝色,大小设置为20。这样就可以让圆在上升过程中保持颜色和大小不变了。 完整修改后的代码如下: MyView.h文件: cpp #pragma once #include "MyDoc.h" #include <vector> class CMyView : public CView { protected: // create from serialization only CMyView() noexcept; DECLARE_DYNCREATE(CMyView) // Attributes public: CMyDoc* GetDocument() const; // Operations public: // Overrides public: virtual void OnDraw(CDC* pDC) override; virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual void OnInitialUpdate() override; // called first time after construct virtual void OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/) override; // Implementation public: virtual ~CMyView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: DECLARE_MESSAGE_MAP() public: afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnTimer(UINT_PTR nIDEvent); void rise(); }; #ifndef _DEBUG // debug version in MyView.cpp inline CMyDoc* CMyView::GetDocument() const { return reinterpret_cast<CMyDoc*>(m_pDocument); } #endif MyView.cpp文件: cpp #include "pch.h" #include "framework.h" #include "My.h" #include "MyDoc.h" #include "MyView.h" #ifdef _DEBUG #define new DEBUG_NEW #endif IMPLEMENT_DYNCREATE(CMyView, CView) BEGIN_MESSAGE_MAP(CMyView, CView) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_TIMER() END_MESSAGE_MAP() // CMyView construction/destruction CMyView::CMyView() noexcept { // TODO: add construction code here } CMyView::~CMyView() { } BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } // CMyView drawing void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // 绘制圆 for (int i = 0;i < points.size();i++) { // 保持颜色和大小不变 color = RGB(0, 0, 255); // 蓝色 r = 20; // 绘制圆 CPen pen(PS_SOLID, 1, color); CBrush brush(color); pDC->SelectObject(&pen); pDC->SelectObject(&brush); pDC->Ellipse(points[i].x - r, points[i].y - r, points[i].x + r, points[i].y + r); } } void CMyView::OnInitialUpdate() { CView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class } void CMyView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: Add your specialized code here and/or call the base class } // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() const // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG // CMyView message handlers void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center = point; r = rand() % 46 + 5; color = RGB(rand() % 256, rand() % 256, rand() % 256); points.push_back(center); SetTimer(1, 200, NULL); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { for (int i = 0;i < points.size();i++) { points[i].y -= 5; if (points[i].y < -r) { points.erase(points.begin() + i); i--; } } } void CMyView::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default if (nIDEvent == 1) { RedrawWindow(); rise(); } CView::OnTimer(nIDEvent); } 这样修改后,圆在上升过程中的大小和颜色就不会变换了。
以下是一个简单的 MFC 计算器的代码: 首先,可以使用 MFC 库中的 CWnd 类来创建窗口和处理事件。我们需要创建一个派生自 CWnd 的类,我们称之为 CCalculatorWnd。 CCalculatorWnd.h 文件: cpp #pragma once class CCalculatorWnd : public CWnd { public: CCalculatorWnd(); protected: afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnPaint(); DECLARE_MESSAGE_MAP() private: CString m_strDisplay; double m_dResult; CString m_strOperator; BOOL m_bDecimal; BOOL m_bNewNumber; BOOL m_bError; }; CCalculatorWnd.cpp 文件: cpp #include "stdafx.h" #include "Calculator.h" #include "CalculatorWnd.h" #ifdef _DEBUG #define new DEBUG_NEW #endif CCalculatorWnd::CCalculatorWnd() { m_strDisplay = _T("0"); m_dResult = 0.0; m_strOperator = _T(""); m_bDecimal = FALSE; m_bNewNumber = TRUE; m_bError = FALSE; } BEGIN_MESSAGE_MAP(CCalculatorWnd, CWnd) ON_WM_CHAR() ON_WM_LBUTTONDOWN() ON_WM_PAINT() END_MESSAGE_MAP() void CCalculatorWnd::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { if (m_bError) { m_strDisplay = _T("0"); m_dResult = 0.0; m_strOperator = _T(""); m_bDecimal = FALSE; m_bNewNumber = TRUE; m_bError = FALSE; } switch (nChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (m_bNewNumber) { m_strDisplay = _T(""); m_bNewNumber = FALSE; } m_strDisplay += nChar; break; case '.': if (m_bNewNumber) { m_strDisplay = _T("0"); m_bNewNumber = FALSE; } if (!m_bDecimal) { m_strDisplay += nChar; m_bDecimal = TRUE; } break; case '+': case '-': case '*': case '/': if (!m_strOperator.IsEmpty()) { OnLButtonDown(0, CPoint(0, 0)); } m_bNewNumber = TRUE; m_bDecimal = FALSE; m_strOperator = nChar; break; case '=': OnLButtonDown(0, CPoint(0, 0)); m_strOperator = _T(""); break; case '\b': if (m_strDisplay.GetLength() > 1) { m_strDisplay = m_strDisplay.Left(m_strDisplay.GetLength() - 1); } else { m_strDisplay = _T("0"); m_bNewNumber = TRUE; } break; case 'c': case 'C': m_strDisplay = _T("0"); m_dResult = 0.0; m_strOperator = _T(""); m_bDecimal = FALSE; m_bNewNumber = TRUE; break; } Invalidate(); } void CCalculatorWnd::OnLButtonDown(UINT nFlags, CPoint point) { double dOperand = _wtof(m_strDisplay); if (m_strOperator.IsEmpty()) { m_dResult = dOperand; } else { switch (m_strOperator[0]) { case '+': m_dResult += dOperand; break; case '-': m_dResult -= dOperand; break; case '*': m_dResult *= dOperand; break; case '/': if (dOperand == 0.0) { m_strDisplay = _T("Error"); m_bError = TRUE; return; } else { m_dResult /= dOperand; } break; } } CString strResult; strResult.Format(_T("%g"), m_dResult); m_strDisplay = strResult; m_bNewNumber = TRUE; m_bDecimal = FALSE; } void CCalculatorWnd::OnPaint() { CPaintDC dc(this); CRect rcClient; GetClientRect(&rcClient); CBrush brush; brush.CreateSolidBrush(RGB(255, 255, 255)); dc.FillRect(&rcClient, &brush); CFont font; font.CreateFont(20, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("Arial")); dc.SelectObject(&font); rcClient.DeflateRect(5, 5); dc.DrawText(m_strDisplay, &rcClient, DT_RIGHT | DT_SINGLELINE | DT_VCENTER); } 最后,在主函数中创建窗口并运行: cpp #include "stdafx.h" #include "Calculator.h" #include "CalculatorWnd.h" #ifdef _DEBUG #define new DEBUG_NEW #endif int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); AfxWinInit(hInstance, NULL, ::GetCommandLine(), 0); CCalculatorWnd wnd; wnd.Create(NULL, _T("Calculator"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 200, 200)); wnd.ShowWindow(nCmdShow); wnd.UpdateWindow(); MSG msg; while (::GetMessage(&msg, NULL, 0, 0)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } return (int)msg.wParam; } 现在,你就可以编译和运行代码,得到一个简单的 MFC 计算器。
好的,以下是一个使用MFC框架的示例代码,演示如何在Windows环境下使用MFC绘制分数随程序执行次数变化的图表: cpp #include <afxwin.h> #include <afxext.h> #include <afxcmn.h> #include <vector> #include <random> using namespace std; class CMyFrame : public CFrameWnd { public: CMyFrame() { Create(NULL, _T("Score vs. Program Execution Times"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 640, 480)); m_chart.Create(WS_CHILD | WS_VISIBLE, CRect(10, 10, 620, 420), this, 100); m_chart.SetTitle(_T("Score vs. Program Execution Times")); m_chart.SetXAxisName(_T("Program Execution Times")); m_chart.SetYAxisName(_T("Score")); } protected: afx_msg void OnPaint() { CPaintDC dc(this); m_chart.Draw(&dc); } DECLARE_MESSAGE_MAP() private: CChartCtrl m_chart; }; BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd) ON_WM_PAINT() END_MESSAGE_MAP() int RunProgram() { // 执行程序并返回分数 random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(0, 100); return dis(gen); } void VisualizeScores(int k) { // 存储每次执行程序的分数 vector<double> scores(k); // 执行程序k次并记录分数 for (int i = 0; i < k; i++) { scores[i] = RunProgram(); } // 绘制分数随程序执行次数变化的图表 CMyFrame frame; CPoint pos(10, 10); CSize size(610, 390); frame.m_chart.AddLineSerie(scores, _T("Score"), RGB(255, 0, 0), pos, size); frame.ShowWindow(SW_SHOW); frame.UpdateWindow(); } int main() { VisualizeScores(10); CWinApp app; return app.Run(); } 这个示例代码中,RunProgram()函数是一个模拟程序执行的函数,它返回一个随机分数。VisualizeScores(k)函数是主函数,它执行程序k次并在MFC窗口中绘制分数随程序执行次数变化的图表。在这个示例中,我们测试了执行程序10次的情况。你可以根据需要修改VisualizeScores()函数以适应你的需求。 请注意,MFC框架需要在Visual Studio等C++开发环境中编译和运行。在编译和运行代码之前,请确保你已经安装了Visual Studio或其他支持MFC的C++开发环境。
以下是在 MFC++6.0 中使用鼠标选择圆心实现椭圆的扫描转换的示例代码: C++ // MyEllipse.h 文件 class CMyEllipse : public CObject { public: CPoint m_Center; // 圆心坐标 int m_RadiusX; // X半径 int m_RadiusY; // Y半径 // 构造函数 CMyEllipse(CPoint center = CPoint(0, 0), int radiusX = 0, int radiusY = 0); // 绘制椭圆 void DrawEllipse(CDC* pDC); }; // MyEllipse.cpp 文件 CMyEllipse::CMyEllipse(CPoint center, int radiusX, int radiusY) { m_Center = center; m_RadiusX = radiusX; m_RadiusY = radiusY; } void CMyEllipse::DrawEllipse(CDC* pDC) { // 计算椭圆上的点并绘制 int x, y; float f; for (x = m_Center.x - m_RadiusX, y = m_Center.y, f = 0; x <= m_Center.x + m_RadiusX; x++, f += 1.0f) { y = (int)(m_Center.y + m_RadiusY * sqrt(1 - pow(f - m_Center.x + 0.5f, 2) / pow(m_RadiusX, 2)) + 0.5f); pDC->SetPixel(x, y, RGB(0, 0, 0)); y = (int)(m_Center.y - m_RadiusY * sqrt(1 - pow(f - m_Center.x + 0.5f, 2) / pow(m_RadiusX, 2)) + 0.5f); pDC->SetPixel(x, y, RGB(0, 0, 0)); } for (y = m_Center.y - m_RadiusY, x = m_Center.x, f = 0; y <= m_Center.y + m_RadiusY; y++, f += 1.0f) { x = (int)(m_Center.x + m_RadiusX * sqrt(1 - pow(f - m_Center.y + 0.5f, 2) / pow(m_RadiusY, 2)) + 0.5f); pDC->SetPixel(x, y, RGB(0, 0, 0)); x = (int)(m_Center.x - m_RadiusX * sqrt(1 - pow(f - m_Center.y + 0.5f, 2) / pow(m_RadiusY, 2)) + 0.5f); pDC->SetPixel(x, y, RGB(0, 0, 0)); } } // MyEllipseDlg.h 文件 class CMyEllipseDlg : public CDialog { public: CMyEllipse m_Ellipse; // 椭圆 BOOL m_bDrawing; // 是否正在绘制 CPoint m_StartPoint; // 绘制起点 CPoint m_EndPoint; // 绘制终点 // 构造函数 CMyEllipseDlg(CWnd* pParent = NULL); // 对话框数据 enum { IDD = IDD_MYELLIPSE_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 消息映射 afx_msg void OnPaint(); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); DECLARE_MESSAGE_MAP() }; // MyEllipseDlg.cpp 文件 CMyEllipseDlg::CMyEllipseDlg(CWnd* pParent /*=NULL*/) : CDialog(CMyEllipseDlg::IDD, pParent) { m_bDrawing = FALSE; } void CMyEllipseDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CMyEllipseDlg, CDialog) ON_WM_PAINT() ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() void CMyEllipseDlg::OnPaint() { CPaintDC dc(this); m_Ellipse.DrawEllipse(&dc); } void CMyEllipseDlg::OnLButtonDown(UINT nFlags, CPoint point) { m_bDrawing = TRUE; m_StartPoint = point; m_EndPoint = point; } void CMyEllipseDlg::OnMouseMove(UINT nFlags, CPoint point) { if (m_bDrawing) { CClientDC dc(this); dc.SetROP2(R2_NOT); dc.SelectStockObject(NULL_BRUSH); dc.Ellipse(m_StartPoint.x, m_StartPoint.y, m_EndPoint.x, m_EndPoint.y); m_EndPoint = point; dc.Ellipse(m_StartPoint.x, m_StartPoint.y, m_EndPoint.x, m_EndPoint.y); } } void CMyEllipseDlg::OnLButtonUp(UINT nFlags, CPoint point) { m_bDrawing = FALSE; m_Ellipse.m_Center = CPoint((m_StartPoint.x + m_EndPoint.x) / 2, (m_StartPoint.y + m_EndPoint.y) / 2); m_Ellipse.m_RadiusX = abs(m_EndPoint.x - m_StartPoint.x) / 2; m_Ellipse.m_RadiusY = abs(m_EndPoint.y - m_StartPoint.y) / 2; Invalidate(FALSE); } // MyEllipse.rc 文件 IDD_MYELLIPSE_DIALOG DIALOGEX 0, 0, 200, 200 STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_APPWINDOW CAPTION "MyEllipse" FONT 8, "MS Shell Dlg", 0, 0, 0x1 BEGIN CONTROL "",IDC_STATIC,"Static",SS_BLACKFRAME,7,7,186,186 END // MyEllipse.cpp 文件 #include "stdafx.h" #include "MyEllipse.h" #include "MyEllipseDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif BEGIN_MESSAGE_MAP(CMyEllipseApp, CWinApp) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) END_MESSAGE_MAP() CMyEllipseApp::CMyEllipseApp() { } CMyEllipseApp theApp; BOOL CMyEllipseApp::InitInstance() { CWinApp::InitInstance(); CMyEllipseDlg dlg; m_pMainWnd = &dlg; dlg.DoModal(); return FALSE; } 在上述代码中,我们定义了一个 CMyEllipse 类来表示椭圆,其中包括圆心坐标和 X、Y 半径。DrawEllipse() 方法用于计算椭圆上的点并绘制。 我们还定义了一个 CMyEllipseDlg 类作为主界面,并在界面上重写了鼠标事件以实现绘制椭圆的功能。当鼠标左键按下时,我们记录下起点并将 m_bDrawing 标志设置为 TRUE,表示正在绘制。当鼠标移动时,我们使用 CClientDC 对象绘制一个矩形来表示正在绘制的椭圆,并在移动过程中不断更新 m_EndPoint 坐标。当鼠标左键释放时,我们将 m_bDrawing 标志设置为 FALSE,然后计算出椭圆的圆心坐标和 X、Y 半径,最后绘制出整个椭圆。 最后,我们在 MyEllipse.rc 文件中定义了主界面的样式和布局,并在 MyEllipse.cpp 文件中启动了应用程序。
CEF3是一个开源的Chromium项目,可以方便地在自己的应用程序中嵌入现代Web技术。而MFC是微软基于Windows的GUI编程框架,用于开发Windows应用程序。在本教程中,我们将介绍如何将CEF3集成到MFC多文档视图应用程序中。 1. 创建一个MFC多文档应用程序项目 首先,我们需要创建一个MFC多文档应用程序项目。在Visual Studio中,选择“文件”>“新建”>“项目”,然后选择“MFC应用程序向导”。在向导中,选择“多文档”应用程序类型,并选择所需的选项。 2. 下载CEF3库文件 从CEF官网上下载最新的CEF3库文件,解压缩到本地文件夹中。 3. 将CEF3库文件添加到项目中 将CEF3库文件中的“Release”文件夹中的所有文件复制到MFC应用程序的“Debug”文件夹中。将CEF3库文件中的“Resources”文件夹中的所有文件复制到MFC应用程序的“Debug”文件夹中的“Resources”子文件夹中。 4. 添加CEF3头文件和链接库 在Visual Studio中,打开项目属性。在“VC++目录”中,添加CEF3库文件的头文件路径。在“链接器”中,添加CEF3库文件的链接库路径。在“输入”中,添加CEF3库文件的链接库名称。 5. 集成CEF3到MFC多文档视图应用程序 在MFC应用程序中,打开“MainFrm.cpp”文件。添加以下代码段到“CMainFrame”类的声明中: #include "cefclient.h" #include "client_handler.h" class CefView : public CWnd { public: CefView() {} virtual ~CefView() {} protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDestroy(); afx_msg void OnSize(UINT nType, int cx, int cy); DECLARE_MESSAGE_MAP() CefRefPtr<ClientHandler> m_ClientHandler; IMPLEMENT_REFCOUNTING(CefView); }; 在“MainFrm.cpp”文件中,添加以下代码段到“CMainFrame”类的实现中: BEGIN_MESSAGE_MAP(CefView, CWnd) ON_WM_CREATE() ON_WM_DESTROY() ON_WM_SIZE() END_MESSAGE_MAP() BOOL CefView::PreCreateWindow(CREATESTRUCT& cs) { if (!CWnd::PreCreateWindow(cs)) return FALSE; cs.dwExStyle |= WS_EX_CLIENTEDGE; cs.style &= ~WS_BORDER; return TRUE; } int CefView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CWnd::OnCreate(lpCreateStruct) == -1) return -1; CefSettings settings; CefInitialize(settings); CefWindowInfo window_info; CefRefPtr<ClientHandler> client_handler(new ClientHandler); m_ClientHandler = client_handler; RECT rect; GetClientRect(&rect); window_info.SetAsChild(m_hWnd, rect); CefBrowserSettings browser_settings; std::string url = "https://www.baidu.com"; CefBrowserHost::CreateBrowserSync(window_info, client_handler.get(), url, browser_settings, NULL); return 0; } void CefView::OnDestroy() { CefShutdown(); CWnd::OnDestroy(); } void CefView::OnSize(UINT nType, int cx, int cy) { CWnd::OnSize(nType, cx, cy); if (m_ClientHandler.get() != NULL && m_ClientHandler->GetBrowser().get() != NULL) { CefRect rect; rect.x = 0; rect.y = 0; rect.width = cx; rect.height = cy; m_ClientHandler->GetBrowser()->GetHost()->WasResized(); } } 在“MainFrm.cpp”文件中,添加以下代码段到“CMainFrame”类的构造函数中: CRect rect; GetClientRect(&rect); CefView* pView = new CefView; pView->Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, this, 0); 现在,我们已经将CEF3集成到MFC多文档视图应用程序中了。运行应用程序,您将看到嵌入的CEF3浏览器窗口显示了百度页面。
好的,请稍等,我为您生成一段MFC打地鼠代码: c++ #include <afxwin.h> #include <time.h> const int GAME_WIDTH = 500; const int GAME_HEIGHT = 500; const int TIMER_INTERVAL = 500; const int HOLE_SIZE = 50; const int HIT_THRESHOLD = 20; class CGameWindow : public CFrameWnd { public: CGameWindow() { Create(NULL, L"MFC打地鼠游戏", WS_OVERLAPPEDWINDOW, CRect(0, 0, GAME_WIDTH, GAME_HEIGHT)); srand(time(0)); SetTimer(1, TIMER_INTERVAL, NULL); } afx_msg void OnPaint() { CPaintDC dc(this); for (int i = 0; i < m_holes.size(); i++) { CRect holeRect = GetHoleRect(m_holes[i]); if (m_holes[i].hit) { dc.FillSolidRect(holeRect, RGB(255, 0, 0)); } else { dc.FillSolidRect(holeRect, RGB(127, 127, 127)); } } } afx_msg void OnLButtonDown(UINT flags, CPoint point) { for (int i = 0; i < m_holes.size(); i++) { if (GetHoleRect(m_holes[i]).PtInRect(point)) { if (!m_holes[i].hit) { m_holes[i].hit = true; Invalidate(); } } } } afx_msg void OnTimer(UINT_PTR timerID) { m_timerCount++; if (m_timerCount % 5 == 0) { GenerateNewHole(); } for (int i = 0; i < m_holes.size(); i++) { if (m_holes[i].hit) { m_holes[i].hitCountdown--; if (m_holes[i].hitCountdown == 0) { m_holes.erase(m_holes.begin() + i); i--; } } else { m_holes[i].lifetime--; if (m_holes[i].lifetime == 0) { m_holes.erase(m_holes.begin() + i); i--; } } } Invalidate(); } DECLARE_MESSAGE_MAP(); private: struct Hole { int x, y; bool hit; int hitCountdown; int lifetime; }; std::vector<Hole> m_holes; int m_timerCount = 0; CRect GetHoleRect(Hole hole) { return CRect(hole.x - HOLE_SIZE / 2, hole.y - HOLE_SIZE / 2, hole.x + HOLE_SIZE / 2, hole.y + HOLE_SIZE / 2); } void GenerateNewHole() { Hole newHole; newHole.x = rand() % (GAME_WIDTH - HOLE_SIZE) + HOLE_SIZE / 2; newHole.y = rand() % (GAME_HEIGHT - HOLE_SIZE) + HOLE_SIZE / 2; newHole.hit = false; newHole.hitCountdown = 3; newHole.lifetime = 10; m_holes.push_back(newHole); } }; BEGIN_MESSAGE_MAP(CGameWindow, CFrameWnd) ON_WM_PAINT() ON_WM_LBUTTONDOWN() ON_WM_TIMER() END_MESSAGE_MAP() class CGameApp : public CWinApp { public: virtual BOOL InitInstance() { CGameWindow* pMainWnd = new CGameWindow; m_pMainWnd = pMainWnd; pMainWnd->ShowWindow(m_nCmdShow); pMainWnd->UpdateWindow(); return TRUE; } }; CGameApp theApp; 这段代码实现一个简单的打地鼠游戏,当您单击地鼠,地鼠会变成红色,并在3秒钟后消失。游戏每隔0.5秒钟刷新一次画面,每隔5秒钟会生成一个新的地鼠出现在随机位置。请注意,这只是一个简单示例,您可能需要根据您的实际需求进行修改和扩展。
在 Visual C++ 中,可以按照以下步骤创建 MFC 应用程序并添加 OpenGL 控件: 1. 打开 Visual Studio,选择创建一个新项目。 2. 在创建新项目的向导中,选择“MFC 应用程序”并点击“下一步”。 3. 选择“桌面应用程序”并点击“下一步”。 4. 在“应用程序类型”中选择“单文档”或“多文档”,并点击“下一步”。 5. 在“高级特性”中勾选“使用 OpenGL”并点击“完成”。 6. 在类视图中打开“MainFrm.cpp”文件,在 CMainFrame::OnCreate() 函数中添加以下代码: C++ m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL); 7. 在类视图中打开“MainFrm.h”文件,在 CMainFrame 类的末尾添加以下代码: C++ protected: CView m_wndView; 8. 在类视图中打开“View.cpp”文件,在 CView::OnDraw() 函数中添加以下代码: C++ glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glFlush(); 9. 在类视图中打开“View.h”文件,在 CView 类的末尾添加以下代码: C++ protected: HGLRC m_hglrc; 10. 在 CView 类的 OnCreate() 函数中添加以下代码: C++ PIXELFORMATDESCRIPTOR pfd; int nPixelFormat; // Get the device context (DC) HDC hdc = ::GetDC(m_hWnd); // Set the pixel format descriptor ZeroMemory(&pfd, sizeof(pfd)); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; // Choose a pixel format that best matches that described in pfd nPixelFormat = ::ChoosePixelFormat(hdc, &pfd); // Set the pixel format for the device context ::SetPixelFormat(hdc, nPixelFormat, &pfd); // Create a rendering context (RC) m_hglrc = wglCreateContext(hdc); // Make the RC current wglMakeCurrent(hdc, m_hglrc); 11. 在 CView 类的 OnDestroy() 函数中添加以下代码: C++ // Get the device context (DC) HDC hdc = ::GetDC(m_hWnd); // Make the RC not current wglMakeCurrent(hdc, NULL); // Delete the RC wglDeleteContext(m_hglrc); 12. 在 CView 类的 OnSize() 函数中添加以下代码: C++ // Get the device context (DC) HDC hdc = ::GetDC(m_hWnd); // Set the viewport glViewport(0, 0, cx, cy); // Set the projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat)cx / (GLfloat)cy, 0.1f, 100.0f); // Set the modelview matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Release the device context ::ReleaseDC(m_hWnd, hdc); 这样,您就可以在 MFC 应用程序中添加 OpenGL 控件了。
以下是一个基于VC++ MFC的简易画图板代码示例: 首先,创建一个MFC应用程序,选择“单文档”类型,然后在资源编辑器中添加一个菜单项“画图板”。 在CMainFrame类中添加以下代码: cpp #include "MyView.h" class CMainFrame : public CFrameWnd { public: CMainFrame(); protected: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnPaint(); afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg void OnDrawLine(); afx_msg void OnDrawRectangle(); afx_msg void OnDrawEllipse(); DECLARE_MESSAGE_MAP() private: MyView m_wndView; }; CMainFrame::CMainFrame() { Create(NULL, _T("Drawing Board"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 800, 600)); } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL)) { TRACE0("Failed to create view window\n"); return -1; } return 0; } void CMainFrame::OnPaint() { CPaintDC dc(this); // device context for painting CRect rect; GetClientRect(&rect); dc.FillSolidRect(&rect, RGB(255, 255, 255)); } void CMainFrame::OnSize(UINT nType, int cx, int cy) { CFrameWnd::OnSize(nType, cx, cy); if (m_wndView.GetSafeHwnd()) m_wndView.MoveWindow(0, 0, cx, cy); } void CMainFrame::OnDrawLine() { m_wndView.SetDrawType(DRAW_LINE); } void CMainFrame::OnDrawRectangle() { m_wndView.SetDrawType(DRAW_RECTANGLE); } void CMainFrame::OnDrawEllipse() { m_wndView.SetDrawType(DRAW_ELLIPSE); } BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_WM_CREATE() ON_WM_PAINT() ON_WM_SIZE() ON_COMMAND(ID_DRAW_LINE, &CMainFrame::OnDrawLine) ON_COMMAND(ID_DRAW_RECTANGLE, &CMainFrame::OnDrawRectangle) ON_COMMAND(ID_DRAW_ELLIPSE, &CMainFrame::OnDrawEllipse) END_MESSAGE_MAP() 在MyView类中添加以下代码: cpp enum DRAW_TYPE { DRAW_NONE = 0, DRAW_LINE, DRAW_RECTANGLE, DRAW_ELLIPSE }; class MyView : public CView { protected: MyView(); DECLARE_DYNCREATE(MyView) public: virtual void OnDraw(CDC* pDC); virtual BOOL PreCreateWindow(CREATESTRUCT& cs); virtual ~MyView(); void SetDrawType(DRAW_TYPE type) { m_nDrawType = type; } protected: afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); DECLARE_MESSAGE_MAP() private: DRAW_TYPE m_nDrawType; CPoint m_ptStart; CPoint m_ptEnd; }; MyView::MyView() { m_nDrawType = DRAW_NONE; } MyView::~MyView() { } BOOL MyView::PreCreateWindow(CREATESTRUCT& cs) { return CView::PreCreateWindow(cs); } void MyView::OnDraw(CDC* pDC) { CDocument* pDoc = GetDocument(); // TODO: add draw code here } void MyView::OnLButtonDown(UINT nFlags, CPoint point) { m_ptStart = point; CView::OnLButtonDown(nFlags, point); } void MyView::OnMouseMove(UINT nFlags, CPoint point) { if (nFlags & MK_LBUTTON) { CDC* pDC = GetDC(); CPen pen; pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0)); pDC->SelectObject(&pen); pDC->SetROP2(R2_NOTXORPEN); switch (m_nDrawType) { case DRAW_LINE: pDC->MoveTo(m_ptStart); pDC->LineTo(m_ptEnd); m_ptEnd = point; pDC->MoveTo(m_ptStart); pDC->LineTo(m_ptEnd); break; case DRAW_RECTANGLE: pDC->Rectangle(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y); m_ptEnd = point; pDC->Rectangle(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y); break; case DRAW_ELLIPSE: pDC->Ellipse(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y); m_ptEnd = point; pDC->Ellipse(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y); break; default: break; } ReleaseDC(pDC); } CView::OnMouseMove(nFlags, point); } void MyView::OnLButtonUp(UINT nFlags, CPoint point) { m_ptEnd = point; CView::OnLButtonUp(nFlags, point); } BEGIN_MESSAGE_MAP(MyView, CView) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() END_MESSAGE_MAP() 在资源编辑器中为菜单项添加ID_DRAW_LINE、ID_DRAW_RECTANGLE和ID_DRAW_ELLIPSE命令ID。 最后,在CWinApp类的InitInstance()函数中添加以下代码: cpp BOOL CMyApp::InitInstance() { CWinApp::InitInstance(); CMainFrame* pFrame = new CMainFrame; m_pMainWnd = pFrame; pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL); m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } 现在,你就可以编译并运行这个简易画图板了,它可以画线、矩形和椭圆。
以下是一个简单的MFC串口调试助手的源码,可以实现串口的打开、关闭、发送和接收数据。需要注意的是,本程序仅供参考,具体实现可能因不同环境而异。 // SerialPortTestDlg.cpp : implementation file // #include "stdafx.h" #include "SerialPortTest.h" #include "SerialPortTestDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CSerialPortTestDlg dialog CSerialPortTestDlg::CSerialPortTestDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_SERIALPORTTEST_DIALOG, pParent) , m_strSend(_T("")) , m_strReceive(_T("")) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSerialPortTestDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT_SEND, m_strSend); DDX_Text(pDX, IDC_EDIT_RECEIVE, m_strReceive); } BEGIN_MESSAGE_MAP(CSerialPortTestDlg, CDialogEx) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_OPEN, &CSerialPortTestDlg::OnBnClickedButtonOpen) ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CSerialPortTestDlg::OnBnClickedButtonClose) ON_BN_CLICKED(IDC_BUTTON_SEND, &CSerialPortTestDlg::OnBnClickedButtonSend) ON_MESSAGE(WM_COMM_RXCHAR, &CSerialPortTestDlg::OnCommRxChar) END_MESSAGE_MAP() // CSerialPortTestDlg message handlers BOOL CSerialPortTestDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 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_pSerial = new CSerialPort; m_pSerial->m_hWnd = m_hWnd; // 初始化串口列表 for (int i = 1; i <= 16; i++) { CString strCom; strCom.Format(_T("COM%d"), i); ((CComboBox*)GetDlgItem(IDC_COMBO_COM))->AddString(strCom); } ((CComboBox*)GetDlgItem(IDC_COMBO_COM))->SetCurSel(0); // 初始化波特率列表 ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("1200")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("2400")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("4800")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("9600")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("19200")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("38400")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("57600")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("115200")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->SetCurSel(3); // 初始化数据位列表 ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->AddString(_T("5")); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->AddString(_T("6")); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->AddString(_T("7")); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->AddString(_T("8")); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->SetCurSel(3); // 初始化停止位列表 ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->AddString(_T("1")); ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->AddString(_T("1.5")); ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->AddString(_T("2")); ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->SetCurSel(0); // 初始化校验位列表 ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->AddString(_T("无")); ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->AddString(_T("奇校验")); ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->AddString(_T("偶校验")); ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->SetCurSel(0); return TRUE; // return TRUE unless you set the focus to a control } void CSerialPortTestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 { CDialogEx::OnPaint(); } } HCURSOR CSerialPortTestDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CSerialPortTestDlg::OnBnClickedButtonOpen() { // 获取串口参数 CString strCom, strBaudrate, strDatabits, strStopbits, strParity; ((CComboBox*)GetDlgItem(IDC_COMBO_COM))->GetWindowText(strCom); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->GetWindowText(strBaudrate); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->GetWindowText(strDatabits); ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->GetWindowText(strStopbits); ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->GetWindowText(strParity); // 设置串口参数 m_pSerial->m_nComPort = _ttoi(&strCom.Right(1)); m_pSerial->m_nBaud = _ttoi(strBaudrate); if (strDatabits == _T("5")) { m_pSerial->m_nDataBits = 5; } else if (strDatabits == _T("6")) { m_pSerial->m_nDataBits = 6; } else if (strDatabits == _T("7")) { m_pSerial->m_nDataBits = 7; } else if (strDatabits == _T("8")) { m_pSerial->m_nDataBits = 8; } if (strStopbits == _T("1")) { m_pSerial->m_nStopBits = ONESTOPBIT; } else if (strStopbits == _T("1.5")) { m_pSerial->m_nStopBits = ONE5STOPBITS; } else if (strStopbits == _T("2")) { m_pSerial->m_nStopBits = TWOSTOPBITS; } if (strParity == _T("无")) { m_pSerial->m_nParity = NOPARITY; } else if (strParity == _T("奇校验")) { m_pSerial->m_nParity = ODDPARITY; } else if (strParity == _T("偶校验")) { m_pSerial->m_nParity = EVENPARITY; } // 打开串口 if (m_pSerial->Open()) { GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(TRUE); } else { AfxMessageBox(_T("无法打开串口!")); } } void CSerialPortTestDlg::OnBnClickedButtonClose() { // 关闭串口 m_pSerial->Close(); GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(TRUE); GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(FALSE); } void CSerialPortTestDlg::OnBnClickedButtonSend() { // 发送数据 UpdateData(TRUE); m_pSerial->Write((LPCTSTR)m_strSend, m_strSend.GetLength()); } afx_msg LRESULT CSerialPortTestDlg::OnCommRxChar(WPARAM wParam, LPARAM lParam) { // 接收数据 BYTE szBuf[4096]; int nCount = m_pSerial->Read(szBuf, 4096); if (nCount > 0) { szBuf[nCount] = 0; m_strReceive += (LPCTSTR)szBuf; UpdateData(FALSE); } return 0; } 需要注意的是,本程序使用了一个名为“CSerialPort”的类来实现串口的打开、关闭、发送和接收数据的功能。该类的实现代码如下: class CSerialPort { public: CSerialPort() { m_hComm = NULL; m_hWnd = NULL; m_nComPort = 1; m_nBaud = 9600; m_nDataBits = 8; m_nStopBits = ONESTOPBIT; m_nParity = NOPARITY; } ~CSerialPort() { Close(); } BOOL Open() { Close(); CString strCom; strCom.Format(_T("\\\\.\\COM%d"), m_nComPort); m_hComm = CreateFile(strCom, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (m_hComm == INVALID_HANDLE_VALUE) { return FALSE; } DCB dcb; GetCommState(m_hComm, &dcb); dcb.BaudRate = m_nBaud; dcb.ByteSize = m_nDataBits; dcb.StopBits = m_nStopBits; dcb.Parity = m_nParity; SetCommState(m_hComm, &dcb); COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(m_hComm, &timeouts); SetCommMask(m_hComm, EV_RXCHAR); return TRUE; } void Close() { if (m_hComm) { CloseHandle(m_hComm); m_hComm = NULL; } } int Read(LPBYTE lpBuf, int nCount) { DWORD dwRead = 0; if (m_hComm && ReadFile(m_hComm, lpBuf, nCount, &dwRead, NULL)) { return (int)dwRead; } return 0; } BOOL Write(LPCTSTR lpBuf, int nCount) { DWORD dwWritten = 0; if (m_hComm && WriteFile(m_hComm, lpBuf, nCount, &dwWritten, NULL)) { return TRUE; } return FALSE; } HWND m_hWnd; int m_nComPort; int m_nBaud; int m_nDataBits; int m_nStopBits; int m_nParity; protected: HANDLE m_hComm; }; 该类的实现使用了Win32 API中的串口相关函数,可以通过修改该类的实现来适应不同的需求。

最新推荐

基于ASP.NET的洗衣房管理系统源码.zip

基于ASP.NET的洗衣房管理系统源码.zip

基于ASP.net图书商城系统源码.zip

基于ASP.net图书商城系统源码.zip

红色喜庆祝寿生日宴会策划方案PPT模板

红色喜庆祝寿生日宴会策划方案PPT模板

VC++: OPCDA开发简单示例

VC++: OPCDA开发简单示例

纯CSS带底部线条动画文本特效.rar

纯CSS带底部线条动画文本特效.rar

代码随想录最新第三版-最强八股文

这份PDF就是最强⼋股⽂! 1. C++ C++基础、C++ STL、C++泛型编程、C++11新特性、《Effective STL》 2. Java Java基础、Java内存模型、Java面向对象、Java集合体系、接口、Lambda表达式、类加载机制、内部类、代理类、Java并发、JVM、Java后端编译、Spring 3. Go defer底层原理、goroutine、select实现机制 4. 算法学习 数组、链表、回溯算法、贪心算法、动态规划、二叉树、排序算法、数据结构 5. 计算机基础 操作系统、数据库、计算机网络、设计模式、Linux、计算机系统 6. 前端学习 浏览器、JavaScript、CSS、HTML、React、VUE 7. 面经分享 字节、美团Java面、百度、京东、暑期实习...... 8. 编程常识 9. 问答精华 10.总结与经验分享 ......

基于交叉模态对应的可见-红外人脸识别及其表现评估

12046通过调整学习:基于交叉模态对应的可见-红外人脸识别Hyunjong Park*Sanghoon Lee*Junghyup Lee Bumsub Ham†延世大学电气与电子工程学院https://cvlab.yonsei.ac.kr/projects/LbA摘要我们解决的问题,可见光红外人重新识别(VI-reID),即,检索一组人的图像,由可见光或红外摄像机,在交叉模态设置。VI-reID中的两个主要挑战是跨人图像的类内变化,以及可见光和红外图像之间的跨模态假设人图像被粗略地对准,先前的方法尝试学习在不同模态上是有区别的和可概括的粗略的图像或刚性的部分级人表示然而,通常由现成的对象检测器裁剪的人物图像不一定是良好对准的,这分散了辨别性人物表示学习。在本文中,我们介绍了一种新的特征学习框架,以统一的方式解决这些问题。为此,我们建议利用密集的对应关系之间的跨模态的人的形象,年龄。这允许解决像素级中�

java二维数组矩阵相乘

矩阵相乘可以使用二维数组来实现,以下是Java代码示例: ```java public class MatrixMultiplication { public static void main(String[] args) { int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}}; // 定义一个2x3的矩阵 int[][] matrix2 = {{7, 8}, {9, 10}, {11, 12}}; // 定义一个3x2的矩阵 int[][] result = multiply(matrix1, matr

数据结构1800试题.pdf

你还在苦苦寻找数据结构的题目吗?这里刚刚上传了一份数据结构共1800道试题,轻松解决期末挂科的难题。不信?你下载看看,这里是纯题目,你下载了再来私信我答案。按数据结构教材分章节,每一章节都有选择题、或有判断题、填空题、算法设计题及应用题,题型丰富多样,共五种类型题目。本学期已过去一半,相信你数据结构叶已经学得差不多了,是时候拿题来练练手了,如果你考研,更需要这份1800道题来巩固自己的基础及攻克重点难点。现在下载,不早不晚,越往后拖,越到后面,你身边的人就越卷,甚至卷得达到你无法想象的程度。我也是曾经遇到过这样的人,学习,练题,就要趁现在,不然到时你都不知道要刷数据结构题好还是高数、工数、大英,或是算法题?学完理论要及时巩固知识内容才是王道!记住!!!下载了来要答案(v:zywcv1220)。

通用跨域检索的泛化能力

12056通用跨域检索:跨类和跨域的泛化2* Soka Soka酒店,Soka-马上预订;1印度理工学院,Kharagpur,2印度科学学院,班加罗尔soumava2016@gmail.com,{titird,somabiswas} @ iisc.ac.in摘要在这项工作中,我们第一次解决了通用跨域检索的问题,其中测试数据可以属于在训练过程中看不到的类或域。由于动态增加的类别数量和对每个可能的域的训练的实际约束,这需要大量的数据,所以对看不见的类别和域的泛化是重要的。为了实现这一目标,我们提出了SnMpNet(语义Neighbourhood和混合预测网络),它包括两个新的损失,以占在测试过程中遇到的看不见的类和域。具体来说,我们引入了一种新的语义邻域损失,以弥合可见和不可见类之间的知识差距,并确保潜在的空间嵌入的不可见类是语义上有意义的,相对于其相邻的类。我们还在图像级以及数据的语义级引入了基于混�