5/5 (13 avis)
Snippet vu 12 944 fois - Téléchargée 38 fois
Private Function ConvertToHTML(ByVal Box As RichTextBox) As String Dim strHTML As String Dim strColeur As String Dim blnGras As Boolean Dim blnItalic As Boolean Dim strPolice As String Dim shtTaille As Short Dim lngDepartOriginal As Long Dim lngTailleOriginal As Long Dim intCount As Integer ' Si le text est vide, on sort If Box.Text.Length = 0 Then Exit Function ' Conserver la selection originale, et selectionné le debut lngDepartOriginal = 0 lngTailleOriginal = Box.TextLength Box.Select(0, 1) ' Entete HTML strHTML = "<HTML>" ' Récuperer les parametres initaux strColeur = Box.SelectionColor.ToKnownColor.ToString blnGras = Box.SelectionFont.Bold blnItalic = Box.SelectionFont.Italic strPolice = Box.SelectionFont.FontFamily.Name shtTaille = Box.SelectionFont.Size ' Inclure le premier parametre HTML "Style" strHTML += "<SPAN style=""font-family: " & strPolice & _ "; font-size: " & shtTaille & "pt; color: " _ & strColeur & """>" ' Inclure le TAg GRAS si besoin est If blnGras = True Then strHTML += "<B>" End If ' Inclure le TAg ITALIQUE si besoin est If blnItalic = True Then strHTML += "<I>" End If ' Finalement on attaque le premier caractère strHTML += Box.Text.Substring(0, 1) ' Boucle sur le reste du texte For intCount = 2 To Box.Text.Length ' Selection du caractere Box.Select(intCount - 1, 1) ' Verifier et implementer si necessaire un changement de style If Box.SelectionColor.ToKnownColor.ToString <> strColeur _ Or Box.SelectionFont.FontFamily.Name <> strPolice _ Or Box.SelectionFont.Size <> shtTaille Then strHTML += "</SPAN><SPAN style=""font-family: " _ & Box.SelectionFont.FontFamily.Name & _ "; font-size: " & Box.SelectionFont.Size & _ "pt; color: " & _ Box.SelectionColor.ToKnownColor.ToString & """>" End If ' Verifier changement GRAS If Box.SelectionFont.Bold <> blnGras Then If Box.SelectionFont.Bold = False Then strHTML += "</B>" Else strHTML += "<B>" End If End If ' Verifier changement ITALIQUE If Box.SelectionFont.Italic <> blnItalic Then If Box.SelectionFont.Italic = False Then strHTML += "</I>" Else strHTML += "<I>" End If End If ' Ajouter le caractere Select Case Box.Text.Substring(intCount - 1, 1) Case ControlChars.Lf ' Si c'est un LineFeed, mettre la borne <BR> strHTML += "<BR>" Case " " strHTML += " " Case ControlChars.Tab strHTML += " " Case Else strHTML += System.Web.HttpUtility.HtmlEncode(Box.Text.Substring(intCount - 1, 1)) End Select ' Mise a jour du style courant strColeur = Box.SelectionColor.ToKnownColor.ToString blnGras = Box.SelectionFont.Bold blnItalic = Box.SelectionFont.Italic strPolice = Box.SelectionFont.FontFamily.Name shtTaille = Box.SelectionFont.Size Next ' Fermer les Tag <B> <I> si necessaire If blnGras = True Then strHTML += "</B>" If blnItalic = True Then strHTML += "</I>" ' Fermer le style et la page HTML strHTML += "</SPAN></HTML>" ' Restorer la selection d'origine Box.Select(lngDepartOriginal, lngTailleOriginal) ' Retourner le code HTML Return strHTML End Function
26 mars 2015 à 23:56
17 oct. 2008 à 19:45
17 oct. 2008 à 19:44
Voila traduction en C# pour les faignant :)
public static string RichToHtml(RichTextBox Box)
{
string strHTML="";
string strColeur;
bool blnGras;
bool blnItalic;
string strPolice;
short shtTaille;
HorizontalAlignment hzAlign;
string strHzAlign="";
int lngDepartOriginal;
int lngTailleOriginal;
int intCount;
if (Box.Text.Length == 0) return strHTML;
lngDepartOriginal = 0;
lngTailleOriginal = Box.TextLength;
Box.Select(0, 1);
//Entete HTML
strHTML = "<HTML>";
strColeur = Box.SelectionColor.ToKnownColor().ToString();
blnGras = Box.SelectionFont.Bold;
blnItalic = Box.SelectionFont.Italic;
strPolice = Box.SelectionFont.FontFamily.Name;
hzAlign = Box.SelectionAlignment; if (hzAlign HorizontalAlignment.Left) strHzAlign "Left"; else if (hzAlign HorizontalAlignment.Center) strHzAlign "Center"; else if (hzAlign HorizontalAlignment.Right) strHzAlign "Right";
shtTaille = Convert.ToInt16( Box.SelectionFont.Size);
strHTML += "
";
if (blnGras)
{
strHTML += "";
}
if (blnItalic)
{
strHTML += "";
}
strHTML += Box.Text.Substring(0, 1);
//Boucle sur le reste du texte
for (intCount =2;intCount<=Box.Text.Length;intCount++) {
//Selection du caractere
Box.Select(intCount - 1, 1);
//Verifier et implementer si necessaire un changement de style
if ((Box.SelectionColor.ToKnownColor().ToString() != strColeur) || (Box.SelectionFont.FontFamily.Name != strPolice) || (Convert.ToInt16(Box.SelectionFont.Size) != shtTaille) || (Box.SelectionAlignment != hzAlign))
{ if (Box.SelectionAlignment HorizontalAlignment.Left) strHzAlign "Left"; else if (Box.SelectionAlignment HorizontalAlignment.Center) strHzAlign "Center"; else if (Box.SelectionAlignment HorizontalAlignment.Right) strHzAlign "Right";
strHTML += "
" + Environment.NewLine + "
";
}
// Verifier changement GRAS
if (Box.SelectionFont.Bold != blnGras)
{
if (Box.SelectionFont.Bold == false)
{
strHTML += "";
}
else
{
strHTML += "";
}
}
//Verifier changement ITALIQUE
if (Box.SelectionFont.Italic != blnItalic)
{
if (Box.SelectionFont.Italic == false)
{
strHTML += "";
}
else
{
strHTML += "";
}
}
//Ajouter le caractere
switch (Box.Text.Substring(intCount - 1, 1))
{
case "\n":
strHTML += "
";
break;
case " ":
strHTML += " ";
break;
case "\t":
strHTML += " ";
break;
default:
strHTML += System.Web.HttpUtility.HtmlEncode(Box.Text.Substring(intCount - 1, 1));
break;
}
//Mise a jour du style courant
strColeur = Box.SelectionColor.ToKnownColor().ToString();
blnGras = Box.SelectionFont.Bold;
blnItalic = Box.SelectionFont.Italic;
strPolice = Box.SelectionFont.FontFamily.Name;
shtTaille = Convert.ToInt16(Box.SelectionFont.Size);
hzAlign = Box.SelectionAlignment;
}
//Fermer les Tag si necessaire
if (blnGras) strHTML += "";
if (blnItalic) strHTML += "";
//Fermer le style et la page HTML
strHTML += "
</HTML>";
//Restorer la selection d'origine
Box.Select(lngDepartOriginal, lngTailleOriginal);
return strHTML;
}
15 mars 2008 à 09:55
8/10
5 sept. 2005 à 23:48
Peut-on facilement transposer le code en VB6 ?
Sûrement en utilisant quelques API, mais lesquelles ?
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.