How to Implement Ajax functions in YC ++
Sometimes, when a user clicks a button on a web page, only a small part of the content will change. Before using Ajax, you must refresh the entire page. You must reread the changed or unchanged page from the server. After Ajax is used, you only need to read the changed part from the server. This not only reduces the waiting time, but also saves network bandwidth resources.
The Ajax function can also be implemented in YC ++. The following example illustrates its principles and usage.
First, create a main window, create an HTML window in the main window, and transfer the webpage rdweb.htm to the HTML window.
Enter the complete URL in the input box of the interface, and click "read webpage" to read the specified URL text into the editing box.
This means that each time the URL is changed and the button is pressed, only the content in the edit box is changed. Therefore, you only need to re-read the content in the edit box, but not the other content.
The following example implements this function, thus implementing the Ajax function.
/*************************************** **************************************** **********************************/
Save the following code to a file named after, such as Ajax. cpp.
In YC ++, use <file/open or create CPP source program> to call Ajax. cpp, and then use <tool/execute> to run Ajax. cpp.
Or in DOS, use ycc Ajax. cpp to generate ajax.exe, and then run ajax.exe
In VC ++, use Cl Ajax. cpp to generate ajax.exe, and then run ajax.exe
//////////////////////////////////////// //////////////////////////////////////// ////////////////////////////
//////////////////////////////////////// //////////////////////////////////////// ////////////////////////////
# Ifndef ycc
# Include <windows. h>
# Include "include/ycapi. H"
# Pragma comment (Lib, "gdi32.lib ")
# Pragma comment (Lib, "user32.lib ")
# Pragma comment (Lib, "yxbapi. lib ")
# Endif
//////////////////////////////////////// //////////////////////////////////////// ////////////////////////////
Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd)
{
Int winapi mainwndproc (hwnd, uint iMessage, uint wparam, long lparam, void * puserdata );
Hwnd = yxb_window (mainwndproc, null, 0, // create the main window. Use the YC ++ API function to create it. It does not need to be registered.
Ws_overlappedwindow | ws_caption | ws_sysmenu,
"YC ++ implements Ajax functions", 800,600, null, wt_win );
MSG;
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Return 0;
}
Int winapi mainwndproc (hwnd, uint iMessage, uint wparam, long lparam, void * puserdata)
{
Static hwnd hwndhtml;
Switch (iMessage)
{
Case wm_create:
Hwndhtml = yxb_window (null, 0, ws_ex_clientedge, 0, null, 0, 0, 0, hwnd, wt_html); // create an HTML window
Web_page apage;
Memset (& apage, 0, sizeof (apage ));
Apage. href = "rdweb.htm ";
Sendmessage (hwndhtml, wm_command, web_a, (INT) & apage); // call the webpage
Return false;
Case wm_size:
Rect mrect;
Getclientrect (hwnd, & mrect );
Movewindow (hwndhtml, 0, 0, mrect. Right, mrect. Bottom, true );
Return true;
Case wm_destroy:
Postquitmessage (0 );
Return false;
}
Return defwindowproc (hwnd, iMessage, wparam, lparam );
}
/*************************************** **************************************** **********************************/
/*************************************** **************************************** **********************************/
Upload the following HTML text to the specified file: rdweb.htm
/*************************************** **************************************** **********************************/
<Body bgcolor = #556667>
Enter the URL:
<Input id = myin value = "www.sohu.com"
Style = "font-size: 28; width = 480; color: red; Border: 4 Green solid; Background-color: RGB (185,185,185);">
<Button onclick = "but_click ()"> Read webpage </button>
<Textarea id = htmltext style = "width = 90%; Height = 470; Background-color: RGB (185,185,185);"> </textarea>
<! ---- Javascript script ----------------------------------------------------------------------->
<Script language = JavaScript>
Function set_text (s) // sets the text box
{
Htmltext. value = Unescape (s );
}
Function but_click () // click the button to execute this function.
{
Read_web_file (myin. Value); // read the webpage file in the URL
}
</SCRIPT>
<! ---- C/C ++ script ------------------------------------------------------------------------------>
<Script language = ycscript>
# Define YCB
Hwnd htmlwnd;
Void main (yarg * parg)
{
Htmlwnd = parg-> hwnd;
}
Void read_web_file (char * fileptr)
{
Char * srcbuf = NULL;
Int Glen = 0;
// Use wininet to read files in the website (fileptr). If you are interested, you can use socket to read files.
Hinternet his, hic, hir;
His = internetopen ("httpget", internet_open_type_direct, null, null, 0 );
Hic = internetconnect (his, fileptr, internet_default_http_port, null, null, internet_service_http, 0, 0 );
Hir = httpopenrequest (HIC, null, "", null, 0, 0 );
If (httpsendrequest (HIR, null, 0, null, 0 ))
{
# Define onetime_bytes 4096
DWORD dwbytes;
For (INT rdlen = 0 ;;)
{
If (Glen + onetime_bytes> = rdlen)
{
Srcbuf = (char *) realloc (srcbuf, rdlen + onetime_bytes );
Rdlen + = onetime_bytes;
}
Internetreadfile (HIR, & srcbuf [Glen], onetime_bytes, & dwbytes );
Glen + = dwbytes;
If (! Dwbytes) break;
}
}
Internetclosehandle (HIC );
Internetclosehandle (HIS );
If (! Srcbuf) // No data is read
{
MessageBox (htmlwnd, fileptr, "No server found! ", Mb_iconhand | mb_ OK );
Return;
}
Char * desbuf, * tempbuf;
Glen = yxb_escape (& desbuf, srcbuf, Glen); // convert the text so that it does not contain quotation marks
Tempbuf = (char *) malloc (Glen + 128 );
Sprintf (tempbuf, "set_text ('% s')", desbuf );
Yxb_runjscriptex (htmlwnd, 0, tempbuf); // execute the JavaScript function set_text (s) in the C/C ++ code)
Free (tempbuf );
Free (desbuf );
Free (srcbuf );
}
</SCRIPT>
/*************************************** **************************************** **********************************/
In this example, the following files of YC ++ must be written into the directory of the source program:
Yc01/yxbapi. dll C/C ++ compiler, browser kernel Library
Yc01/yxbimg. dll image, animation decoding library
Yc01/yxbext. dll browser kernel interaction code library
Yc01/YC. Ah C/C ++ compiler header package
Yc01/YC. cmp c/C ++ compiler error message
Yc01/include/ycapi. h header files required to run this example using other compilers
Yc01/include/yxbapi. lib use other compilers to run the required library files, such as vc6.0