Control content Update DrawText UpdateData setwindowtext

Source: Internet
Author: User
Tags drawtext

To draw a "talk Duration: XX:XX:XX" On a static control, for example, an interface that shows the user's call duration

With regard to drawing, you can use the handle obtained to the control and CDC, draw through DrawText, or update through the API function UpdateData or SetWindowText.

Timing can be timed by thread or by using a timer.

Below, according to the different drawing way to compare, see which is the most convenient ~

Method One: DrawText + thread Timing

This is undoubtedly the most stupid way ...

Code:

Variable

cdc* CDC for Pdcstatic;//static controls

Whether the BOOL biintstate;//is initialized (the processing of the wm_size must be done after the initialization is complete, otherwise an error occurs)

BOOL bonline;//thread Run control character

The DWORD dwstarttime;//is used to record the start time of a call by GetTickCount () where it needs to start timing

HANDLE hupdateevent;//for thread synchronization event
cwinthread* pthreadupdate;//timed thread handle


Static UINT CALLBACK Paintwindow (LPVOID lParam);//Timed thread

void Gettimespace (DWORD dwstarttime,cstring & Sztime);//used to get the CString form of the duration

Dialog initialization function

BOOL C****dlg::oninitdialog ()
{
CDialog::OnInitDialog ();

Todo:add Extra initialization here
This->pdcstatic = NULL;

This->bonline = TRUE;

To create a thread synchronization event
This->hupdateevent = CreateEvent(NULL, TRUE, FALSE, "updateevent");
To create a timed thread
This->pthreadupdate=new CWinThread;
this->pthreadupdate->m_bautodelete=true;
this->pthreadupdate=AfxBeginThread(Afx_threadproc (Paintwindow), this,thread_priority_normal,0,0, NULL);

Initialize the end of the position

This->biintstate = FALSE;

return TRUE; Return TRUE unless you set the focus to a control
Exception:ocx Property Pages should return FALSE
}

Window wm_size response function
void C****dlg::onsize (UINT nType, int cx, int cy)
{
Cdialog::onsize (NType, CX, CY);

Todo:add your message Handler code here
if (!this->biintstate)
{

CRect Rconline;

Gets the client area size of the current window
GetClientRect (&rconline);

cwnd* hstatic = GetDlgItem (idc_time);
This->pdcstatic = Hstatic->getdc ();

Gets the size of the emoji on the static to determine the position and size of the static control

CSize font = this->pdcstatic->gettextextent ("You");

and customer area is the same width, starting point in the customer area of 1/4, high-word height of 3 times times
This->rcalltext = this->rconline;
This->rcalltext.top + = This->rconline.height ()/4;
This->rcalltext.bottom = this->rcalltext.top+ (font.cy*3);

Nudge Static Control

Hstatic->movewindow (&this->rcalltext, TRUE);
}
}

Timed Threads

UINT C****dlg::P aintwindow (LPVOID lParam)
{
C****dlg * Vnol = (c****dlg*) LParam;

while (Vnol->bonline)
{
WaitForSingleObject (vnol->hupdateevent,infinite);//wait for the event to be set, this thread pauses on this
:: SendMessage (vnol->m_hwnd,wm_paint,0,0);//Send Refresh message to window
Sleep (100);//time interval is 100ms
}

return 1;
}

Refresh function

void C****dlg::onpaint ()
{
CPAINTDC DC (this); Device context for painting

Todo:add your message Handler code here
Cdialog::onpaint () for painting messages

Get the current length of time

CString Strstatictext;

CString Strtimespace;

A custom function that is used to get the CString form of the time-length

Dwstarttime where it takes to start timing.

This->gettimespace (This->dwstarttime,strtimespace);

Strstatictext = "Length of call:" + strtimespace;
CBrush Brush (RGB (255,255,255));

Draw a white background to the control area
This->pdcstatic->fillrect (CRect (0,0,this->rcalltext.width (), This->rcalltext.height ()), &brush);
Display words to the screen with DrawText on the control

This->pdcstatic->DrawText(Sztext,crect (0,0,this->rcalltext.width (), This->rcalltext.height ()), dt_center| dt_vcenter| Dt_singleline);

}

These three methods share functions to get the CString form of jet lag.

void C****dlg::gettimespace (DWORD dwstarttime,cstring & Sztime)
{
DWORD dwnowtime=0;
DWORD dwspace=0;
Char Time[10]={0,};

Get current time
Dwnowtime = GetTickCount ();

Start calculating the time difference
Dwspace = Dwnowtime-dwstarttime;

Dwspace = dwspace/1000;//converted to seconds
int sec = Dwspace ';
int min = ((dwspace-sec)/60) ';
int hour = ((dwspace-sec)/60)/60;

sprintf (Time, "-:-:-", hour,min,sec);
Sztime.format ("%s", (char*) time);

Return
}

Method Two: SetWindowText + timer

This method is OK. The idea is: Start the timer, setwindowtext the control every once in a while.

In this method, we use two controls, one is the Statictext control, and the other is the edit control.

SetWindowText: Sets the display information for the control, which is for a single control drop!

Cwnd::setwindowtext

void SetWindowText (LPCTSTR lpszstring );

Parameters

Lpszstring

Points to a CString object or null-terminated string to is used as the new title or control text.

Corresponding to the SetWindowText is the GetWindowText.

GetWindowText: Gets the current contents of the control (for the edit control, you can get the current display in its en_change processing).

Cwnd::getwindowtext

int GetWindowText (LPTSTR lpszstringbuf, int nmaxcount ) const;

