如何以正确的编码显示文本?

时间:2011-08-20 00:15:08

标签: c#

我的c#剧本:

using System;
using System.Text;
using System.Drawing;  
using System.IO;
using System.Windows.Forms;
using fomm.Scripting;


class Script : FalloutNewVegasBaseScript {
public static bool install;

// PNH MENU SETTINGS
public static Form installPNHForm; 
public static PictureBox backgroundPicture; 
public static Button okButton;  
public static Button cancelButton;

// Options buttons  

public static RadioButton ReadiusRadioButton;  
public static Label vanillaIconsLabel;  

public static RadioButton Pipboy2005RadioButton;  
public static Label pnhLabel;  

//Options panel
public static Panel optionsPanel;
public static Label optionsLabel;

public static bool IsPluginActive(String pluginName) {      
    string[] loadOrder = GetActivePlugins();  
    for (int i = 0; i < loadOrder.Length; ++i) {  
        if (loadOrder[i].Equals(pluginName, StringComparison.InvariantCultureIgnoreCase)) {
             return true;  
        }
    }  

    return false;  
} 


public static Image GetImageFromFomod(string filename) {  
    byte[] data = GetFileFromFomod(filename);  
    MemoryStream s = new MemoryStream(data);  
    Image img = Image.FromStream(s);  
    s.Close();  

    return img;  
}  

public static void CreatePNHForm() {    
    setUpPNHForm();

    setUpPNHBackgroundImage();

    setUpPNHHelpertext();

    setUpOptions();

    setUpButtons();

    AttachHandlers();  
}

 public static void setUpOptions() {        
    optionsPanel = new Panel();
    optionsPanel.Location = new Point(10, 180);  
    optionsPanel.Size = new Size(267, 50);  
    optionsPanel.BackColor = Color.WhiteSmoke; 
    optionsPanel.BorderStyle = BorderStyle.FixedSingle;  
    backgroundPicture.Controls.Add(optionsPanel);

    ReadiusRadioButton = new RadioButton();
    ReadiusRadioButton.Checked = true;  
    ReadiusRadioButton.Location = new Point(15, 15);  
    ReadiusRadioButton.Size = new Size(20, 20);  
    vanillaIconsLabel = new Label();  
    vanillaIconsLabel.Text = "�����"; 
    vanillaIconsLabel.Location = new Point(37, 19);  
    vanillaIconsLabel.AutoSize = true;  
    optionsPanel.Controls.Add(ReadiusRadioButton);  
    optionsPanel.Controls.Add(vanillaIconsLabel);

    Pipboy2005RadioButton = new RadioButton();  
    Pipboy2005RadioButton.Checked = false;  
    Pipboy2005RadioButton.Location = new Point(168, 16);  
    Pipboy2005RadioButton.Size = new Size(20, 20);  
    pnhLabel = new Label();  
    pnhLabel.Text = "Pipboy2005";  
    pnhLabel.Location = new Point(190, 19);  
    pnhLabel.AutoSize = true;  
    optionsPanel.Controls.Add(Pipboy2005RadioButton);  
    optionsPanel.Controls.Add(pnhLabel);

}

public static void setUpPNHHelpertext() {
    Panel helperTextPanel = new Panel();
    helperTextPanel.Location = new Point(60, 130);  
    helperTextPanel.Size = new Size(170, 30);  
    helperTextPanel.BackColor = Color.WhiteSmoke; 
    helperTextPanel.BorderStyle = BorderStyle.FixedSingle;  
    backgroundPicture.Controls.Add(helperTextPanel);

    Label helperTextLabel = new Label();   
    helperTextLabel.Text = "Readius or Pipboy 2005?";
    helperTextLabel.Location = new Point(22, 5);  
    helperTextLabel.AutoSize = true; 
    helperTextPanel.Controls.Add(helperTextLabel); 
}

public static void setUpPNHForm() {
    installPNHForm = CreateCustomForm(); 

    installPNHForm.FormBorderStyle = FormBorderStyle.Fixed3D;
    installPNHForm.StartPosition = FormStartPosition.CenterScreen;
    installPNHForm.MaximizeBox = false;
    installPNHForm.Size = new System.Drawing.Size(300, 360); 
    installPNHForm.Text = "Readius vs Pipboy 2005";
}

public static void setUpPNHBackgroundImage() {
    backgroundPicture = new PictureBox();  
    backgroundPicture.Location = new Point(0, 0);  
    backgroundPicture.Size = new Size(292, 327);  

    // load picture file from .fomod and stream into picture box  


    // add BackgroundPicture to list of controls  
    installPNHForm.Controls.Add(backgroundPicture);
}


public static void setUpButtons() {
    okButton = new Button();  
    okButton.Text = "Install";  
    okButton.BackColor = Color.Silver;  
    okButton.Location = new Point(148, 286);  
    okButton.Size = new Size(130, 33);  

    // cancel button  
    cancelButton = new Button();  
    cancelButton.Text = "Cancel";  
    cancelButton.BackColor = Color.Silver;  
    cancelButton.Location = new Point(9, 286);  
    cancelButton.Size = new Size(130, 33);  

    backgroundPicture.Controls.Add(okButton);  
    backgroundPicture.Controls.Add(cancelButton);     
}



public static void AttachHandlers() {  

    //Attach a handler that will fire when the apply button is clicked  
    okButton.Click += delegate(object sender, EventArgs args) {  
        install = true;  
        installPNHForm.Close(); 
    };  

    cancelButton.Click += delegate(object sender, EventArgs args) {  
        install = false;  
        installPNHForm.Close();  
    };  
} 


public static bool OnActivate() {
    CreatePNHForm();
    installPNHForm.ShowDialog(); 

    if (install) {
        InstallMenu();
    }

    return install;
}

public static int GetPluginIndex(String pluginName)
{
string[] loadOrder = GetAllPlugins();
for (int i = 0; i < loadOrder.Length; ++i)
{
    if (loadOrder[i].Equals(pluginName, StringComparison.InvariantCultureIgnoreCase))
        return i;
}
return -1;
}
public static void PlaceAtPlugin(String targetPlugin, String pluginToMove, int offset)
{
int targetPluginIndex = GetPluginIndex(targetPlugin);
int pluginToMoveIndex = GetPluginIndex(pluginToMove);
if (targetPluginIndex != -1 && pluginToMoveIndex != -1)
{
    int[] pluginIdxArray = { pluginToMoveIndex };
    SetLoadOrder(pluginIdxArray, targetPluginIndex + offset);
}
}
public static void PlaceAfterPlugin(String targetPlugin, String pluginToMove)
{
PlaceAtPlugin(targetPlugin, pluginToMove, 1);
}
public static void PlaceBeforePlugin(String targetPlugin, String pluginToMove)
{
PlaceAtPlugin(targetPlugin, pluginToMove, 0);
}

public static void InstallMenu() {

    if (ReadiusRadioButton.Checked) {
        CopyDataFile("Readius_NV.bsa", "Readius_NV.bsa");
        InstallFileFromFomod("Readius_NV.esp");
        if (IsPluginActive("WMX-ArenovalisTextures.esp")) 
    {
        PlaceAfterPlugin("WMX-ArenovalisTextures.esp", "Readius_NV.esp");
    }
        if (IsPluginActive("TheUltimateGhostOverhaul.esp")) 
    {
        PlaceAfterPlugin("TheUltimateGhostOverhaul.esp", "Readius_NV.esp");
    }
        SetPluginActivation("Readius_NV.esp", true);
    } else if (Pipboy2005RadioButton.Checked) {
        CopyDataFile("Pipboy2500.bsa", "Pipboy2500.bsa");
        InstallFileFromFomod("Pipboy2500.esp");
        if (IsPluginActive("WMX-ArenovalisTextures.esp")) 
    {
        PlaceAfterPlugin("WMX-ArenovalisTextures.esp", "Pipboy2500.esp");
    }
        if (IsPluginActive("TheUltimateGhostOverhaul.esp")) 
    {
        PlaceAfterPlugin("TheUltimateGhostOverhaul.esp", "Pipboy2500.esp");
    }
        SetPluginActivation("Pipboy2500.esp", true);
    }   

}

}

