【无标题】

OpenCV_Test_01Dlg 


// OpenCV_Test_01Dlg.cpp : 实现文件
//

#include "stdafx.h"
#include "OpenCV_Test_01.h"
#include "OpenCV_Test_01Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


BOOL m_bRun;//线程是否正常运行 

UINT ThreadProc(LPVOID pParam) //线程函数  
{
	COpenCV_Test_01Dlg* pDlg = (COpenCV_Test_01Dlg*)pParam;
	while (pDlg->m_bRun)
	{
		// 打开视频捕获设备
		pDlg->capture.open(0); // 0 是默认摄像头
		if (!pDlg->capture.isOpened())
		{
			AfxMessageBox(_T("无法打开摄像头"));
			return 1;
		}
		pDlg->flg = TRUE;
		namedWindow("newWindows", WINDOW_AUTOSIZE);
		HWND hWnd = (HWND)cvGetWindowHandle("newWindows");
		::SetParent(hWnd, pDlg->GetDlgItem(IDC_VIDEO)->m_hWnd);
		CRect rect;
		pDlg->GetDlgItem(IDC_VIDEO)->GetWindowRect(&rect);
		::SetWindowPos(hWnd, HWND_BOTTOM, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
		Mat frame;
		while (pDlg->flg)
		{
			pDlg->capture >> frame;
			if (frame.empty())
			{
				AfxMessageBox(L"视频为空!");
				break;
			}
			if (pDlg->SaveImg)//抓图
			{
				//保存未调整大小的原始图像				
				cv::imwrite(pDlg->ImgPath + pDlg->GetSysTime() + ".jpg", frame);
				pDlg->SaveImg = FALSE;
			}
			//调整帧图像大小,适合控件的大小
			cv::Mat resizedImage;
			cv::resize(frame, resizedImage, cv::Size(rect.Width(), rect.Height()), cv::INTER_LINEAR);
			imshow("newWindows", resizedImage);
			//隐藏OpenCV窗口,但一直不起作用……,再研究
			//HWND cvhWnd = ::FindWindow(NULL,_T("newWindows"));
			//if(cvhWnd !=NULL)
			//{ 
			//	//::ShowWindow(cvhWnd, SW_HIDE);
			//	::SetWindowPos(cvhWnd, HWND_BOTTOM, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
			//}
			if ((char)waitKey(10) >= 0)
			{
				break;//按任意键推出
			}
		}
		pDlg->capture.release();
		destroyAllWindows();
	}
	return 0;
}

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// COpenCV_Test_01Dlg 对话框



COpenCV_Test_01Dlg::COpenCV_Test_01Dlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_OPENCV_TEST_01_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void COpenCV_Test_01Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(COpenCV_Test_01Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &COpenCV_Test_01Dlg::OnBnClickedButton1)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDCANCEL, &COpenCV_Test_01Dlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON3, &COpenCV_Test_01Dlg::OnBnClickedButton3)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON2, &COpenCV_Test_01Dlg::OnBnClickedButton2)
END_MESSAGE_MAP()


// COpenCV_Test_01Dlg 消息处理程序

BOOL COpenCV_Test_01Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	SaveImg = FALSE;
	ImgPath = "c:\\img\\";

	m_bRun = FALSE;
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void COpenCV_Test_01Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void COpenCV_Test_01Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR COpenCV_Test_01Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//打开相机
void COpenCV_Test_01Dlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	flg = TRUE;
	m_bRun = TRUE;	
	pThread = AfxBeginThread((AFX_THREADPROC)ThreadProc, this);
}


void COpenCV_Test_01Dlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值	
	CDialogEx::OnTimer(nIDEvent);
}


void COpenCV_Test_01Dlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnCancel();
}


void COpenCV_Test_01Dlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bRun = FALSE;
	flg = FALSE;
}

void COpenCV_Test_01Dlg::OnDestroy()
{
	CDialogEx::OnDestroy();
	//capture.release();
	//destroyAllWindows();
	// TODO: 在此处添加消息处理程序代码
}


void COpenCV_Test_01Dlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	SaveImg = TRUE;
}