void GetWindowText (cstring& rString ) const;

The code is as follows:

//Variables

CString m_statictext;//the string to display on the static control

CString m_edittext;//the string to display on the edit control

Whether the BOOL m_binitstate;//is in the initialization state (to overload onsize, it must be after the initialization has ended, otherwise an error)

The DWORD m_dwstarttime;//start time is obtained by GetTickCount () where it needs to start timing

void Gettimespace (DWORD dwstarttime,cstring & Sztime);//function for the current time difference string

The initialization function of the window

BOOL C****dlg::oninitdialog ()
{
CDialog::OnInitDialog ();

Set The icon for this dialog. The framework does this automatically
When the application ' s main window was not a dialog
SetIcon (M_hicon, TRUE); Set Big icon
SetIcon (M_hicon, FALSE); Set Small Icon

Todo:add Extra initialization here
Set timer
SetTimer (1,500,null);

This->m_binitstate = FALSE;

return TRUE; Return TRUE unless you set the focus to a control
}


Timer response function

void Cupdatedatedlg::ontimer (UINT nidevent)
{
Todo:add your message handler code here and/or call default
Switch (nidevent)
{
Case 1:
Timer time to
{
Calculates the time difference str

CString strtemp;
This->gettimespace (this->m_dwstarttime,strtemp);
This->m_statictext = "Length of call:" +STRTEMP;

Edit and static display the same

This->m_edittext = this->m_statictext;
Get a handle to a control
cwnd* hstatic = GetDlgItem (idc_mystatic);

cwnd* Hedit = GetDlgItem (Idc_myedit);

Show statements to two controls
Hstatic->setwindowtext(this->m_statictext);
Hedit->setwindowtext(this->m_edittext);
}
Break
Default
Cdialog::ontimer (nidevent);
}
}

The edit control displays the response function of the en_change when the change occurs

void C****dlg::onchangemyedit ()
{

CString Sznowtext;
cwnd* Hedit = GetDlgItem (Idc_myedit);
Hedit->getwindowtext(sznowtext);
}

Method Three: UpdateData + timer

This method is the same as the method of two ideas, but the difference lies in the use of UpdateData ~

UpdateData Refreshes the entire dialog box , rather than working on the control as if it were setwindowtext .

UpdateData is an MFC API, so this method is used based on MFC.

Using this method, you first establish the connection between the control and the variable through ClassWizard. When you modify the value of a variable, and you want the dialog control to update the display, you should call UpdateData (FALSE) after modifying the variable, and if you want to know exactly what the user entered in the dialog box, call UpdateData (TRUE) before accessing the variable.

ClassWizard establishing a link between a control and a variable



Code:

The code that establishes a link between a control and a variable is reflected by ClassWizard

. h

{{Afx_data (CUPDATEDATEDLG)
enum {IDD = Idd_updatedate_dialog};
CString M_statictext;
CString M_edittext;
}}afx_data

. cpp

void Cupdatedatedlg::D odataexchange (cdataexchange* PDX)
{
CDialog::D odataexchange (PDX);
{{Afx_data_map (CUPDATEDATEDLG)
DDX_Text (PDX, idc_mystatic, M_statictext);
DDX_Text (PDX, Idc_myedit, M_edittext);
}}afx_data_map
}

//Variables

Whether the BOOL m_binitstate;//is in the initialization state (to overload onsize, it must be after the initialization has ended, otherwise an error)

DWORD m_dwstarttime;//Start time
void Gettimespace (DWORD dwstarttime,cstring & Sztime);//function for the current time difference string

The initialization function of the window

BOOL C****dlg::oninitdialog ()
{
CDialog::OnInitDialog ();

Set The icon for this dialog. The framework does this automatically
When the application ' s main window was not a dialog
SetIcon (M_hicon, TRUE); Set Big icon
SetIcon (M_hicon, FALSE); Set Small Icon

Todo:add Extra initialization here
Set timer
SetTimer (1,500,null);

This->m_binitstate = FALSE;

return TRUE; Return TRUE unless you set the focus to a control
}


The response function of the timer

void Cupdatedatedlg::ontimer (UINT nidevent)
{
Todo:add your message handler code here and/or call default
Switch (nidevent)
{
Case 1:
Timer time to
{
Calculate the time difference
This->gettimespace (This->m_dwstarttime,this->m_edittext);
This->m_statictext = this->m_edittext;


This->UpdateData(false);

}
Break
Default
Cdialog::ontimer (nidevent);
}
}

The edit control displays the response function of the en_change when the content changes

void C****dlg::onchangemyedit ()
{

This->updatedata(true);
}


UpdateData Detailed :

UpdateData (TRUE):

is to pass the control's state to its associated variable, and of course you want to associate the variable with the control.

Used to exchange data in controls on a screen into a variable.
UpdateData (FALSE):

is to pass the value of the control's associated variable to the control and change the control state.

Used to display data in the corresponding control on the screen.

Attention:

UpdateData Refreshes the current dialog box .

When using the UpdateData () function, all the bound variables in the current interface (that is, the corresponding variables added to the control via MFC ClassWizard) are updated to the contents of the corresponding control by UpdateData (TRUE), and the contents of all the controls that are bound to the variable are UpdateData (FALSE) is updated to the contents of the corresponding variable.

Important Additions :

GetWindowText () is to get the current contents of the control, which is for a single control ;
UpdateData () is a two-way channel for controls and data in the DDX data exchange mechanism that acts on the entire CWnd .

Control content Update DrawText UpdateData setwindowtext

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.