MFC中如何实现软件启动画面&软件开机自启
1、新建一个dialog
新建一个dlg,取名叫 IDD_DLG_STARTBMP,并设置相关属性,如下
然后添加类,添加 init 属性,ontimer 属性等
2、添加位图
资源视图->Bitmap右键->添加资源->bitmap->导入 然后导入提前设置好的BMP格式图片。
3、代码编写
在 OnInitDialog() 里面,设置计时器
在 OnTimer() 里面设置图片响应时间
在 OnPaint() 里面绘制图像
如下:.cpp文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include "stdafx.h" #include "StartBmp.h" #include "afxdialogex.h" #include "MainFormDlg.h"
IMPLEMENT_DYNAMIC(CStartBmp, CDialogEx)
CStartBmp::CStartBmp(CWnd* pParent ) : CDialogEx(IDD_DLG_STARTBMP, pParent) {
}
CStartBmp::~CStartBmp() { }
void CStartBmp::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); }
BEGIN_MESSAGE_MAP(CStartBmp, CDialogEx) ON_WM_PAINT() ON_WM_TIMER() END_MESSAGE_MAP()
void CStartBmp::OnPaint() { CPaintDC dc(this); CRect rect; GetClientRect(&rect); CDC dcBmp; dcBmp.CreateCompatibleDC(&dc); CBitmap bmpBackground; bmpBackground.LoadBitmap(IDB_START_BITMAP); BITMAP m_bitmap; bmpBackground.GetBitmap(&m_bitmap); CBitmap *pbmpOld = dcBmp.SelectObject(&bmpBackground); dc.SetStretchBltMode(COLORONCOLOR); dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &dcBmp, 0, 0, m_bitmap.bmWidth, m_bitmap.bmHeight, SRCCOPY); dcBmp.SelectObject(pbmpOld);
}
void CStartBmp::OnTimer(UINT_PTR nIDEvent) {
Sleep(2000);
KillTimer(1); OnCancel();
CDialogEx::OnTimer(nIDEvent); }
BOOL CStartBmp::OnInitDialog() { CDialogEx::OnInitDialog();
SetTimer(1, 2000, NULL);
return TRUE; }
|
如下:.h 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #pragma once #include "resource.h"
class CStartBmp : public CDialogEx { DECLARE_DYNAMIC(CStartBmp)
public: CStartBmp(CWnd* pParent = NULL); virtual ~CStartBmp();
#ifdef AFX_DESIGN_TIME enum { IDD = IDD_DLG_STARTBMP }; #endif
protected: virtual void DoDataExchange(CDataExchange* pDX);
DECLARE_MESSAGE_MAP() public: afx_msg void OnPaint(); afx_msg void OnTimer(UINT_PTR nIDEvent); virtual BOOL OnInitDialog(); };
|
4、开软件自启
首先在主函数中实例化我们刚刚写的类
然后在主函数的勒种添加OnCreat事件
在create函数中如下写入
1 2 3 4 5 6 7 8 9 10 11 12
| int CMainFormDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) return -1;
m_StartBmpDlg.DoModal(); OnBnClickedDiagnosisButton(); m_SYSTEM_DIAGNOSIS.ShowWindow(SW_HIDE);
return 0; }
|
大功告成
在前面ontiner中的sleep函数时用来设置开机动画中图片的停留时间的。
关于软件开机自启的设置
方法一:
在每一个开始菜单里面的附件中有一个自启文件夹,将这个文件夹打开,将需要开机自启的软件丢进去即可。
方法二
代码实现,读写注册表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| HKEY hKey; CString strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) { TCHAR strExeFullDir[MAX_PATH]; GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
TCHAR strDir[MAX_PATH] = {}; DWORD nLength = MAX_PATH; long result = RegGetValue(hKey, nullptr, L"GISRestart", RRF_RT_REG_SZ, 0, strDir, &nLength);
if (result != ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) != 0) { RegSetValueEx(hKey, L"GISRestart", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
RegCloseKey(hKey); m_PowerAuto.SetWindowText(_T("已自启")); } else { m_PowerAuto.SetWindowText(_T("已自启")); } } else { MessageBox(_T("打开启动项失败")); }
|
当然也可以在 init 函数中查看是否已被开机自启
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| HKEY hKey;
CString lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_ALL_ACCESS, &hKey); if (lRet != ERROR_SUCCESS) MessageBox(_T("打开启动项失败"));
TCHAR pFileName[MAX_PATH] = {}; GetModuleFileName(NULL, pFileName, MAX_PATH);
TCHAR PowerBoot[MAX_PATH] = {}; DWORD nLongth = MAX_PATH; long result = RegGetValue(hKey, NULL, _T("PowerBoot"), RRF_RT_REG_SZ, 0, PowerBoot, &nLongth); if (result == ERROR_SUCCESS) { m_PowerAuto.SetWindowText(_T("已自启")); } else { m_PowerAuto.SetWindowText(_T("未自启")); }
RegCloseKey(hKey);
|
不积跬步无以至千里,不积小流无以成江河!