123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- #include "stdafx2.h"
- #include "mainfrm.h"
- #include "ukeytask.h"
- BOOL CMainFrame::PreTranslateMessage( MSG* pMsg )
- {
- return CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg);
- }
- BOOL CMainFrame::OnIdle()
- {
- BOOL bEnable = m_iSelectIdx != -1;
- UIEnable(ID_START, bEnable);
- UIEnable(ID_STOP, bEnable);
- UIEnable(ID_PAUSE, bEnable);
- UIEnable(ID_CONTINUE, bEnable);
- UIEnable(ID_KILL, bEnable);
- UIUpdateToolBar();
- return FALSE;
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- CreateSimpleToolBar();
- m_hWndStatusBar = m_status.Create(m_hWnd, rcDefault, NULL, WS_VISIBLE|WS_CHILD,WS_EX_STATICEDGE);
- m_status.SetMinHeight(120);
- int _parts[] = {400,200,200,-1};
- m_status.SetParts(sizeof(_parts)/sizeof(_parts[0]), _parts);
- //static
- WTL::CStatic staticbar;
- RECT rc;
- m_status.GetRect(0, &rc);
- rc.left += 5;
- rc.right= rc.left+ 110;
- rc.top += 30;
- rc.bottom = rc.top + 30;
- staticbar.Create(m_hWndStatusBar,&rc,"UKEY密码(8位)",WS_CHILD|WS_VISIBLE | ES_AUTOHSCROLL | ES_LEFT);
- rc.left = rc.right-3;
- rc.right= rc.left+ 180;
- rc.top -= 5;
- rc.bottom = rc.top + 30;
- m_cmd.SetOwner(this);
- m_cmd.Create(m_hWndStatusBar, &rc, NULL, WS_CHILD|ES_PASSWORD|WS_VISIBLE | ES_AUTOHSCROLL | ES_LEFT, WS_EX_STATICEDGE);
- m_cmd.SetPasswordChar('*');
- rc.left = rc.left+45;
- rc.right= rc.left+ 80;
- rc.top =rc.bottom+15;
- rc.bottom = rc.top + 25;
- m_OpenKeyboard.Create(m_hWndStatusBar, &rc,_T("软键盘"),WS_CHILD | WS_VISIBLE |SS_NOTIFY ,0,IDC_BTN_SOFTBOARD);
- rc.left = rc.left+150;
- rc.right= rc.left+ 80;
- rc.top -=45;
- rc.bottom = rc.top + 30;
- m_PasswordOk.Create(m_hWndStatusBar, &rc,_T("确定"),WS_CHILD| WS_VISIBLE,0,IDC_BTN_OK);
- //打开机柜按钮
- m_status.GetRect(1, &rc);
- rc.left = rc.left+80;
- rc.top = rc.top+30;
- rc.bottom = rc.top + 50;
- rc.right = rc.left + 120;
- m_OpenDoor.Create(m_hWndStatusBar, &rc,_T("打开机柜"),WS_CHILD| WS_VISIBLE,0,IDC_BTN_OPENDOOR);
- rc.left = rc.right+60;
- rc.right = rc.left + 120;
- m_OpenLocalconsole.Create(m_hWndStatusBar, &rc,_T("进入本地维护"),WS_CHILD| WS_VISIBLE,0,IDC_BTN_LOCALCONSOLE);
- m_hWndClient = m_splitter.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
- UpdateLayout();
- m_verticalSplitter.Create(m_splitter, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
- //output
- m_output.Create(m_splitter, rcDefault, NULL,
- WS_CHILD|WS_VISIBLE|ES_AUTOVSCROLL|ES_LEFT|ES_MULTILINE|WS_VSCROLL, WS_EX_STATICEDGE);
- m_output_font.CreateFont(20,0,0,0,FALSE,FALSE,FALSE,0,DEFAULT_CHARSET,
- OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,DEFAULT_QUALITY, DEFAULT_PITCH,NULL);
- m_output.SetFont(m_output_font);
- CRect rect;
- GetClientRect(rect);
- m_splitter.SetSplitterPos(rect.Height()*3/5);
- m_splitter.SetSplitterPanes(m_verticalSplitter, m_output);
- m_left_list.Create(m_verticalSplitter, rcDefault, NULL,
- WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
- LVS_REPORT | LVS_AUTOARRANGE | LVS_SHOWSELALWAYS | LVS_SINGLESEL, 0, IDC_LIST1);
- m_left_list.SetExtendedListViewStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT, 0);
- m_right_list.Create(m_verticalSplitter, rcDefault, NULL,
- WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
- LVS_REPORT | LVS_AUTOARRANGE | LVS_SHOWSELALWAYS | LVS_SINGLESEL, 0, IDC_LIST2);
- m_right_list.SetExtendedListViewStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT, 0);
- m_verticalSplitter.SetSplitterPos((rect.Width()-4)/2);
- m_verticalSplitter.SetSplitterPanes(m_left_list, m_right_list);
- LVCOLUMN col = {0};
- col.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
- col.fmt = LVCFMT_LEFT;
- col.cx = 5/3*rect.Width() / 10-1;
- col.pszText = _T("terminalno");
- m_left_list.InsertColumn(0, &col);
- m_right_list.InsertColumn(0, &col);
- col.pszText = _T("warning");
- col.cx = 5/3*rect.Width() / 10-1;
- m_left_list.InsertColumn(1, &col);
- m_right_list.InsertColumn(1, &col);
- col.pszText = _T("entityname");
- col.cx = 5/3*rect.Width() / 10-1;
- m_left_list.InsertColumn(2, &col);
- m_right_list.InsertColumn(2, &col);
- col.pszText = _T("time");
- col.cx = 5/3*rect.Width() / 10-1;
- m_left_list.InsertColumn(3, &col);
- m_right_list.InsertColumn(3, &col);
- UIAddToolBar(m_hWndToolBar);
- CMessageLoop* pLoop = _Module.GetMessageLoop();
- pLoop->AddMessageFilter(this);
- pLoop->AddIdleHandler(this);
- // load all entity
- CAutoArray<EntityEntry> Entities;
- m_pConsole->GetAllEntity(Entities);
- for (int i = 0; i < Entities.GetCount(); ++i) {
- LoadEntity(i, Entities[i]);
- }
- ::AtlLoadAccelerators(MAKEINTRESOURCE(IDR_MAINFRAME));
- return 0;
- }
- int CMainFrame::FindEntitylistItemIndex(int entity_id)
- {
- int i;
- for (i = 0; i < m_left_list.GetItemCount(); ++i) {
- int id = (int)m_left_list.GetItemData(i);
- if (id == entity_id)
- return i*2;
- }
- for (i = 0; i < m_right_list.GetItemCount(); ++i) {
- int id = (int)m_right_list.GetItemData(i);
- if (id == entity_id)
- return i*2+1;
- }
- return -1;
- }
- int CMainFrame::FindEntitylistItemIndex(const char *entity_name)
- {
- int i;
- for (i = 0; i < m_left_list.GetItemCount(); ++i) {
- char str[128];
- m_left_list.GetItemText(i, 0, str, sizeof(str));
- char *p = strchr(str, ':');
- if (p)
- ++p;
- if (_stricmp(entity_name, p) == 0)
- return i*2;
- }
- for (i = 0; i < m_right_list.GetItemCount(); ++i) {
- char str[128];
- m_right_list.GetItemText(i, 0, str, sizeof(str));
- char *p = strchr(str, ':');
- if (p)
- ++p;
- if (_stricmp(entity_name, p) == 0)
- return i*2+1;
- }
- return -1;
- }
- void CMainFrame::SetEntityState(int entity_id, int state, int pid)
- {
- int idx = FindEntitylistItemIndex(entity_id);
- if (idx != -1) {
- char tmp[16];
- wsprintfA(tmp, "%d", pid);
- if (idx & 1) {
- m_right_list.SetItem((idx-1)/2, 2, LVIF_TEXT, (LPCSTR)EntityEntry::GetStateName(state), 0, 0, 0, NULL);
- m_right_list.SetItem((idx-1)/2, 3, LVIF_TEXT, tmp, 0, 0, 0, NULL);
- } else {
- m_left_list.SetItem(idx/2, 2, LVIF_TEXT, (LPCSTR)EntityEntry::GetStateName(state), 0, 0, 0, NULL);
- m_left_list.SetItem(idx/2, 3, LVIF_TEXT, tmp, 0, 0, 0, NULL);
- }
- }
- }
- void CMainFrame::SetEntityState(const char *entity_name, int state, int pid)
- {
- int idx = FindEntitylistItemIndex(entity_name);
- if (idx != -1) {
- char tmp[16];
- wsprintfA(tmp, "%d", pid);
- if (idx & 1) {
- m_right_list.SetItem((idx-1)/2, 2, LVIF_TEXT, (LPCSTR)EntityEntry::GetStateName(state), 0, 0, 0, NULL);
- m_right_list.SetItem((idx-1)/2, 3, LVIF_TEXT, tmp, 0, 0, 0, NULL);
- } else {
- m_left_list.SetItem(idx/2, 2, LVIF_TEXT, (LPCSTR)EntityEntry::GetStateName(state), 0, 0, 0, NULL);
- m_left_list.SetItem(idx/2, 3, LVIF_TEXT, tmp, 0, 0, 0, NULL);
- }
- }
- }
- void CMainFrame::LoadEntity( int idx, EntityEntry &e )
- {
- char tmp[16];
- wsprintfA(tmp, "%d", e.Pid);
- CSimpleStringA strName = CSimpleStringA::Format("%s:%s", (LPCSTR)e.ModuleName, (LPCSTR)e.Name);
- if (idx & 1) {
- idx = (idx-1)/2;
- m_right_list.InsertItem(LVIF_PARAM|LVIF_TEXT, idx, (LPCSTR)strName, 0, 0, 0, e.Id);
- m_right_list.AddItem(idx, 1, (LPCSTR)EntityEntry::GetTypeName(e.Type));
- m_right_list.AddItem(idx, 2, (LPCSTR)EntityEntry::GetStateName(e.State));
- m_right_list.AddItem(idx, 3, tmp);
- } else {
- idx = idx / 2;
- m_left_list.InsertItem(LVIF_PARAM|LVIF_TEXT, idx, (LPCSTR)strName, 0, 0, 0, e.Id);
- m_left_list.AddItem(idx, 1, (LPCSTR)EntityEntry::GetTypeName(e.Type));
- m_left_list.AddItem(idx, 2, (LPCSTR)EntityEntry::GetStateName(e.State));
- m_left_list.AddItem(idx, 3, tmp);
- }
- }
- #define PREFIX_LEN 26
- // [2011-02-20 16:13:23.123]
- void CMainFrame::output_printf(const char *fmt, ...)
- {
- int len;
- va_list arg;
- va_start(arg, fmt);
- len = _vscprintf(fmt, arg);
- if (len > 0) {
- SYSTEMTIME st;
- char *buf;
- GetLocalTime(&st);
- buf = (char*)malloc(PREFIX_LEN + len+1);
- sprintf(buf, "[%04d-%02d-%02d %02d:%02d:%02d.%03d] ", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
- vsprintf(buf+PREFIX_LEN, fmt, arg);
- m_strOuput += buf;
- m_nLineCount++;
- // 检测最大长度
- CheckOutputMaxLen();
- m_output.SetWindowText(m_strOuput);
- m_output.SetFocus();
- m_output.ShowCaret();
- m_output.SetSel(m_strOuput.GetLength(), m_strOuput.GetLength());
- m_output.LineScroll(-1);
- free(buf);
- }
- va_end(arg);
- }
- void CMainFrame::CheckOutputMaxLen()
- {
- if (m_nLineCount > 200)
- {
- int count = 0;
- int index = 0;
- while(count < 50 && index < m_strOuput.GetLength())
- {
- index = m_strOuput.Find("\r\n", index);
- if (index == -1)
- break;
- index += 2;
- count++;
- }
- if (index >0 && index < m_strOuput.GetLength())
- {
- m_strOuput.Delete(0, index);
- m_nLineCount -= count;
- }
- else
- {
- m_strOuput = "";
- m_nLineCount = 0;
- }
- }
- }
- #undef PREFIX_LEN
- LRESULT CMainFrame::OnListLeftItemchanged(NMHDR* phdr)
- {
- NMLISTVIEW* pnmlv = (NMLISTVIEW*) phdr;
- m_iSelectIdx = m_left_list.GetSelectedIndex();
- if (m_iSelectIdx != -1) {
- m_iSelectIdx = m_iSelectIdx * 2;
- }
- return 0;
- }
- LRESULT CMainFrame::OnListRightItemchanged(NMHDR* phdr)
- {
- NMLISTVIEW* pnmlv = (NMLISTVIEW*) phdr;
- m_iSelectIdx = m_right_list.GetSelectedIndex();
- if (m_iSelectIdx != -1) {
- m_iSelectIdx = m_iSelectIdx * 2+1;
- }
- return 0;
- }
- //LRESULT CMainFrame::OnSoftBoard()
- //{
- // ShellExecute(NULL, _T("open"), _T("osk.exe"), NULL, NULL, SW_SHOW);
- // return 0;
- //}
- LRESULT CMainFrame::OnListLeftItemclicked(NMHDR* phdr)
- {
- m_iSelectIdx = m_left_list.GetSelectedIndex();
- if (m_iSelectIdx != -1) {
- m_iSelectIdx = m_iSelectIdx * 2;
- }
- return 0;
- }
- LRESULT CMainFrame::OnListRightItemclicked(NMHDR* phdr)
- {
- m_iSelectIdx = m_right_list.GetSelectedIndex();
- if (m_iSelectIdx != -1) {
- m_iSelectIdx = m_iSelectIdx * 2+1;
- }
- return 0;
- }
- void CMainFrame::OnTimer(UINT_PTR nIDEvent)
- {
- if (m_bOperationPending) {
- }
- }
- void CMainFrame::StartEntity(const char *entity_name, const char *cmdline)
- {
- ErrorCodeEnum Error = m_pConsole->AsyncStartEntity(entity_name, cmdline, NULL);
- if (Error == Error_Succeed) {
- output_printf("starting %s\r\n", entity_name);
- m_bOperationPending = TRUE;
- SetTimer(0, 100, NULL);
- } else {
- output_printf("start %s failed, error = 0x%08X\r\n", entity_name, Error);
- }
- }
- void CMainFrame::StopEntity(const char *entity_name)
- {
- ErrorCodeEnum Error = m_pConsole->AsyncStopEntity(entity_name, NULL);
- if (Error == Error_Succeed) {
- output_printf("stopping %s\r\n", entity_name);
- m_bOperationPending = TRUE;
- SetTimer(0, 100, NULL);
- } else {
- output_printf("stop %s failed, error = 0x%08X\r\n", entity_name, Error);
- }
- }
- void CMainFrame::OnEditEnter(const char *tmp)
- {
- char entity[128];
- if (_strnicmp(tmp, "start", 5) == 0) { /* start entity */
- const char *p = tmp+5;
- while (*p == ' ') p++;
- if (sscanf(p, "%s", entity) == 1) {
- const char *cmdline = p + strlen(entity);
- while (*cmdline == ' ') cmdline++;
- StartEntity(entity, cmdline);
- } else {
- output_printf("invalid input format!\r\n");
- }
- } else if (_strnicmp(tmp, "stop", 4) == 0) { /* stop entity */
- const char *p = tmp+4;
- while (*p == ' ') p++;
- if (sscanf(p, "%s", entity) == 1) {
- StopEntity(entity);
- } else {
- output_printf("invalid input format!\r\n");
- }
- } else if (_strnicmp(tmp, "pause", 5) == 0) { /* pause entity */
- const char *p = tmp+5;
- while (*p == ' ') p++;
- if (sscanf(p, "%s", entity) == 1) {
- //PauseEntity(entity);
- } else {
- output_printf("invalid input format!\r\n");
- }
- } else if (_strnicmp(tmp, "continue", 8) == 0) { /* continue entity */
- const char *p = tmp+8;
- while (*p == ' ') p++;
- if (sscanf(p, "%s", entity) == 1) {
- //ContinueEntity(entity);
- } else {
- output_printf("invalid input format!\r\n");
- }
- } else if (_strnicmp(tmp, "terminate", 9) == 0) { /* terminate entity */
- const char *p = tmp+9;
- while (*p == ' ') p++;
- if (sscanf(p, "%s", entity) == 1) {
- //TerminateEntity(entity);
- } else {
- output_printf("invalid input format!\r\n");
- }
- } else if (_stricmp(tmp, "help") == 0 || _stricmp(tmp, "?") == 0) {
- Usage();
- }
- m_cmd.SetWindowText(NULL);
- }
- void CMainFrame::Usage()
- {
- output_printf("help -- display help usage\r\n");
- output_printf("start <Entity_Name> [arg1] [arg2] ... [argn] -- start entity with args[optional] \r\n");
- output_printf("stop <Entity_Name> -- stop entity\r\n");
- output_printf("pause <Entity_Name> -- pause entity\r\n");
- output_printf("continue <Entity_Name> -- pause entity\r\n");
- output_printf("terminate <Entity_Name> -- terminate entity\r\n");
- }
- HBRUSH CMainFrame::OnCtlColorEdit( CDCHandle dc, CEdit edit )
- {
- if (m_output.m_hWnd == edit.m_hWnd) {
- dc.SetTextColor(RGB(0, 255, 0));
- dc.SetBkColor(RGB(0,0,0));
- return (HBRUSH)GetStockObject(BLACK_BRUSH);
- } else {
- SetMsgHandled(FALSE);
- return 0;
- }
- }
- HBRUSH CMainFrame::OnCtlColorStatic( CDCHandle dc, CStatic wndStatic )
- {
- if (m_output.m_hWnd == wndStatic.m_hWnd) {
- dc.SetTextColor(RGB(0, 255, 0));
- dc.SetBkColor(RGB(0, 0, 0));
- return (HBRUSH)GetStockObject(BLACK_BRUSH);
- } else {
- SetMsgHandled(FALSE);
- return 0;
- }
- }
- void CMainFrame::OnOutputChanged( UINT uNotifyCode, int nID, CWindow wndCtl )
- {
- int n = m_output.GetWindowTextLength();
- m_strOuput.GetBufferSetLength(n);
- m_output.GetWindowText(m_strOuput.GetBufferSetLength(n), n+1);
- }
- void CMainFrame::OnStartMenuRange(UINT uNotifyCode, int nID, CWindow wndCtl)
- {
- CMenuHandle menuMain = GetMenu();
- CMenuHandle menuStartList = menuMain.GetSubMenu(2);
- WTL::CString strText;
- menuStartList.GetMenuStringA(nID, strText, MF_BYCOMMAND);
- if (strText.GetLength() > 0) {
- int m = strText.Find("\t", 0);
- if (m > 0) {
- strText = strText.Left(m);
- }
- const char *p = strchr(strText, ' ');
- if (p) {
- char name[128];
- memcpy(name, strText, p-(LPCSTR)strText);
- name[p-(LPCSTR)strText] = 0;
- p++;
- StartEntity(name, p);
- }
- }
- }
- void CCommandEdit::OnChar( UINT nChar, UINT nRepCnt, UINT nFlags )
- {
- if (nChar == VK_RETURN) {
- char tmp[512];
- int n = GetWindowText(tmp, sizeof(tmp));
- if (n <= 0)
- return;
- if (m_nHistorySize) {
- int last = (m_nHistoryHead + m_nHistorySize-1) % MAX_HISTORY;
- if (strcmp(tmp, m_strHistoryList[last]) != 0) {
- m_nHistoryCursor = (m_nHistoryHead + m_nHistorySize) % MAX_HISTORY;
- m_strHistoryList[m_nHistoryCursor] = tmp;
- m_nHistorySize = (m_nHistorySize + 1) % MAX_HISTORY;
- }
- } else {
- m_nHistoryCursor = (m_nHistoryHead + m_nHistorySize) % MAX_HISTORY;
- m_strHistoryList[m_nHistoryCursor] = tmp;
- m_nHistorySize = (m_nHistorySize + 1) % MAX_HISTORY;
- }
- m_frm->OnEditEnter(tmp);
- SetWindowText(NULL);
- } else {
- SetMsgHandled(FALSE);
- }
- }
- //
- //void CCommandEdit::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags )
- //{
- // int last = m_nHistoryCursor;
- //
- // if (nChar == VK_UP) {
- // if (m_nHistoryCursor != m_nHistoryHead)
- // m_nHistoryCursor = (m_nHistoryCursor + MAX_HISTORY - 1) % MAX_HISTORY;
- // SetWindowText(m_strHistoryList[last]);
- // } else if (nChar == VK_DOWN) {
- // if (m_nHistorySize && (m_nHistoryCursor != (m_nHistoryHead+m_nHistorySize-1) % MAX_HISTORY))
- // m_nHistoryCursor = (m_nHistoryCursor + 1) % MAX_HISTORY;
- // SetWindowText(m_strHistoryList[last]);
- // } else {
- // SetMsgHandled(FALSE);
- // }
- //}
|