欢迎界面,主程序界面,子界面切换,自适应大小
// DialogExAutosize.cpp : 实现文件
// 编者: 贺昌锋
// 最后日期:2019-04-04
// 目的: VC++, MFC, WINDOWS的基本功能
// 功能:窗口放缩,则其内控件自动随之放缩
#include “stdafx.h”
#include “DialogExtAutosize.h”
#include “afxdialogex.h”
// CDialogExtAutosize 对话框
IMPLEMENT_DYNAMIC(CDialogExtAutosize, CDialogEx)
- CDialogExtAutosize::CDialogExtAutosize(CWnd* pParent / =NULL/)
-
CDialogEx(IDD, pParent)
{
}
CDialogExtAutosize::CDialogExtAutosize(UINT IDD, CWnd* pParent) : CDialogEx(IDD, pParent){
};
CDialogExtAutosize::~CDialogExtAutosize()
{
}
BEGIN_MESSAGE_MAP(CDialogExtAutosize, CDialogEx)
ON_WM_SIZE()
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CDialogExtAutosize 消息处理程序
void CDialogExtAutosize::Get_Control_Original_Proportion()
{
// 保存窗口内所有控件的原始位置和大小数据
HWND hwndChild = ::GetWindow(this->GetSafeHwnd(), GW_CHILD);
// 遍历窗口内所有控件
while (hwndChild)
{
CRect rect; //获取控件大小
CRectOfControl m_CtrolRect;
CWnd* pWnd = FromHandle(hwndChild); // 获取句柄
pWnd->GetWindowRect(&rect);
// 坐标转换
ScreenToClient(&rect); //将控件大小转换为在对话框中的区域坐标
m_CtrolRect.nId = ::GetDlgCtrlID(hwndChild); //获得控件的ID;
m_CtrolRect.rect = rect;
m_CtrolRect.height = rect.right - rect.left;
m_CtrolRect.height = rect.bottom - rect.top;
// 保存控件的位置和大小数据
m_ListRectOfControl.AddTail(m_CtrolRect);
hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
}
}
// 窗体大小变化
void CDialogExtAutosize::ReSize(int cx, int cy){
float fRatioX, fRatioY;
CRect m_rectWndNew;
//获取对话框的当前大小
GetClientRect(&m_rectWndNew);
if ((m_rectWndOld.right - m_rectWndOld.left) <= 0) return;
if ((m_rectWndOld.bottom - m_rectWndOld.top) <= 0) return;
// 计算水平和纵向放大比例系数
fRatioX = (float)(m_rectWndNew.right - m_rectWndNew.left) / (float)(m_rectWndOld.right - m_rectWndOld.left);
fRatioY = (float)(m_rectWndNew.bottom - m_rectWndNew.top) / (float)(m_rectWndOld.bottom - m_rectWndOld.top);
CRect rect;
POSITION pos = m_ListRectOfControl.GetHeadPosition();
HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD);
//遍历窗口上的所有控件
for (int i = 0; i < m_ListRectOfControl.GetCount(); i++)
{
CRectOfControl m_control = (CRectOfControl)m_ListRectOfControl.GetNext(pos);
// 更改控件的位置和大小
rect.left = (long)(fRatioX * (float)m_control.rect.left);
rect.right = (long)(fRatioX * (float)m_control.rect.right);
rect.top = (long)(fRatioY * (float)m_control.rect.top);
rect.bottom = (long)(fRatioY * (float)m_control.rect.bottom);
GetDlgItem(m_control.nId)->MoveWindow(rect, TRUE);
hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
}
}
void CDialogExtAutosize::OnSize(UINT nType, int cx, int cy)
{ //判断窗口是不是最小化了,因为窗口最小化之后 ,窗口的长和宽会变成0,当前一次变化的时就会出现除以0的错误操作
if (nType != SIZE_MINIMIZED)
{
ReSize(cx, cy);
}
CDialogEx::OnSize(nType, cx, cy);
}
BOOL CDialogExtAutosize::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 原始窗口大小
GetClientRect(&m_rectWndOld);
// 保存窗口内所有控件的原始位置和大小数据
Get_Control_Original_Proportion();
return TRUE;
}
void CDialogExtAutosize::OnClose()
{
// 清理内存,回收内存
m_ListRectOfControl.RemoveAll();
CDialogEx::OnClose();
}
// WelComeDlg.cpp : 实现文件//
#include “stdafx.h”
#include “WellComeDlg.h”
#include “afxdialogex.h”
void CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nTimerid, DWORD dwTime)
{
KillTimer(hWnd, nTimerid);
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
CWellComeDlg::CWellComeDlg() : CDialogEx(IDD_WELCOME_DIALOG)
{
}
void CWellComeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CWellComeDlg, CDialogEx)
ON_WM_CTLCOLOR()
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
// ON_WM_CLOSE()
// ON_WM_DESTROY()
END_MESSAGE_MAP()
BOOL CWellComeDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetTimer((UINT)m_hWnd, (UINT)1, NULL);
return TRUE; // return TRUE; 异常: 返回 FALSE
}
void CWellComeDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
dc.SetTextColor(0xff0000);
// 使图标在工作区矩形中居中
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();
}
}
void CWellComeDlg::OnTimer(UINT_PTR nIDEvent)
{
CRect rect; // 定义一个矩形区域变量
GetClientRect(rect);
int nWidth = rect.Width();
int nHeight = rect.Height();
// 定义设备上下文
CDC memDC, memBitmapDC;
// 定义一个内存显示设备对象
BITMAP bitmap;
HBITMAP hBitmap;
CBitmap memBitmap; // 定义一个位图对象
pDC = GetDC();
if (pDC){
//建立与屏幕显示兼容的内存显示设备
memDC.CreateCompatibleDC(pDC);
if (!memDC){
return;
}
memBitmapDC.CreateCompatibleDC(pDC);
if (!memBitmapDC){
memDC.DeleteDC();
return;
}
//建立一个与屏幕显示兼容的位图,位图的大小可选用窗口客户区的大小
if (!memBitmap.CreateCompatibleBitmap(pDC, nWidth, nHeight)){
memDC.DeleteDC();
memBitmapDC.DeleteDC();
return;
}
//将位图选入到内存显示设备中,只有选入了位图的内存显示设备才有地方绘图,画到指定的位图上
CBitmap* pOldBitmap = memDC.SelectObject(&memBitmap);
//memBitmap.Detach(); // 此句这里不能用,否则内存泄漏
//先用背景色将位图清除干净,否则是黑色。这里用的是白色作为背景
memDC.FillSolidRect(0, 0, nWidth, nHeight, RGB(255, 255, 255));
memBitmap.DeleteObject();
//显示图片函数LoadImage 根据位图(或图片)的全路径名TotalName加载图片
hBitmap = (HBITMAP)LoadImage(NULL, TEXT("res\\PH02740U.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_CREATEDIBSECTION);
memBitmap.Attach(hBitmap); // 让位图对象mBitmap 和我们加载的位图相关联(加载进来的位图只要提供一个位图句柄就可以)
//定义一个位图结构体,将图片信息保存在位图结构体中
memBitmap.GetBitmap(&bitmap);
memBitmapDC.SelectObject(&memBitmap); //将位图选入临时内存设备环境
memBitmap.DeleteObject();
memDC.BitBlt(100, 100, bitmap.bmWidth, bitmap.bmHeight, &memBitmapDC, 0, 0, SRCCOPY);
memBitmapDC.DeleteDC();
CFont fontTextSizeExample;
fontTextSizeExample.CreateFont(
80, 0, 700, 0, FW_BOLD, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("Arial"));
memDC.SetBkMode(TRANSPARENT);
memDC.SetTextColor(RGB(255, 0, 0)); //设置字体颜色
memDC.SelectObject(&fontTextSizeExample);
memDC.TextOut(100, y, TEXT("西安威盛电子有限公司"));
fontTextSizeExample.DeleteObject();
fontTextSizeExample.DeleteTempMap();
CString s;
s.Format(TEXT(" %d, %d"), times, y);
memDC.TextOut(600, 400,s);
memDC.MoveTo(y, nHeight);
memDC.LineTo(nWidth - y , nHeight - y );
y++;
//将内存中的图拷贝到屏幕上进行显示
pDC->BitBlt(0, 0, nWidth, nHeight, &memDC, 0, 0, SRCCOPY);
memDC.DeleteDC();
if (y > 1000)
y = 1;
}
ReleaseDC( pDC );
times += 1;
if (times > 990){
SetTimer((UINT)m_hWnd, (UINT)6, TimerProc);
}
CDialogEx::OnTimer(nIDEvent);
}
void CWellComeDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
::SendMessage(m_hWnd, WM_CLOSE, 0, 0);
times = 9999;
CDialogEx::OnLButtonDown(nFlags, point);
}
/// “CommFunction.cpp”
#include “stdafx.h”
#include “afxcmn.h”
#include “CommFunction.h”
void SetCurrentDirectorySimple()
{
CString strPath;
GetModuleFileName(NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH);
strPath.ReleaseBuffer();
int pos;
pos = strPath.ReverseFind('\\');
strPath = strPath.Left(pos);
SetCurrentDirectory(strPath);
}
void trans_Unicode_To_Ansi(CString &strData, CString &strDest){
// 输入必须是Unicode,否则,输出编码会出错
if (!strData) return;
//int nLen = WideCharToMultiByte(CP_ACP, 0, strData.GetBuffer(), -1, NULL, 0, NULL, NULL);
int nLen = WideCharToMultiByte(NULL, 0, strData.GetBuffer(), -1, NULL, 0, NULL, NULL);
strDest.GetBuffer(nLen + 1);
LPSTR lpwstr = (LPSTR)(LPCTSTR)strDest.GetBuffer();
LPCWCH lpcch = (LPCWCH)(LPCTSTR)strData.GetBuffer();
WideCharToMultiByte(NULL, 0, lpcch, -1, lpwstr, nLen, NULL, FALSE);
//lpwstr[nLen] = '\0';
}
void UnicodeToAnsiSimple(CString &strData){
CString strDest;
trans_Unicode_To_Ansi(strData, strDest);
strData = strDest;
}
void ExecuteApp000(CString strParaGraph, CString strKeyName){
CString strPath, strIniFile, strAppFile;
GetModuleFileName(NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH);
strPath.ReleaseBuffer();
int pos;
pos = strPath.ReverseFind('\\');
strIniFile = strPath.Left(pos) + _T("\\SystemConfig.ini");
//AfxMessageBox(strParaGraph + _T(" ") + strKeyName + _T(" ") + strIniFile + _T(" ") + strAppFile);
SetCurrentDirectorySimple();
GetPrivateProfileString(strParaGraph.GetBuffer(), strKeyName.GetBuffer(), CString("NOTEPAD.EXE"), strAppFile.GetBuffer(MAX_PATH), MAX_PATH, strIniFile.GetBuffer());
UnicodeToAnsiSimple(strAppFile);
WinExec((LPCSTR)strAppFile.GetString(), SW_SHOW);
}
// DialogExtAutosize.h
#pragma once
// CDialogExtAutosize 对话框
// 保存控件的大小,位置
class CRectOfControl
{
public:
int nId;
int width;
int height;
CRect rect;
};
class CDialogExtAutosize : public CDialogEx
{
DECLARE_DYNAMIC(CDialogExtAutosize)
public:
CDialogExtAutosize(CWnd* pParent = NULL); // 标准构造函数
CDialogExtAutosize(UINT IDD, CWnd* pParent = NULL); // 构造函数
virtual ~CDialogExtAutosize();
// 对话框资源ID(假的)
enum { IDD = 0 };
protected:
CRect m_rectWndOld;
void ChangeSize(UINT nID, int x, int y);
void ReSize(int cx, int cy);
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnSize(UINT nType, int cx, int cy);//窗口自适应大小
virtual BOOL OnInitDialog();
public:
// 保存控件的位置等数据
CList<CRectOfControl, CRectOfControl> m_ListRectOfControl;
void Get_Control_Original_Proportion();
afx_msg void OnClose();
};
// CCommFunction.h
#pragma once
#include “stdafx.h”
#include “targetver.h”
#include “afxdialogex.h”
void SetCurrentDirectorySimple();
void UnicodeToAnsiSimple(CString &strData);
void trans_Unicode_To_Ansi(CString &strData, CString &strDest);
// 本段注释作者:贺昌锋// 注释开始
// 类CPngFile objectPngFile编写者: 贺昌锋,最后日期:2019-02-26
// 本类与以下类有关:CPngFile objectPngFile,
// 在initPanelControls()中调用objectPngFile.initControls(), 在OnPaint()中调用objectPngFile.rePaint(),
// afx_msg BOOL OnToolTipNotify(UINT id, NMHDR *pNMHDR, LRESULT *pResult);
// 与以下有关:
// afx_msg BOOL OnToolTipNotify(UINT id, NMHDR pNMHDR, LRESULT pResult);
// virtual BOOL PreTranslateMessage(MSG pMsg);
// 在PreTranslateMessage(MSG pMsg)中:
// objectPngFile.nIndex = 1;
// objectPngFile.setControlsVisible();
// 注释结束
// MFCAppMenuDlg.h : 头文件//
#pragma once
#include “afxcmn.h”
#include “PngFile.h”
#include “afxwin.h”
#include “StaticExt.h”
#include “DialogExtAutosize.h”
#include “ViewExt.h”
// COilWellDlg 对话框
class COilWellDlg : public CDialogExtAutosize
{
// 构造
public:
COilWellDlg(CWnd* pParent = NULL); // 标准构造函数
CPngFile objectPngFile; // 由贺添加
void initPanelControls(); // 由贺添加
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DIALOG_MAIN };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
protected:
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnClickedMfcbuttonOk();
afx_msg void OnBnClickedCancel();
afx_msg void OnClose();
afx_msg BOOL OnToolTipNotify(UINT id, NMHDR *pNMHDR, LRESULT *pResult); // 由贺添加
virtual BOOL PreTranslateMessage(MSG* pMsg); // 由贺添加
BOOL cRestart = FALSE;
//CMenu cMainMenu;
CStaticExt m_PictureControl1;
CStaticExt m_PictureControl2;
CStaticExt m_PictureControl3;
CStaticExt m_PictureControl4;
CStaticExt m_PictureControl5;
CStaticExt m_PictureControl6;
CStaticExt m_PictureControl7;
CStaticExt m_PictureControl8;
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
public:
void InitPanelControls();
afx_msg void OnStnClickedPngControlpanel1();
afx_msg void OnStnClickedPngControlpanel2();
afx_msg void OnStnClickedPngControlpanel3();
afx_msg void OnStnClickedPngControlpanel4();
afx_msg void OnStnClickedPngControlpanel5();
afx_msg void OnStnClickedPngControlpanel6();
afx_msg void OnStnClickedPngControlpanel7();
afx_msg void OnStnClickedPngControlpanel8();
CStatic *m_pStatic_Selected;
void DrawRectangleBox();
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
void ExecuteApp(CString strParaGraph, CString strKeyName );
};
// CPngFile.cpp : 实现文件
#include “stdafx.h”
#include “PngFile.h”
#include “resource.h”
#include “StaticExt.h”
#include “ViewExt.h”
#pragma comment(lib, “GdiPlus.lib”)
using namespace Gdiplus;
// CPngFile
IMPLEMENT_DYNAMIC(CPngFile, CPngFile)
CPngFile::CPngFile()
{
initControls();
}
CPngFile::CPngFile(CWnd* pParentWnd)
{
this->pParentWnd = pParentWnd;
initControls(); // 参数定义
}
void CPngFile::initControls()
{
// list of all controls
arrayControlsID[0] = IDB_PNG_ControlPanel1;
arrayControlsID[1] = IDB_PNG_ControlPanel2;
arrayControlsID[2] = IDB_PNG_ControlPanel3;
arrayControlsID[3] = IDB_PNG_ControlPanel4;
arrayControlsID[4] = IDB_PNG_ControlPanel5;
arrayControlsID[5] = IDB_PNG_ControlPanel6;
arrayControlsID[6] = IDB_PNG_ControlPanel7;
arrayControlsID[7] = IDB_PNG_ControlPanel8;
// BackGround Picture
arrayPngFileNames[0] = TEXT("MainBackGround_Original.PNG");
arrayPngFileNames[1] = TEXT("MainBackGround_Original11.PNG");
arrayPngFileNames[2] = TEXT("MainBackGround_Original22.PNG");
// Tip Text
arrayItemsComment[0] = TEXT("Do Control Panel");
arrayItemsComment[1] = TEXT("Do Service Edit");
arrayItemsComment[2] = TEXT("Do Calibration"); // Recalculation
arrayItemsComment[3] = TEXT("Do Acqusition");
arrayItemsComment[4] = TEXT("Do Editor");
arrayItemsComment[5] = TEXT("Do Interactive Plot");
arrayItemsComment[6] = TEXT("Do Data Management");
arrayItemsComment[7] = TEXT("");
arrayItemsComment[8] = TEXT("");
arrayItemsComment[9] = TEXT("Do Format Editor");
arrayItemsComment[10] = TEXT("Do Heading Editor");
arrayItemsComment[11] = TEXT("Do Plot Job Editor");
arrayItemsComment[12] = TEXT("Do ToolEditor");
arrayItemsComment[13] = TEXT("Do Service Edit");
arrayItemsComment[14] = TEXT("");
arrayItemsComment[15] = TEXT("Do Back");
arrayItemsComment[16] = TEXT("");
arrayItemsComment[17] = TEXT("Do Merge");
arrayItemsComment[18] = TEXT("Do Recalculation");
arrayItemsComment[19] = TEXT("Do Backup & Restore");
arrayItemsComment[20] = TEXT("Do Simulator");
arrayItemsComment[21] = TEXT("");
arrayItemsComment[22] = TEXT("");
arrayItemsComment[23] = TEXT("Do Back");
arrayItemsComment[24] = TEXT("");
arrayItemsComment[25] = TEXT("");
arrayItemsComment[26] = TEXT("");
arrayItemsComment[27] = TEXT("");
// Command String
arrayObjecsCommand[0] = TEXT("ControlPanel");
arrayObjecsCommand[1] = TEXT("ServiceEditor");
arrayObjecsCommand[2] = TEXT("Calibration");
arrayObjecsCommand[3] = TEXT("Acquisition");
arrayObjecsCommand[4] = TEXT("1"); // Editor
arrayObjecsCommand[5] = TEXT("InteractivePlot");
arrayObjecsCommand[6] = TEXT("2"); // Data Management
arrayObjecsCommand[7] = TEXT("none");
arrayObjecsCommand[8] = TEXT("none");
arrayObjecsCommand[9] = TEXT("FormatEditor");
arrayObjecsCommand[10] = TEXT("HeadingEditor");
arrayObjecsCommand[11] = TEXT("PlotJobEditor");
arrayObjecsCommand[12] = TEXT("ToolEditor");
arrayObjecsCommand[13] = TEXT("ServiceEdit");
arrayObjecsCommand[14] = TEXT("none");
arrayObjecsCommand[15] = TEXT("0"); // back
arrayObjecsCommand[16] = TEXT("Merge");
arrayObjecsCommand[17] = TEXT("Recalculation");
arrayObjecsCommand[18] = TEXT("BackupRestore");
arrayObjecsCommand[19] = TEXT("Simulator");
arrayObjecsCommand[20] = TEXT("none");
arrayObjecsCommand[21] = TEXT("none");
arrayObjecsCommand[22] = TEXT("none");
arrayObjecsCommand[23] = TEXT("0"); // back
///////////////// 3个界面,对应3组配置 ///////////////////////
arrayItemsVisible[0] = 1; // >0 表示visible,=0表示invisible
arrayItemsVisible[1] = 2;
arrayItemsVisible[2] = 3;
arrayItemsVisible[3] = 4;
arrayItemsVisible[4] = 5;
arrayItemsVisible[5] = 6;
arrayItemsVisible[6] = 7;
arrayItemsVisible[7] = -1;
arrayItemsVisible[8] = -1;
arrayItemsVisible[9] = 0;
arrayItemsVisible[10] = 0;
arrayItemsVisible[11] = 0;
arrayItemsVisible[12] = 0;
arrayItemsVisible[13] = 0;
arrayItemsVisible[14] = -1;
arrayItemsVisible[15] = +1;
arrayItemsVisible[16] = -1;
arrayItemsVisible[17] = 0;
arrayItemsVisible[18] = 0;
arrayItemsVisible[19] = 0;
arrayItemsVisible[20] = 0;
arrayItemsVisible[21] = -1;
arrayItemsVisible[22] = -1;
arrayItemsVisible[23] = +1;
}
bool CPngFile::bExecuteShellCommand(int n){
if (arrayObjecsCommand[m_nIndexOfPngARRAYSIZE + n].GetLength() < 4){
m_nIndexOfPng = _ttoi(arrayObjecsCommand[m_nIndexOfPngARRAYSIZE + n]);
return false;
}
CString SName;
int MAX_PATHLENGTH = 255;
CString des;
::GetCurrentDirectory(MAX_PATHLENGTH, des.GetBuffer(MAX_PATHLENGTH));
des.ReleaseBuffer();
des += TEXT("\\SystemConfig.ini");
GetPrivateProfileString(TEXT("ExeFilePath"), arrayObjecsCommand[m_nIndexOfPng*ARRAYSIZE + n], TEXT("TestApplication.exe"), SName.GetBuffer(MAX_PATHLENGTH), MAX_PATHLENGTH, des);
ShellExecute(NULL, TEXT("open"), SName, TEXT(""), NULL, SW_SHOWNORMAL);
return true;
}
void CPngFile::setControlsVisible(){
int j = m_nIndexOfPng * ARRAYSIZE;
for (int i = 0; i < ARRAYSIZE; i++){
CStaticExt* pPictureControl = (CStaticExt*)pParentWnd->GetDlgItem(arrayControlsID[i]);
pPictureControl->m_nIndexOfCurrent = i;
pParentWnd->SetRedraw(false);
if (arrayItemsVisible[j + i] >0)
pPictureControl->ShowWindow(true);
if (arrayItemsVisible[j + i] <0)
pPictureControl->ShowWindow(false);
pPictureControl->m_pStrToolTip = &arrayItemsComment[j + i];
}
pParentWnd->SetRedraw(true);
rePaint();
}
void CPngFile::rePaint(){
int i = 0, j = m_nIndexOfPng * 8;
for (i = 0; i < 8; i++){
CStaticExt* pPictureControl = (CStaticExt*)pParentWnd->GetDlgItem(arrayControlsID[i]);
}
readPngFile(pParentWnd, TEXT("res\\") + arrayPngFileNames[m_nIndexOfPng]);
}
void CPngFile::readPngFile(CWnd* pPictureControl, CString strFileName){
// filename 是要加载的文件名
CDC* pDC;
RECT rect;
CImage image;
if (!PathFileExists(strFileName)){
AfxMessageBox(strFileName + TEXT(" File Not Found") );
return;
}
pDC = pPictureControl->GetDC();
pPictureControl->GetClientRect(&rect);
HRESULT ret = image.Load(strFileName);
if (image.IsNull()) {
AfxMessageBox(_T("没有加载成功"));
return;
}
if (image.GetBPP() == 32) //确认该图像包含Alpha通道
{
int i;
int j;
for (i = 0; i < image.GetWidth(); i++)
{
for (j = 0; j < image.GetHeight(); j++)
{
byte *pByte = (byte *)image.GetPixelAddress(i, j);
pByte[0] = pByte[0] * pByte[3] / 255;
pByte[1] = pByte[1] * pByte[3] / 255;
pByte[2] = pByte[2] * pByte[3] / 255;
}
}
}
pDC->SetStretchBltMode(HALFTONE);
image.Draw(pDC->GetSafeHdc(), 0, 0, rect.right - rect.left, rect.bottom - rect.top);
}
void CPngFile::displayItemLabel(CWnd* pPictureControl, CString strText){
CDC* pDC;
RECT rect;
pDC = pPictureControl->GetDC();
pPictureControl->GetClientRect(&rect);
drawText_AlignCenter_Vertical(pDC, 0, rect.bottom - rect.top - 10, rect.right - rect.left, 40, strText);
}
void CPngFile::drawText_AlignCenter_Vertical(CDC* pDC, int x, int y, int width, int height, CString strText) {
CPoint point;
point = pDC->GetTextExtent(strText);
pDC->TextOut(x + (width - point.x) / 2, y - (height - point.y) / 2, strText.GetString());
}
void CPngFile::testView( ){
CDC* pDC;
RECT rect;
pDC = pParentWnd->GetDC();
pParentWnd->GetClientRect(&rect);
rect.bottom = 100;
rect.right = 100;
CRuntimeClass* pViewRuntimeClass = RUNTIME_CLASS(CViewExt);
CView *viewExt = (CViewExt*)pViewRuntimeClass->CreateObject();
//viewExt->Create(NULL, NULL, WS_VISIBLE | WS_CHILD, rect, pParentWnd, 123, NULL);
LONG style = 0;
CStatic* pPictureControl = (CStatic*)pParentWnd->GetDlgItem(IDB_PNG_ControlPanel1);
style = GetWindowLong(pPictureControl->GetSafeHwnd(), GWL_STYLE);
style |= WS_BORDER | SS_GRAYFRAME ; // SS_BLACKFRAME SS_WHITEFRAME SS_GRAYFRAME
SetWindowLong(pPictureControl->GetSafeHwnd(), GWL_STYLE, style);
pPictureControl->ModifyStyle(SS_WHITEFRAME | WS_BORDER, 0);//去掉边框
}
#pragma once
#include “afxcmn.h”
#include “atlimage.h”
#include “resource.h”
int CONST ARRAYSIZE = 8 ;
// CSensorDlg 对话框
class CPngFile
{
DECLARE_DYNAMIC(CPngFile);
public:
CPngFile();
CPngFile(CWnd *pParentWnd);
CImage img;
CWnd* pParentWnd;
int m_nIndexOfPng = 0 ;
CString arrayPngFileNames[3];
int arrayControlsID[8];
int arrayItemsVisible[24];
CString arrayObjecsCommand[24];
CString arrayItemsComment[30];
void initControls();
void rePaint();
void setControlsVisible();
void modifyControlAttributes();
void displayItemLabel(CWnd* pPictureControl, CString strText);
bool bExecuteShellCommand(int n);
void drawText_AlignCenter_Vertical(CDC* pDC, int x, int y, int width, int height, CString strText);
void readPngFile(CWnd* pPictureControl, CString strFileName);
void CPngFile::testView();
protected:
private:
};