wxWidgets一些代码片段(不断更新中)

1:获得本程序的进程ID
        long gpid;
        gpid=::wxGetProcessId();
        wxString tempWxString;
        tempWxString.sprintf(wxT("%d"), gpid);
        wxMessageBox(tempWxString, _("Welcome to..."));
 
2:定时器
        ::wxStartTimer();
        long gt;
        gt=::wxGetElapsedTime(true);
        wxString tempWxString;
        tempWxString.sprintf(wxT("is %d second"), gt/1000);
        wxMessageBox(tempWxString, _("Welcome to..."));
 
3:一句话COPY文件
//bool wxCopyFile(const wxString& file1, const wxString& file2, bool overwrite = true)

::wxCopyFile("c://README.TXT","d://1.txt",true);
 
4:获得系统文件夹
        wxString osDirectory;
        osDirectory=::wxGetOSDirectory();
        wxMessageBox(osDirectory, _("Welcome to..."));
 
5:获得系统版本信息
        wxString OsVersion;
        OsVersion=::wxGetOsDescription();
        wxMessageBox(OsVersion, _("axi Test"));
 
6:窗体坐标
        wxPoint wp;
        wp=frame->GetClientAreaOrigin();
        int x1,y1;
        x1=wp.x;
        y1=wp.y;
        wxString tempWxString;
        tempWxString.sprintf(wxT("x: %d y: %d"), x1,y1);
        wxMessageBox(tempWxString, _("frame xy"));
 
其他的一些文件操作
::wxRemoveFile
bool wxRemoveFile(const wxString& file)

Removes file, returning true if successful.





::wxRenameFile
bool wxRenameFile(const wxString& file1, const wxString& file2, bool overwrite = true)

Renames file1 to file2, returning true if successful.

If overwrite parameter is true (default), the destination file is overwritten if it exists, but if overwrite is false, the functions fails in this case.

::wxRmdir
bool wxRmdir(const wxString& dir, int flags=0)

Removes the directory dir, returning true if successful. Does not work under VMS.

The flags parameter is reserved for future use.

Please notice that there is also a wxRmDir() function which simply wraps the standard POSIX rmdir() function and so return an integer error code instead of a boolean value (but otherwise is currently identical to wxRmdir), don't confuse these two functions.
Telnet是一种通过网络连接远程计算机的协议,而wxWidgets是一个跨平台GUI工具包。如果您想使用wxWidgets编写一个telnet客户端,可以参考以下代码: ```cpp #include <wx/wx.h> #include <wx/socket.h> class MyFrame : public wxFrame { public: MyFrame(wxWindow* parent, const wxString& title) : wxFrame(parent, wxID_ANY, title) { m_textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); m_button = new wxButton(this, wxID_ANY, "Connect"); m_button->Bind(wxEVT_BUTTON, &MyFrame::OnButtonConnect, this); wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); sizer->Add(m_textCtrl, 1, wxEXPAND | wxALL, 10); sizer->Add(m_button, 0, wxALIGN_RIGHT | wxALL, 10); SetSizerAndFit(sizer); } private: void OnButtonConnect(wxCommandEvent& event) { // Create a TCP socket wxSocketClient* socket = new wxSocketClient(wxSOCKET_WAITALL); // Bind the socket events socket->Bind(wxEVT_SOCKET, &MyFrame::OnSocketEvent, this); // Connect to the telnet server socket->Connect("localhost", 23); m_textCtrl->AppendText("Connecting to localhost:23...\n"); } void OnSocketEvent(wxSocketEvent& event) { wxSocketBase* socket = event.GetSocket(); switch (event.GetSocketEvent()) { case wxSOCKET_CONNECTION: m_textCtrl->AppendText("Connected!\n"); break; case wxSOCKET_INPUT: { char buffer[1024]; int size = socket->Read(buffer, sizeof(buffer)).LastCount(); buffer[size] = '\0'; m_textCtrl->AppendText(wxString::FromUTF8(buffer)); break; } case wxSOCKET_LOST: m_textCtrl->AppendText("Disconnected!\n"); socket->Destroy(); break; } } wxTextCtrl* m_textCtrl; wxButton* m_button; }; class MyApp : public wxApp { public: virtual bool OnInit() { MyFrame* frame = new MyFrame(nullptr, "Telnet Client"); frame->Show(); return true; } }; wxIMPLEMENT_APP(MyApp); ``` 以上代码使用了wxSocketClient类来创建TCP套接字,并通过绑定事件来处理连接、接收数据和断开连接等操作。在点击“Connect”按钮时,它将连接到本地主机的23号端口(即telnet服务)。请注意,该代码仅提供了一个基本的框架,您可能需要根据实际情况进行修改和完善。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值