機械学習基礎理論独習

誤りがあればご指摘いただけると幸いです。数式が整うまで少し時間かかります。リンクフリーです。

勉強ログです。リンクフリーです
目次へ戻る

【MFC】Window を貼り付ける Sample Program

手順

1. MFC を選び、プロジェクト名を SampleWindowPaint とし Dialog Base で作成する。
2. Base Class を CWnd にして Class CImageView を作成する。
3. CImageView に CImage m_Image を定義する。
4. CImageView に BOOL LoadImageFile(LPCTSTR path); afx_msg void OnPaint(); を実装する。
5. CSampleWindowPaintDlg に CImageView m_ImageView; を定義する。
6. CSampleWindowPaintDlg に BOOL OnInitDialog();を実装する。
7. 実装完了。お疲れさまでした。

コード

ImageView.h
#pragma once


// CImageView

class CImageView : public CWnd
{
	DECLARE_DYNAMIC(CImageView)

public:
	CImageView();
	virtual ~CImageView();

protected:
	DECLARE_MESSAGE_MAP()
public:
	BOOL LoadImageFile(LPCTSTR path);
protected:
	CImage m_Image;
public:
	afx_msg void OnPaint();
};
ImageView.cpp
// ImageView.cpp : implementation file
//

#include "pch.h"
#include "SampleWindowPaint.h"
#include "ImageView.h"


// CImageView

IMPLEMENT_DYNAMIC(CImageView, CWnd)

CImageView::CImageView()
{

}

CImageView::~CImageView()
{
}


BEGIN_MESSAGE_MAP(CImageView, CWnd)
	ON_WM_PAINT()
END_MESSAGE_MAP()



// CImageView message handlers



BOOL CImageView::LoadImageFile(LPCTSTR path)
{
    // TODO: Add your implementation code here.
    return SUCCEEDED(m_Image.Load(path));
}

void CImageView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CWnd::OnPaint() for painting messages
	CRect rect;
	GetClientRect(&rect);
	m_Image.Draw(dc, rect);
}
SampleWindowPaintDlg.h
// SampleWindowPaintDlg.h : header file
//

#pragma once
#include "ImageView.h"

// CSampleWindowPaintDlg dialog
class CSampleWindowPaintDlg : public CDialogEx
{
// Construction
public:
	CSampleWindowPaintDlg(CWnd* pParent = nullptr);	// standard constructor

// Dialog Data
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_SAMPLEWINDOWPAINT_DIALOG };
#endif

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


// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
	CImageView m_ImageView;
};
SampleWindowPaintDlg.cpp
// SampleWindowPaintDlg.cpp : implementation file
//

#include "pch.h"
#include "framework.h"
#include "SampleWindowPaint.h"
#include "SampleWindowPaintDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

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

// Dialog Data
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

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

// Implementation
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()


// CSampleWindowPaintDlg dialog



CSampleWindowPaintDlg::CSampleWindowPaintDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SAMPLEWINDOWPAINT_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

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

BEGIN_MESSAGE_MAP(CSampleWindowPaintDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()


// CSampleWindowPaintDlg message handlers

BOOL CSampleWindowPaintDlg::OnInitDialog()
{
	CDialogEx::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 != nullptr)
	{
		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);
		}
	}

	// 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

	// 表示領域を定義(座標は適宜調整)
	CRect imageRect(8, 8, 400, 300);
	m_ImageView.Create(
		nullptr,                 // ウィンドウクラス名(PreCreateWindow で設定するなら nullptr)
		nullptr,                 // ウィンドウタイトル(不要なら nullptr)
		WS_CHILD | WS_VISIBLE,  // スタイル
		imageRect,              // 表示領域
		this,                   // 親ウィンドウ
		1234                    // コントロールID
	);


	// 画像読み込み
	m_ImageView.LoadImageFile(_T(".\\sample.jpg"));

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSampleWindowPaintDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::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 CSampleWindowPaintDlg::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();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSampleWindowPaintDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

目次へ戻る