// CString转std::string
std::string CStringToStdString(const CString& cstr) {
	return std::string(static_cast<LPCSTR>(CT2A(cstr.GetString())));
}

string COpenCV_Test_01Dlg::GetSysTime()
{
	CTime t = CTime::GetCurrentTime();
	CString strTime;
	strTime.Format(_T("%04d%02d%02d%02d%02d%02d"), t.GetYear(), t.GetMonth(), t.GetDay(), t.GetHour(), t.GetMinute(), t.GetSecond());
	return CStringToStdString(strTime); ;
}

OpenCV_Test_01 


// OpenCV_Test_01.cpp : 定义应用程序的类行为。
//

#include "stdafx.h"
#include "OpenCV_Test_01.h"
#include "OpenCV_Test_01Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// COpenCV_Test_01App

BEGIN_MESSAGE_MAP(COpenCV_Test_01App, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// COpenCV_Test_01App 构造

COpenCV_Test_01App::COpenCV_Test_01App()
{
	// 支持重新启动管理器
	m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

	// TODO: 在此处添加构造代码,
	// 将所有重要的初始化放置在 InitInstance 中
}


// 唯一的一个 COpenCV_Test_01App 对象

COpenCV_Test_01App theApp;


// COpenCV_Test_01App 初始化

BOOL COpenCV_Test_01App::InitInstance()
{
	// 如果一个运行在 Windows XP 上的应用程序清单指定要
	// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
	//则需要 InitCommonControlsEx()。  否则,将无法创建窗口。
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// 将它设置为包括所有要在应用程序中使用的
	// 公共控件类。
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();


	AfxEnableControlContainer();

	// 创建 shell 管理器,以防对话框包含
	// 任何 shell 树视图控件或 shell 列表视图控件。
	CShellManager *pShellManager = new CShellManager;

	// 激活“Windows Native”视觉管理器,以便在 MFC 控件中启用主题
	CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

	// 标准初始化
	// 如果未使用这些功能并希望减小
	// 最终可执行文件的大小,则应移除下列
	// 不需要的特定初始化例程
	// 更改用于存储设置的注册表项
	// TODO: 应适当修改该字符串,
	// 例如修改为公司或组织名
	SetRegistryKey(_T("应用程序向导生成的本地应用程序"));

	COpenCV_Test_01Dlg dlg;
	m_pMainWnd = &dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: 在此放置处理何时用
		//  “确定”来关闭对话框的代码
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: 在此放置处理何时用
		//  “取消”来关闭对话框的代码
	}
	else if (nResponse == -1)
	{
		TRACE(traceAppMsg, 0, "警告: 对话框创建失败,应用程序将意外终止。\n");
		TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件,则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n");
	}

	// 删除上面创建的 shell 管理器。
	if (pShellManager != NULL)
	{
		delete pShellManager;
	}

#ifndef _AFXDLL
	ControlBarCleanUp();
#endif

	// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
	//  而不是启动应用程序的消息泵。
	return FALSE;
}

 Dialog_Test01Dlg

#include "stdafx.h"
#include "Dialog_Test01.h"
#include "Dialog_Test01Dlg.h"
#include "afxdialogex.h"
#include "Resource.h" // 包含资源定义
#include <iomanip>
#include <time.h>
#include <signal.h>
#pragma warning( disable : 4996 )
#include <opencv2/opencv.hpp>
#include <libfreenect2/libfreenect2.hpp>
#include <libfreenect2/frame_listener_impl.h>
#include <libfreenect2/registration.h>
#include <libfreenect2/packet_pipeline.h>
#include <libfreenect2/logger.h>
#include <mutex>
#include <condition_variable>

using namespace std;
using namespace cv;
using namespace libfreenect2;

enum
{
	Processor_cl,
	Processor_gl,
	Processor_cpu
};

libfreenect2::Freenect2 freenect2;
bool protonect_shutdown = false; // 是否应该关闭运行中的应用。
bool save_images = false;        // 表示何时保存图像的标志。
bool frames_ready = false;       // 表示来自两个相机的帧已准备好的标志。

std::mutex frames_mutex;
std::condition_variable frames_cv;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

