HideProcessDlg.cpp
上传用户:xsxdsb
上传日期:2009-12-14
资源大小:672k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // HideProcessDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "HideProcess.h"
  5. #include "HideProcessDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. typedef DWORD (CALLBACK* LPREGISTERSERVICEPROCESS)(DWORD,DWORD);
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CHideProcessDlg dialog
  51. CHideProcessDlg::CHideProcessDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CHideProcessDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CHideProcessDlg)
  55. m_nSeconds = 60;
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CHideProcessDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CHideProcessDlg)
  64. DDX_Text(pDX, IDC_TIME, m_nSeconds);
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CHideProcessDlg, CDialog)
  68. //{{AFX_MSG_MAP(CHideProcessDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_BN_CLICKED(IDC_HIDE, OnHide)
  73. ON_WM_TIMER()
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CHideProcessDlg message handlers
  78. BOOL CHideProcessDlg::OnInitDialog()
  79. {
  80. CDialog::OnInitDialog();
  81. // Add "About..." menu item to system menu.
  82. // IDM_ABOUTBOX must be in the system command range.
  83. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  84. ASSERT(IDM_ABOUTBOX < 0xF000);
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96. // Set the icon for this dialog.  The framework does this automatically
  97. //  when the application's main window is not a dialog
  98. SetIcon(m_hIcon, TRUE); // Set big icon
  99. SetIcon(m_hIcon, FALSE); // Set small icon
  100. // TODO: Add extra initialization here
  101. return TRUE;  // return TRUE  unless you set the focus to a control
  102. }
  103. void CHideProcessDlg::OnSysCommand(UINT nID, LPARAM lParam)
  104. {
  105. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  106. {
  107. CAboutDlg dlgAbout;
  108. dlgAbout.DoModal();
  109. }
  110. else
  111. {
  112. CDialog::OnSysCommand(nID, lParam);
  113. }
  114. }
  115. // If you add a minimize button to your dialog, you will need the code below
  116. //  to draw the icon.  For MFC applications using the document/view model,
  117. //  this is automatically done for you by the framework.
  118. void CHideProcessDlg::OnPaint() 
  119. {
  120. if (IsIconic())
  121. {
  122. CPaintDC dc(this); // device context for painting
  123. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  124. // Center icon in client rectangle
  125. int cxIcon = GetSystemMetrics(SM_CXICON);
  126. int cyIcon = GetSystemMetrics(SM_CYICON);
  127. CRect rect;
  128. GetClientRect(&rect);
  129. int x = (rect.Width() - cxIcon + 1) / 2;
  130. int y = (rect.Height() - cyIcon + 1) / 2;
  131. // Draw the icon
  132. dc.DrawIcon(x, y, m_hIcon);
  133. }
  134. else
  135. {
  136. CDialog::OnPaint();
  137. }
  138. }
  139. // The system calls this to obtain the cursor to display while the user drags
  140. //  the minimized window.
  141. HCURSOR CHideProcessDlg::OnQueryDragIcon()
  142. {
  143. return (HCURSOR) m_hIcon;
  144. }
  145. void CHideProcessDlg::OnHide() 
  146. {
  147. UpdateData(TRUE);
  148. HINSTANCE hDLL;
  149.     LPREGISTERSERVICEPROCESS lpRegisterServiceProcess;
  150.     //加载RegisterServiceProcess函数所在的链接库
  151.     hDLL = LoadLibrary("KERNEL32");
  152.     //得到RegisterServiceProcess函数的地址
  153.     lpRegisterServiceProcess = (LPREGISTERSERVICEPROCESS)GetProcAddress(hDLL, "RegisterServiceProcess");
  154.     //执行RegisterServiceProcess函数,在任务列表中隐藏程序
  155.     lpRegisterServiceProcess(GetCurrentProcessId(),1);
  156.     //卸载链接库
  157.     FreeLibrary(hDLL);
  158. //设定定时器
  159. SetTimer(0,m_nSeconds*1000,NULL);
  160. //隐藏程序的同时,隐藏窗口
  161. ShowWindow(SW_HIDE);
  162. }
  163. void CHideProcessDlg::OnTimer(UINT nIDEvent) 
  164. {
  165. CDialog::OnTimer(nIDEvent);
  166. if(nIDEvent==0)
  167. {
  168. HINSTANCE hDLL;
  169. LPREGISTERSERVICEPROCESS lpRegisterServiceProcess;
  170. //加载RegisterServiceProcess函数所在的链接库
  171. hDLL = LoadLibrary("KERNEL32");
  172. //得到RegisterServiceProcess函数的地址
  173. lpRegisterServiceProcess = (LPREGISTERSERVICEPROCESS)GetProcAddress(hDLL,     "RegisterServiceProcess");
  174. //执行RegisterServiceProcess函数,在任务列表中显示程序
  175. lpRegisterServiceProcess(GetCurrentProcessId(),0);
  176. //卸载链接库
  177. FreeLibrary(hDLL);
  178. //显示窗口
  179. ShowWindow(SW_SHOW);
  180. }
  181. }