181 lines
4.1 KiB
C++
181 lines
4.1 KiB
C++
|
|
// 图片Dlg.cpp: 实现文件
|
|
//
|
|
|
|
#include "pch.h"
|
|
#include "framework.h"
|
|
#include "图片.h"
|
|
#include "图片Dlg.h"
|
|
#include "afxdialogex.h"
|
|
|
|
#ifdef _DEBUG
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
|
|
// C图片Dlg 对话框
|
|
|
|
|
|
|
|
C图片Dlg::C图片Dlg(CWnd* pParent /*=nullptr*/)
|
|
: CDialogEx(IDD_MY_DIALOG, pParent)
|
|
{
|
|
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
|
}
|
|
|
|
void C图片Dlg::DoDataExchange(CDataExchange* pDX)
|
|
{
|
|
CDialogEx::DoDataExchange(pDX);
|
|
DDX_Control(pDX, IDC_PICTURE, m_Picture);
|
|
}
|
|
|
|
BEGIN_MESSAGE_MAP(C图片Dlg, CDialogEx)
|
|
ON_WM_PAINT()
|
|
ON_WM_QUERYDRAGICON()
|
|
ON_BN_CLICKED(IDC_BUTTON1, &C图片Dlg::OnBnClickedButton1)
|
|
// ON_WM_SIZING()
|
|
//ON_WM_SIZE()
|
|
ON_BN_CLICKED(IDC_BUTTON2, &C图片Dlg::OnBnClickedButton2)
|
|
ON_WM_SIZE()
|
|
ON_BN_CLICKED(IDC_BUTTON3, &C图片Dlg::OnBnClickedButton3)
|
|
END_MESSAGE_MAP()
|
|
|
|
|
|
// C图片Dlg 消息处理程序
|
|
|
|
BOOL C图片Dlg::OnInitDialog()
|
|
{
|
|
CDialogEx::OnInitDialog();
|
|
|
|
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
|
|
// 执行此操作
|
|
SetIcon(m_hIcon, TRUE); // 设置大图标
|
|
SetIcon(m_hIcon, FALSE); // 设置小图标
|
|
|
|
// TODO: 在此添加额外的初始化代码
|
|
cdc = m_Picture.GetDC();
|
|
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
|
|
}
|
|
|
|
// 如果向对话框添加最小化按钮,则需要下面的代码
|
|
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
|
|
// 这将由框架自动完成。
|
|
|
|
void C图片Dlg::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
|
|
{
|
|
showImg();
|
|
CDialogEx::OnPaint();
|
|
}
|
|
}
|
|
|
|
//当用户拖动最小化窗口时系统调用此函数取得光标
|
|
//显示。
|
|
HCURSOR C图片Dlg::OnQueryDragIcon()
|
|
{
|
|
return static_cast<HCURSOR>(m_hIcon);
|
|
}
|
|
|
|
|
|
|
|
void C图片Dlg::OnBnClickedButton1()
|
|
{
|
|
// TODO: 在此添加控件通知处理程序代码
|
|
TCHAR szFilter[] = _T("文本文件(*.jpg)|*.jpg|所有文件(*.*)|*.*||");
|
|
// 构造打开文件对话框
|
|
CFileDialog fileDlg(TRUE, _T("jpg"), NULL, 0, szFilter, this);
|
|
CString strFilePath;
|
|
//显示打开文件对话框
|
|
if (IDOK == fileDlg.DoModal())
|
|
{
|
|
// 如果点击了文件对话框上的“打开”按钮,则将选择的文件路径显示到编辑框里
|
|
strFilePath = fileDlg.GetPathName();
|
|
if (!img.IsNull())
|
|
img.Destroy();
|
|
img.Load(strFilePath);
|
|
showImg();
|
|
}
|
|
else
|
|
{
|
|
MessageBox(_T("用户取消选择"));
|
|
}
|
|
}
|
|
|
|
VOID C图片Dlg::showImg()
|
|
{
|
|
if (!img.IsNull())
|
|
{
|
|
CRect rt;
|
|
m_Picture.GetWindowRect(rt);
|
|
rt.MoveToXY(0,0);
|
|
SetBkMode(cdc->m_hDC, TRANSPARENT);
|
|
img.Draw(cdc->m_hDC, rt);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
void C图片Dlg::OnBnClickedButton2()
|
|
{
|
|
// TODO: 在此添加控件通知处理程序代码
|
|
CDC* pDC;//屏幕DC
|
|
pDC = GetDesktopWindow()->GetDC();//获取当前整个屏幕DC
|
|
int Width = pDC->GetDeviceCaps(HORZRES);
|
|
int Height = pDC->GetDeviceCaps(VERTRES);
|
|
img.Destroy();
|
|
img.Create(Width, Height, 32);
|
|
HDC hdc = img.GetDC();
|
|
SetBkMode(hdc, TRANSPARENT);
|
|
BitBlt(hdc, 0, 0, Width, Height, pDC->m_hDC, 0, 0, SRCCOPY);
|
|
img.ReleaseDC();
|
|
showImg();
|
|
}
|
|
|
|
|
|
void C图片Dlg::OnSize(UINT nType, int cx, int cy)
|
|
{
|
|
CDialogEx::OnSize(nType, cx, cy);
|
|
//showImg();
|
|
// TODO: 在此处添加消息处理程序代码
|
|
}
|
|
|
|
|
|
void C图片Dlg::OnBnClickedButton3()
|
|
{
|
|
// TODO: 在此添加控件通知处理程序代码
|
|
TCHAR szFilter[] = _T("文本文件(*.jpg)|*.jpg|所有文件(*.*)|*.*||");
|
|
// 构造打开文件对话框
|
|
CFileDialog fileDlg(FALSE, _T("jpg"), NULL, 0, szFilter, this);
|
|
CString strFilePath;
|
|
//显示打开文件对话框
|
|
if (IDOK == fileDlg.DoModal())
|
|
{
|
|
// 如果点击了文件对话框上的“打开”按钮,则将选择的文件路径显示到编辑框里
|
|
strFilePath = fileDlg.GetPathName();
|
|
img.Save(strFilePath);
|
|
}
|
|
else
|
|
{
|
|
MessageBox(_T("用户取消选择"));
|
|
}
|
|
}
|