Problem introduction:
I want to have a log output window in the program. The simplest thing is to put a listview on the form. Then, when the program calls the log output from various places, you can write the log in this listview.
Just like
Logform1-> writelog ("system message ...");
However... because the form is relatively heavy, there are many places to output logs (logs may be output everywhere), and it is not a project yet, and it is not necessarily a form program. If you want to write logs, it would be too much for others to include logform1.h or something.
Just as I have three projects:
Mainui: interface display
Dataproc: data and business processing
Msql: MySQL Encapsulation
The build relationship and dependency relationship are independent of MySQL, dataproc depends on MySQL, and mainui depends on dataproc.
For cleanliness, dataproc has no interface. You can put a form that outputs logs in mainui.
Obviously, it is really difficult for dataproc to turn around and use mainui.
So, dataproc sends a windowsmessage to the interface, and displays the interface through sendmessage.
In this case, system: String Conversion is involved. A new buffer is used. After receiving the buffer, the receiver displays the free buffer.
CmdbLog::CmdbLog(void *pHwnd,UINT wm_id){m_MainHwnd = (HWND)pHwnd;m_MessageID = wm_id;}void CmdbLog::WriteLog(System::String ^ pString,...array<System::Object ^>^pOArray){pin_ptr<const wchar_t> wch = PtrToStringChars(pString);char*pc;int ipc;System::String ^ o;o = System::String::Format(pString,pOArray);ipc = wcslen(wch) + 1;ipc *= sizeof(wchar_t);pc= new char[ipc];memcpy(pc,wch,ipc);::SendMessage(m_MainHwnd,m_MessageID,0,(LPARAM)pc);}
Main form (response to this message in logform)
const UINT wm_LogMessageID = ::RegisterWindowMessageA( "LOGEVENT" );
[Securitypermission (securityaction: Demand, flags = securitypermissionflag: unmanagedcode)] virtual void wndproc (Message % m) override // process messages in the reload window {If (M. MSG = wm_logmessageid) {system: String ^ m_sshowmessagestring; m_sshowmessagestring = gcnew system: string (const wchar_t *) (void *) M. lparam); MessageBox: Show (m_sshowmessagestring); // receives custom messages and processes addlogrow (m_sshowmessagestring); Delete [] (char *) (void *) M. lparam; // release this buffer} form: wndproc (m );}
m_vars->InitCmdbLog((void*)this->Handle,wm_LogMessageID);
void Form1::AddLogRow(System::String^ pString)
{Array <system: String ^> ^ pitem = pstring-> split (',', ':'); array <system :: string ^> ^ PS = gcnew array <system: String ^> (2); System: Windows: forms: listviewitem ^ plistviewitem; if (pitem-> length> 1) {PS [0] = pitem [0]; PS [1] = pstring-> substring (pitem [0]-> Length + 1, pstring-> length-pitem [0]-> length-1);} else {PS [0] = "default"; PS [1] = pstring ;} plistviewitem = gcnew system: Windows: forms: listviewitem (Ps,-1); listview1-> beginupdate (); listview1-> items-> Add (plistviewitem ); listview1-> endupdate ();}