CWinAppDlg.cpp
// CWinAppDlg.cpp: 实现文件
//
#include "pch.h"
#include "framework.h"
#include "CWinApp.h"
#include "CWinAppDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CCWinAppDlg 对话框
CCWinAppDlg::CCWinAppDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_CWINAPP_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCWinAppDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CCWinAppDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
END_MESSAGE_MAP()
// CCWinAppDlg 消息处理程序
BOOL CCWinAppDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
SetDlgItemText(IDC_EDIT1, theApp.m_pszAppName);
CString str;
str.Format(L"%d", theApp.m_bHelpMode);
SetDlgItemText(IDC_EDIT2, str);
str.Format(L"%#x", theApp.m_pActiveWnd);
SetDlgItemText(IDC_EDIT3, str);
str.Format(L"%s", theApp.m_pszExeName);
SetDlgItemText(IDC_EDIT4, str);
str.Format(L"%s", theApp.m_pszHelpFilePath);
SetDlgItemText(IDC_EDIT5, str);
str.Format(L"%s", theApp.m_pszProfileName);
SetDlgItemText(IDC_EDIT6, str);
str.Format(L"%s", theApp.m_pszRegistryKey);
SetDlgItemText(IDC_EDIT7, str);
//读取
int left=theApp.WriteProfileInt(L"Position", L"left", 200);
int top =theApp.WriteProfileInt(L"Position", L"top", 200);
int right=theApp.WriteProfileInt(L"Position", L"right", 200);
int bottom=theApp.WriteProfileInt(L"Position", L"bottom", 200);
CRect rect = { left, top, right, bottom };
MoveWindow(&rect);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CCWinAppDlg::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 CCWinAppDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCWinAppDlg::OnDestroy()
{
CDialogEx::OnDestroy();
//获取窗口带下
CRect rect;
GetWindowRect(rect);
// 写入注册表
theApp.WriteProfileInt(L"注册表Position",L"left",rect.left);
theApp.WriteProfileInt(L"注册表Position", L"top", rect.top);
theApp.WriteProfileInt(L"注册表Position", L"right", rect.right);
theApp.WriteProfileInt(L"注册表Position", L"bottom", rect.bottom);
theApp.WriteProfileString(L"Info",L"Title",L"QQ");
}
CWinApp.cpp
// CWinApp.cpp: 定义应用程序的类行为。
//
#include "pch.h"
#include "framework.h"
#include "CWinApp.h"
#include "CWinAppDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CCWinAppApp
BEGIN_MESSAGE_MAP(CCWinAppApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CCWinAppApp 构造
CCWinAppApp::CCWinAppApp()
{
// 支持重新启动管理器
m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}
// 唯一的 CCWinAppApp 对象
CCWinAppApp theApp;
// CCWinAppApp 初始化
BOOL CCWinAppApp::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("应用程序向导生成的本地应用程序"));
CCWinAppDlg 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 != nullptr)
{
delete pShellManager;
}
#if !defined(_AFXDLL) && !defined(_AFX_NO_MFC_CONTROLS_IN_DIALOGS)
ControlBarCleanUp();
#endif
// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
// 而不是启动应用程序的消息泵。
return FALSE;
}