#include "base_form.h" #include "serial_port.h" #include "utils.h" const std::wstring BasicForm::kClassName = L"Basic"; BasicForm::BasicForm(): mMonitorNewSelect(nullptr), mRightShow(nullptr) { this->SetInitSize(1024, 768, true, false); } BasicForm::~BasicForm() { } std::wstring BasicForm::GetSkinFolder() { return L"basic"; } std::wstring BasicForm::GetSkinFile() { return L"basic.xml"; } std::wstring BasicForm::GetWindowClassName() const { return kClassName; } int gTcpServerCnt = 0; int gTcpClientCnt = 0; LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { wprintf(L"recv message 0x%x\r\n", uMsg); if (uMsg == WM_ADD_UART_CLOSE) { wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str()); mRightShow->SetVisible(false); mRightSide->SetAutoDestroy(true); mUartForm[*(wstring*)(wParam)] = nullptr; int cnt = mMonitor->GetRootNode()->GetChildNode(0)->GetChildNodeCount(); ui::TreeNode* p = nullptr; for (int i = 0; i < cnt; i++) { if (mMonitor->GetRootNode()->GetChildNode(0)->GetChildNode(i)->GetText() == *(wstring*)(wParam)) { p = mMonitor->GetRootNode()->GetChildNode(0)->GetChildNode(i); break; } } auto comname = *(wstring*)(wParam); mMonitor->GetRootNode()->GetChildNode(0)->RemoveChildNode(p); UINT PortNum = 0; for (int i = 3; comname[i] != L'\0'; i++) //转换为数字 { PortNum = PortNum * 10 + (comname[i] - L'0'); } SerialPort::ClosePort(PortNum); } if (uMsg == WM_ADD_UART_RECVDATA) { printf("WM_ADD_UART_RECVDATA"); //this->Paint(); } if (uMsg == WM_USER_POS_CHANGED) { } if (uMsg == WM_ADD_TCPCLIENT_MONITOR) { TcpClientInfo* info = (TcpClientInfo*)wParam; TcpClientLibevent* cli = (TcpClientLibevent*)lParam; ui::TreeNode* node = new ui::TreeNode; auto key = info->ip + L":" + std::to_wstring(gTcpClientCnt); node->SetText(key); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 20, 0, 0, 0 }); mMonitor->GetRootNode()->GetChildNode(1)->AddChildNode(node); gTcpClientCnt++; if (mTcpClientForm.find(info->ip + L":" + std::to_wstring(gTcpClientCnt)) == mTcpClientForm.end()) { auto form = new TcpClientForm(this,wstring2string(info->ip),info->port, cli); form->SetChildLayoutXML(L"basic/tcp_form.xml"); form->SetName(key); form->SetVisible(false); mTcpClientForm[key] = form; if (!mRightSide->Add(form)) printf("erroer 1"); } node->AttachAllEvents([this](ui::EventArgs* ev) { if (ui::EventType::kEventSelect == ev->Type) { wprintf(L"%s\r\n", dynamic_cast (ev->pSender)->GetText().c_str()); printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); TcpClientForm* p = mTcpClientForm[dynamic_cast (ev->pSender)->GetText()]; if (p != nullptr) { printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); p->SetAutoDestroy(true); if (mRightShow != nullptr) { mRightShow->SetVisible(false); p->SetVisible(true); mRightShow = p; wprintf(L"%s",p->GetName()); mRightSide->SelectItem(p->GetName()); } else { p->SetVisible(true); mRightSide->SelectItem(p->GetName()); mRightShow = p; } } } return true; }); } if (uMsg == WM_ADD_TCPSERVER_MONITOR) { TcpServerInfo* info = (TcpServerInfo*)wParam; TcpServerLibevent* server = (TcpServerLibevent*)lParam; ui::TreeNode* node = new ui::TreeNode; auto key = info->ip + L":" + std::to_wstring(gTcpServerCnt); node->SetText(key); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 20, 0, 0, 0 }); mMonitor->GetRootNode()->GetChildNode(2)->AddChildNode(node); gTcpServerCnt++; printf("WM_ADD_TCPSERVER_MONITOR\r\n"); if (mTcpServerForm.find(info->ip + L":" + std::to_wstring(gTcpServerCnt)) == mTcpServerForm.end()) { auto form = new TcpServerFrom(this, wstring2string(info->ip), info->port, server); form->SetChildLayoutXML(L"basic/tcp_server_form.xml"); form->SetName(key); form->SetVisible(false); mTcpServerForm[key] = form; if (!mRightSide->Add(form)) printf("error 1"); } node->AttachAllEvents( [this](ui::EventArgs* ev) { if (ui::EventType::kEventSelect == ev->Type) { wprintf(L"%s\r\n", dynamic_cast (ev->pSender)->GetText().c_str()); printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); TcpServerFrom* p = mTcpServerForm[dynamic_cast (ev->pSender)->GetText()]; if (p != nullptr) { printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); p->SetAutoDestroy(true); if (mRightShow != nullptr) { mRightShow->SetVisible(false); p->SetVisible(true); mRightShow = p; wprintf(L"%s", p->GetName()); mRightSide->SelectItem(p->GetName()); } else { wprintf(L"%s", p->GetName()); p->SetVisible(true); mRightSide->SelectItem(p->GetName()); mRightShow = p; } } } return true; } ); } if (uMsg == WM_ADD_UART_MONITOR) { printf("add monitor"); UartInfo* p = (UartInfo*)wParam; wprintf(L"%s", p->name.c_str()); ui::TreeNode* node = new ui::TreeNode; node->SetText(p->name); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 20, 0, 0, 0 }); mMonitor->GetRootNode()->GetChildNode(0)->AddChildNode(node); if (mUartForm.find(p->name) == mUartForm.end()) { auto form = new UartForm(this,p->name, p->baurate, p->data_bits, p->stop_bits, p->verify, p->flow_control); form->SetChildLayoutXML(L"basic/uart_form.xml"); form->SetName(p->name); form->SetVisible(false); mUartForm[p->name] = form; if (!mRightSide->Add(form)) printf("erroer 1"); } node->AttachAllEvents([this](ui::EventArgs* ev){ if (ui::EventType::kEventSelect == ev->Type) { wprintf(L"%s\r\n",dynamic_cast (ev->pSender)->GetText().c_str()); printf("GetCurSel %d\r\n", mRightSide->GetCurSel()); UartForm* p = mUartForm[dynamic_cast (ev->pSender)->GetText()]; if (p != nullptr) { p->SetAutoDestroy(true); if (mRightShow != nullptr) { mRightShow->SetVisible(false); p->SetVisible(true); mRightShow = p; mRightSide->SelectItem(p->GetName()); } else { p->SetVisible(true); mRightSide->SelectItem(p->GetName()); mRightShow = p; } } } return true; }); } return WindowImplBase::HandleMessage(uMsg, wParam, lParam); } void BasicForm::InitWindow(){ /* Show settings menu */ ui::Button* btn_add_new = dynamic_cast(FindControl(L"add_new")); btn_add_new->SetText(L"新建监听项"); if (nullptr != btn_add_new) { btn_add_new->AttachClick([this](ui::EventArgs* args) { if (mMonitorNewSelect == nullptr) { mMonitorNewSelect = new NewMonitorForm(this); mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0); mMonitorNewSelect->CenterWindow(); mMonitorNewSelect->ShowModalFake(this->GetHWND()); } else { mMonitorNewSelect = new NewMonitorForm(this); mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0); mMonitorNewSelect->CenterWindow(); mMonitorNewSelect->ShowModalFake(this->GetHWND()); } return true; }); } mMonitor = dynamic_cast(FindControl(L"tree")); if (nullptr != mMonitor) { ui::TreeNode* node = new ui::TreeNode; node->SetText(nbase::StringPrintf(L"uart")); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 10, 0, 0, 0 }); mMonitor->GetRootNode()->AddChildNode(node); node = new ui::TreeNode; node->SetText(nbase::StringPrintf(L"tcp client")); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 10, 0, 0, 0 }); mMonitor->GetRootNode()->AddChildNode(node); node = new ui::TreeNode; node->SetText(nbase::StringPrintf(L"tcp server")); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 10, 0, 0, 0 }); mMonitor->GetRootNode()->AddChildNode(node); node = new ui::TreeNode; node->SetText(nbase::StringPrintf(L"udp client")); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 10, 0, 0, 0 }); mMonitor->GetRootNode()->AddChildNode(node); node = new ui::TreeNode; node->SetText(nbase::StringPrintf(L"udp server")); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 10, 0, 0, 0 }); mMonitor->GetRootNode()->AddChildNode(node); node = new ui::TreeNode; node->SetText(nbase::StringPrintf(L"udp group")); node->SetClass(L"listitem"); node->SetFixedHeight(20); node->SetMargin({ 10, 0, 0, 0 }); mMonitor->GetRootNode()->AddChildNode(node); } mRightSide = dynamic_cast(FindControl(L"tab_side")); mRightSide->RemoveAll(); this->SetMinInfo(1280, 768, true); this->SetInitSize(1280, 768, false, false); ui::UiRect maxRect = GetMaximizeInfo(); this->Invalidate(maxRect); RECT rc; //取当前窗口区域 GetClientRect(m_hWnd, &rc); //改变窗口大小 //移动窗口到指定区域 ::MoveWindow(m_hWnd, rc.left, rc.top, rc.right, rc.bottom, true); } LRESULT BasicForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { PostQuitMessage(0L); return __super::OnClose(uMsg, wParam, lParam, bHandled); }