65.9K
CodeProject 正在变化。 阅读更多。
Home

HTML 和 CSS C++ 类

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.95/5 (6投票s)

2008 年 1 月 27 日

CPOL
viewsIcon

29458

downloadIcon

426

使在 C/C++ 程序中轻松处理 HTML 和 CSS

引言

我创建这些类是为了能够在我的程序中轻松处理 HTML 和 CSS。但因为它们对我来说非常有用,我想它们对其他人也可能有用。这不是一个教程,所以代码、我的文档和快速示例就足够了。如果您在我的文章或代码中发现任何错误,请随时告诉我。

使用代码

HTML 类文档

BOOL newdoc();
// Allocates necessary memory, and initializes variables.
// Call this before any other class functions.


void deldoc(); // Frees allocated memory. // Call this when you have finished with the class. // You may bring your class back on-line with another call to newdoc().

BOOL div(char *arga, char *argb); // Adds an HTML tag section (like 'body' or 'table'). // arga - name of the tag. // argb - the tag's parameters, can be ignored. // To end a tag started by this function, call it again with no arguments.

BOOL tag(char *arga, char *argb); // Adds a HTML empty tag (like 'img' or 'br'). // arga - name of the tag. // argb - the tag's parameters, can be ignored.

BOOL extmem(DWORD arga, DWORD argb); // Extends the allocated buffer. // arga - amount of bytes to be extended. // argb - amount of bytes that have to be in use in order to extended, can be ignored.

char *text(); // Returns the complete HTML text buffer.

BOOL raw(char *arga); // Adds raw text to the HTML text. Call this to add text or special tags.
CSS 类文档
BOOL newdoc();
// Same as in the HTML class.


void deldoc(); // Same as in the HTML class.

BOOL div(char *arga); // Adds an CSS style section (like 'body' or '#myclass'). // arga - name of the style section. // To end a section started by this function, call it again with 0 as the first argument. // If you start a section without ending the previous one, the class will automatically end it for you.

BOOL tag(char *arga, char *argb); // Adds a CSS style attribute (like 'background' or 'border'). // arga - name of the attribute. // argb - the attribute's value. // A call to tag() will fail if called outside of a style section.

BOOL extmem(DWORD arga, DWORD argb); // Same as in the HTML class.

char *text(); // Same as in the HTML class.
所有布尔函数都返回 TRUE 或 FALSE,具体取决于它们的成功与否。

如何使用 HTML 类的一个示例
HTML html; // Declare our class
html.newdoc(); // Start it up
html.raw("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); // A special tag
html.div("html"); // Start the html page
html.div("head"); // Start the header
html.tag("meta", "http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\""); // A meta tag
html.div("title"); // Start the title
html.raw("Hello World"); // Our title
html.div(); // End the title
html.tag("link", "rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" media=\"screen\""); // Link to a CSS file
html.div(); // End the header
html.div("body"); // Start the body
html.raw("Hello World."); // Print our body's text
html.tag("br"); // Make a break
html.div(); // End the body
html.div(); // End the html page
SetWindowText(hChild, html.text()); // Set a edit control to contain the html document we just compiled.
html.deldoc(); // End our stuff
如何使用 CSS 类的一个示例
CSS css; // Declare our class
css.newdoc(); // Start it up
css.div("body"); // We are going to edit the body's style.
css.tag("background-color", "#2f2f2f"); // set the background color
css.tag("font-size", "1.01em"); // set the font size
css.tag("margin", "0"); // set the page's margin
css.tag("padding", "0"); // set the page's padding
css.div("table"); // stop editing the body's style, and start editing the table's style
css.tag("background-color", "#010101"); // set the table color
css.div(); // stop editing the table's style.
SetWindowText(hChild, css.text()); // Set a edit control to contain the css document we just compiled.
css.deldoc(); // End our stuff
HTMLCSS 类的一个很好的特性是,它们会自动缩进,因此手动编辑结果或查看结果时,您会发现它非常易于管理。

© . All rights reserved.