std::vector<libfreenect2::Freenect2Device*> devices;
std::vector<libfreenect2::PacketPipeline*> pipelines;
std::vector<libfreenect2::SyncMultiFrameListener*> listeners;
std::vector<libfreenect2::FrameMap> frames;

std::string save_directory = "C:\\ImgSave\\";

void sigint_handler(int s)
{
	protonect_shutdown = true;
}

std::string getCurrentTimeString() {
	time_t now = time(0);
	tm *ltm = localtime(&now);
	char buffer[80];
	strftime(buffer, sizeof(buffer), "%Y%m%d_%H%M%S", ltm);
	return std::string(buffer);
}

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

	// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()

// CDialog_Test01Dlg 对话框

CDialog_Test01Dlg::CDialog_Test01Dlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_DIALOG_TEST01_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDialog_Test01Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CDialog_Test01Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CDialog_Test01Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CDialog_Test01Dlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CDialog_Test01Dlg::OnBnClickedButton3)
	ON_WM_CLOSE()
END_MESSAGE_MAP()

// CDialog_Test01Dlg 消息处理程序

BOOL CDialog_Test01Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);      // 设置大图标
	SetIcon(m_hIcon, FALSE);     // 设置小图标

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CDialog_Test01Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

void CDialog_Test01Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

HCURSOR CDialog_Test01Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CDialog_Test01Dlg::OnBnClickedButton1()
{
	for (int i = 0; i < 2; ++i)  // 假设你有2个相机
	{
		string serial = freenect2.getDeviceSerialNumber(i);
		libfreenect2::PacketPipeline* pipeline = nullptr;
		int depthProcessor = Processor_cl;
		if (depthProcessor == Processor_cpu)
		{
			pipeline = new libfreenect2::CpuPacketPipeline();
		}
		else if (depthProcessor == Processor_gl) // if support gl
		{
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
			pipeline = new libfreenect2::OpenGLPacketPipeline();
#else
			std::cout << "OpenGL pipeline is not supported!" << std::endl;
#endif
		}
		else if (depthProcessor == Processor_cl) // if support cl
		{
			pipeline = new libfreenect2::OpenGLPacketPipeline();
		}

		if (pipeline)
		{
			// 尝试打开设备
			libfreenect2::Freenect2Device* dev = freenect2.openDevice(serial, pipeline);
			if (dev == nullptr)
			{
				if (i == 0)
				{
					MessageBox(L"相机1打开失败");  // 相机1打开失败
				}
				else if (i == 1)
				{
					MessageBox(L"相机2打开失败");  // 相机2打开失败
				}
				return;  // 如果相机打开失败则返回
			}

			// 成功打开设备时,继续正常流程
			devices.push_back(dev);
			pipelines.push_back(pipeline);

			libfreenect2::SyncMultiFrameListener* listener = new libfreenect2::SyncMultiFrameListener(
				libfreenect2::Frame::Color |
				libfreenect2::Frame::Depth |
				libfreenect2::Frame::Ir);
			listeners.push_back(listener);
			frames.push_back(libfreenect2::FrameMap());
			dev->setColorFrameListener(listener);
			dev->setIrAndDepthFrameListener(listener);
			dev->start();
		}
	}

	signal(SIGINT, sigint_handler);
	protonect_shutdown = false;

	// 在 MFC 窗口中显示视频
	while (!protonect_shutdown)
	{
		for (int i = 0; i < devices.size(); ++i)
		{
			listeners[i]->waitForNewFrame(frames[i]);

			libfreenect2::Frame* rgb = frames[i][libfreenect2::Frame::Color];

			// 将 Kinect 的帧转换为 OpenCV 格式
			Mat rgbMat(Size(static_cast<int>(rgb->width), static_cast<int>(rgb->height)), CV_8UC4, rgb->data);

			// 获取 MFC 控件的指针
			CWnd* pWnd = GetDlgItem(IDC_VIDEO1);
			if (pWnd != nullptr) // 验证控件是否存在
			{
				// 获取控件的尺寸
				CRect rect;
				pWnd->GetWindowRect(&rect);
				ScreenToClient(&rect); // 将屏幕坐标转换为客户区坐标

				int width = rect.Width();
				int height = rect.Height();

				// 调整图像大小以匹配控件
				Mat resizedImage;
				resize(rgbMat, resizedImage, Size(width, height), INTER_LINEAR);

				// 在 OpenCV 窗口中显示图像
				// imshow("Camera View", resizedImage);

				// 在 MFC 窗口中显示图像
				CStatic* pImageStatic = (CStatic*)pWnd;
				CDC* pDC = pImageStatic->GetDC();
				BITMAPINFO bmi;
				ZeroMemory(&bmi, sizeof(BITMAPINFO));
				bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
				bmi.bmiHeader.biWidth = resizedImage.cols;
				bmi.bmiHeader.biHeight = -resizedImage.rows;
				bmi.bmiHeader.biPlanes = 1;
				bmi.bmiHeader.biBitCount = 24;
				bmi.bmiHeader.biCompression = BI_RGB;

				// 创建 DIBSection
				HBITMAP hBitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&resizedImage.data, NULL, 0);

				// 将 DIBSection 选入设备上下文
				HBITMAP hOldBitmap = (HBITMAP)SelectObject(pDC->GetSafeHdc(), hBitmap);

				// 绘制图像
				pDC->BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);

				// 恢复旧的位图
				SelectObject(pDC->GetSafeHdc(), hOldBitmap);
				DeleteObject(hBitmap);

				pImageStatic->ReleaseDC(pDC);
			}

			listeners[i]->release(frames[i]);
		}
	}

	// 关闭设备
	for (auto dev : devices)
	{
		dev->stop();
		dev->close();
	}

	// 清理资源
	for (auto pipeline : pipelines)
	{
		delete pipeline;
	}
	for (auto listener : listeners)
	{
		delete listener;
	}

	destroyAllWindows();
}

 Dialog_Test01


