如何以适当的编码显示文本
本文关键字:编码 显示 文本 | 更新日期: 2023-09-27 18:06:04
我的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代码页中显示?谢谢。
基本上,不是以您希望的方式,或者实际上,从性能的角度来看,以一种有意义的方式。
。Net控件是Unicode,并且接受Unicode字符串。
您可以读取1252中的File并从中获取字符串,就像这里演示的SO问题一样:
如何从可能的Windows 1252 'ANSI'编码上传文件到UTF8在。net ?
或在1251:
将Latin 1编码的UTF8转换为Unicode
但是在Label控件中获取值并不是一个好方法。
你应该把你的源文本转换成Unicode,让它都是原生的。
如果你打算支持不同的语言,那么你可能也想研究一下使用。net RESX系统。
很难确切地说出这里发生了什么:是程序不正确地显示文本,还是只是VS?
我猜这只是VS的问题,你的文本是在正确的编码,但它没有显示正确的编码VS
你可以强制VS在VS中显示特定编码的文本文件,如下所示:
右键单击file -> Open With…-> CSharp编辑器与编码->{选择编码}
除非你知道你的文件是用什么编码,否则你将不得不做试错。有几个可以试试:
(自动检测)Unicode (UTF-8无签名)- Codepage 65001
Unicode -码页1200
然而,你的文件可能不是Unicode,也许是ANSI,这意味着你将不得不选择一个Codepage,即。"语言"来显示它。你知道这是什么语言吗?
如果这些都失败了,也许你可以把文件附加到某个地方,或者指出在哪里可以下载,然后我们可以看看
OK。Bitaweb.com/en/codeConverter.html帮不了我。然后我在记事本中打开我的代码script.cs,保存在UTF-8中,问题消失了。整个问题都在c#编辑器中,它只保留了ANSI,而不是UTF-8。