“vanillaIconsLabel.Text =”????“;”,如何在1251代码页中显示? 感谢。

3 个答案:

答案 0 :(得分:0)

基本上,不是以你喜欢的方式,或者从表现的角度来看,以一种有意义的方式。

.Net控件是Unicode,并接受Unicode字符串。

可以在1252年的文件中读取并从中获取一个字符串,就像这里的SO问题所示:

How do I convert from a possibly Windows 1252 'ANSI' encoded uploaded file to UTF8 in .NET?

或在1251年:

Convert Latin 1 encoded UTF8 to Unicode

但这不是将值转换为Label控件的好方法。

您应该将源文本转换为Unicode,并让它们都是原生的。

如果您打算考虑支持不同的语言,那么您可能也想要使用.net RESX系统进行调查。

答案 1 :(得分:0)

很难确切地说出这里发生了什么:程序是否显示文字错误,还是只是VS?

我猜这只是VS的一个问题,而且你的文本编码正确,但VS中没有显示正确的编码。

您可以强制VS在VS中以特定编码显示文本文件,如下所示:

  

右键单击文件 - &gt;打开... - &gt;带编码的CSharp编辑器 - &gt; {选择编码}

除非您知道文件的编码是什么,否则您将不得不逐个进行试错。一些尝试:

  

(自动检测)
  Unicode(UTF-8无签名) - 代码页65001
  Unicode - 代码页1200

但是,您的文件可能不是Unicode,也许是ANSI,这意味着您必须选择一个代码页,即“语言”来显示它。您对这种语言有什么想法吗?

如果所有其他方法都失败了,也许您可​​以将文件附加到某处,或指出可以下载的位置,那么我们可以看看。

答案 2 :(得分:0)

行。 Bitaweb.com/en/codeConverter.html没有帮助我。然后我在记事本中打开我的代码script.cs,以UTF-8保存,问题就消失了。整个问题在C#编辑器中,它只保留ANSI,而不是UTF-8。

相关问题