-
Notifications
You must be signed in to change notification settings - Fork 286
/
MainFrm.cpp
295 lines (252 loc) · 6.61 KB
/
MainFrm.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// MainFrm.cpp : CMainFrame 类的实现
//
#include "stdafx.h"
#include "PcProc.h"
#include "MainFrm.h"
#include ".\mainfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_COPYDATA()
ON_UPDATE_COMMAND_UI(IDS_MYDIRFILES, OnUpdateMyFiles)
ON_COMMAND(ID_PROCESS_KILL, OnProcessKill)
ON_UPDATE_COMMAND_UI(ID_PROCESS_KILL, OnUpdateProcessKill)
ON_COMMAND(ID_PROCESS_FLUSH, OnProcessFlush)
ON_UPDATE_COMMAND_UI(ID_PROCESS_FLUSH, OnUpdateProcessFlush)
ON_WM_SIZE()
ON_WM_CLOSE()
ON_MESSAGE(WM_USERRCLICKPROCESS,OnUserRClickProcess)
ON_WM_SETCURSOR()
ON_MESSAGE(WM_SETMYCURSOR,OnSetMyCursor)
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
IDS_MYDIRFILES,
};
// CMainFrame 构造/析构
CMainFrame::CMainFrame()
{
m_WaitCur = AfxGetApp()->LoadCursor(IDC_CURSOR_WAIT);
m_CurSorStatus = TRUE;
memset(&m_MissionData,0,sizeof(CLIENTDATA));
m_MissionData.m_WorkEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
m_MissionData.m_TransData = new char[TRAN_DATA_LEN + 100];
}
CMainFrame::~CMainFrame()
{
if(m_MissionData.m_TransData)
{
CloseHandle(m_MissionData.m_WorkEvent);
delete [] m_MissionData.m_TransData;
}
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("未能创建状态栏\n");
return -1; // 未能创建
}
m_MissionData.hParent = GetSafeHwnd();
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
cs.style ^= WS_MAXIMIZE;
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(
CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW),
(HBRUSH) ::GetStockObject(WHITE_BRUSH),
AfxGetApp()->LoadIcon(IDR_MAINFRAME));
return TRUE;
}
// CMainFrame 诊断
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
//接收套接字信息
LPWSAPROTOCOL_INFO pInfo =
(LPWSAPROTOCOL_INFO) pCopyDataStruct->lpData;
if(pCopyDataStruct->dwData == CONN_FILE_PROC_RECV)
{
m_MissionData.m_SendSocket =
WSASocket(AF_INET, SOCK_STREAM,0,
pInfo,0,WSA_FLAG_OVERLAPPED);
return TRUE;
}
if(pCopyDataStruct->dwData == CONN_FILE_PROC_SEND)
{
m_MissionData.m_RecvSocket =
WSASocket(AF_INET, SOCK_STREAM,0,
pInfo,0,WSA_FLAG_OVERLAPPED);
m_CurSorStatus = FALSE;
PostMessage(WM_SETCURSOR);
OnProcessFlush();
return TRUE;
}
return CFrameWnd::OnCopyData(pWnd, pCopyDataStruct);
}
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
CCreateContext pCreateContext;
pCreateContext.m_pCurrentFrame = this;
pCreateContext.m_pCurrentDoc = NULL;
//进程列表视
pCreateContext.m_pNewViewClass = RUNTIME_CLASS(CMyProcessListView);
pProcessListView = (CMyProcessListView *) CreateView(&pCreateContext,AFX_IDW_PANE_FIRST);
ASSERT(pProcessListView);
return TRUE;
}
void CMainFrame::ShowResponse(UINT m_Response)
{
char m_Text[4096] = {0};
switch(m_Response)
{
case GET_PROCESS_LIST_ERR : strcpy(m_Text,"取进程列表失败");break;
case KILL_PROCESS_ERR : strcpy(m_Text,"终止进程失败");break;
case TRANS_DATA_ERR : strcpy(m_Text,"通信失败");break;
default : return;
}
m_wndStatusBar.SetPaneText(0,m_Text);
}
void CMainFrame::OnProcessKill()
{
int nItem = pProcessListView->GetListCtrl().GetNextItem(-1, LVNI_SELECTED);
char m_TmpBuf[2048];
memset(m_TmpBuf,0,sizeof(m_TmpBuf));
strcpy(m_TmpBuf,(LPCTSTR) pProcessListView->GetListCtrl().GetItemText(nItem,0));
*((DWORD*) m_MissionData.m_TransData) = atol(m_TmpBuf);
m_MissionData.m_TransLen = sizeof(DWORD);
m_MissionData.m_Command = CLIENT_PROCESS_KILL;
if(!ProcessOneTrans(&m_MissionData))
{
ShowResponse(m_MissionData.m_Command);
return ;
}
if(m_MissionData.m_Command != 0)
{
ShowResponse(m_MissionData.m_Command);
return ;
}
pProcessListView->GetListCtrl().DeleteItem(nItem);
}
void CMainFrame::OnUpdateProcessKill(CCmdUI* pCmdUI)
{
pCmdUI->Enable(pProcessListView->GetListCtrl().GetSelectedCount() == 1
&& !m_MissionData.m_IsWork);
}
void CMainFrame::OnProcessFlush()
{
m_MissionData.m_TransLen = 0;
m_MissionData.m_Command = CLIENT_PROCESS_LIST;
if(!ProcessOneTrans(&m_MissionData))
{
ShowResponse(m_MissionData.m_Command);
return ;
}
if(m_MissionData.m_Command == NOT_FIND_ANY_FILE)
{
pProcessListView->GetListCtrl().DeleteAllItems();
return ;
}
if(m_MissionData.m_Command != 0)
{
ShowResponse(m_MissionData.m_Command);
return ;
}
DWORD dwFileSize = m_MissionData.m_TransLen;
BYTE * m_DesBuf = (LPBYTE) m_MissionData.m_TransData;
pProcessListView->GetListCtrl().DeleteAllItems();
LPPROCESSLIST pList = (LPPROCESSLIST) m_DesBuf;
for(DWORD i = 0; i < (dwFileSize/sizeof(PROCESSLIST)); i++)
{
pProcessListView->InsertMyItem(&pList[i]);
}
}
void CMainFrame::OnUpdateProcessFlush(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_MissionData.m_IsWork);
}
void CMainFrame::OnUpdateMyFiles(CCmdUI* pCmdUI)
{
char m_Text[256] = {0};
sprintf(m_Text,"共有%d个进程",pProcessListView->GetListCtrl().GetItemCount());
pCmdUI->SetText(m_Text);
}
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
if(m_wndStatusBar.GetSafeHwnd())
{
UINT nID,nStyle;
int Width;
m_wndStatusBar.GetPaneInfo(1,nID,nStyle,Width);
Width = cx/4;
m_wndStatusBar.SetPaneInfo(1,nID,nStyle,Width);
}
}
void CMainFrame::OnClose()
{
SetEvent(m_MissionData.m_WorkEvent);
closesocket(m_MissionData.m_SendSocket);
closesocket(m_MissionData.m_RecvSocket);
CFrameWnd::OnClose();
}
LRESULT CMainFrame::OnUserRClickProcess(WPARAM wParam,LPARAM lParam)
{
CPoint m_point;
CMenu m_menu;
m_menu.LoadMenu(IDR_MENU_PROCESS_LIST);
GetCursorPos(&m_point);
SetForegroundWindow();
m_menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|
TPM_RIGHTBUTTON,m_point.x,m_point.y,this);
m_menu.DestroyMenu();
return TRUE;
}
BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if(m_CurSorStatus)
{
SetCursor(m_WaitCur);
return TRUE;
}
return CFrameWnd::OnSetCursor(pWnd, nHitTest, message);
}
LRESULT CMainFrame::OnSetMyCursor(WPARAM wParam,LPARAM lParam)
{
if(wParam)
{
m_CurSorStatus = TRUE;
PostMessage(WM_SETCURSOR);
}
else
{
m_CurSorStatus = FALSE;
PostMessage(WM_SETCURSOR);
}
return TRUE;
}