// Dialog_Test01.cpp : 定义应用程序的类行为。
//

#include "stdafx.h"
#include "Dialog_Test01.h"
#include "Dialog_Test01Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDialog_Test01App

BEGIN_MESSAGE_MAP(CDialog_Test01App, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CDialog_Test01App 构造

CDialog_Test01App::CDialog_Test01App()
{
	// 支持重新启动管理器
	m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

	// TODO: 在此处添加构造代码,
	// 将所有重要的初始化放置在 InitInstance 中
}


// 唯一的一个 CDialog_Test01App 对象

CDialog_Test01App theApp;


// CDialog_Test01App 初始化

BOOL CDialog_Test01App::InitInstance()
{
	// 如果一个运行在 Windows XP 上的应用程序清单指定要
	// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
	//则需要 InitCommonControlsEx()。  否则,将无法创建窗口。
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// 将它设置为包括所有要在应用程序中使用的
	// 公共控件类。
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();


	AfxEnableControlContainer();

	// 创建 shell 管理器,以防对话框包含
	// 任何 shell 树视图控件或 shell 列表视图控件。
	CShellManager *pShellManager = new CShellManager;

	// 激活“Windows Native”视觉管理器,以便在 MFC 控件中启用主题
	CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

	// 标准初始化
	// 如果未使用这些功能并希望减小
	// 最终可执行文件的大小,则应移除下列
	// 不需要的特定初始化例程
	// 更改用于存储设置的注册表项
	// TODO: 应适当修改该字符串,
	// 例如修改为公司或组织名
	SetRegistryKey(_T("应用程序向导生成的本地应用程序"));

	CDialog_Test01Dlg dlg;
	m_pMainWnd = &dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: 在此放置处理何时用
		//  “确定”来关闭对话框的代码
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: 在此放置处理何时用
		//  “取消”来关闭对话框的代码
	}
	else if (nResponse == -1)
	{
		TRACE(traceAppMsg, 0, "警告: 对话框创建失败,应用程序将意外终止。\n");
		TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件,则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n");
	}

	// 删除上面创建的 shell 管理器。
	if (pShellManager != NULL)
	{
		delete pShellManager;
	}

	// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
	//  而不是启动应用程序的消息泵。
	return FALSE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值