原文地址: http://cyzhou.spaces.live.com/blog/cns!C7264561BBA7DCEF!145.entry
注音假名,也就是
振り仮名 ,是学习日语的一种非常好的工具。就如同拼音是学习中文的工具一样。
其实,在微软的输入法系统中,它们都属于
Phonetics 。在MSoffice中提供了显示Phonetics的工具,就是在[格式(書式)]菜单下的注音命令(对应于A字上面有123的工具栏项目)。
1) 下面的Excel宏演示了从单元格中获取注音假名的方法:
Sub Macro1()
Set objPhon = Cells(1, 1).Phonetics
'取得注音
objPhon.CharacterType = xlHiragana '设置注音为平假名
With objPhon
For Each objPhonItem In objPhon
Cells(3, 1).Value = objPhonItem.Text '显示
Next
End With
objPhon.CharacterType = xlHiragana '设置注音为平假名
With objPhon
For Each objPhonItem In objPhon
Cells(3, 1).Value = objPhonItem.Text '显示
Next
End With
End Sub
不过,以上做法有个问题:
它仅仅显示汉字的假名,而忽略单词中本身存在的假名。比如
「私の本」就会变成 「わたしほん」
而把の丢掉了。解决的办法是:
利用PHONETIC()函数。
在excel工具栏上点函数图标(f(x))
在信息类中可以找到它。以下是在VBA中使用的例子:
Sub Macro6()
Cells(2, 2).Select
ActiveCell.FormulaR1C1 = "=phonetic(RC[1])"
Cells(2, 2).Value = StrConv(Cells(2, 2).Value, vbHiragana)
End Sub
Cells(2, 2).Select
ActiveCell.FormulaR1C1 = "=phonetic(RC[1])"
Cells(2, 2).Value = StrConv(Cells(2, 2).Value, vbHiragana)
End Sub
我用这个方法解决了为标准日本语7000个单词在excel列表中注音的工作:
Sub Macro1()
Dim i As Integer
For i = 1 To 110 ' 第1行到第110行
Cells(i, 4).Select
ActiveCell.FormulaR1C1 = "=PHONETIC(RC[-3])" ' 通过过渡单元格取出注音
Cells(i, 2).Value = StrConv(ActiveCell.Value, vbHiragana) ' 单元格内容copy到目标单元格
Dim i As Integer
For i = 1 To 110 ' 第1行到第110行
Cells(i, 4).Select
ActiveCell.FormulaR1C1 = "=PHONETIC(RC[-3])" ' 通过过渡单元格取出注音
Cells(i, 2).Value = StrConv(ActiveCell.Value, vbHiragana) ' 单元格内容copy到目标单元格
ActiveCell.Clear '
清除过渡单元格
Next i
End Sub
Next i
End Sub
2) 在.net程序中也可以取出注音假名(想必拼音也是一样)。
下面以C#为例,说明如何操作:
///////////////////////////////////// Form1.cs ///////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(104, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 40);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(104, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 40);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void CompoEvent(object sender, TextBoxEx.CompositionEventArgs e)
{
label1.Text += e.ImeStr;
}
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void CompoEvent(object sender, TextBoxEx.CompositionEventArgs e)
{
label1.Text += e.ImeStr;
}
private void Form1_Load(object sender, System.EventArgs e)
{
TextBoxEx tb = new TextBoxEx();
tb.Name = "textBoxEx1";
tb.Text = "textBoxEx1";
tb.CompositionEvent +=
new TextBoxEx.CompositionEventHandler(this.CompoEvent);
this.Controls.Add(tb);
{
TextBoxEx tb = new TextBoxEx();
tb.Name = "textBoxEx1";
tb.Text = "textBoxEx1";
tb.CompositionEvent +=
new TextBoxEx.CompositionEventHandler(this.CompoEvent);
this.Controls.Add(tb);
}
}
}
}
}
///////////////////////////////////////// TextBoxEx.cs /////////////////////////////////////////////////
using System;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
/// <summary>
///
/// </summary>
public class TextBoxEx : TextBox
{
const int WM_IME_COMPOSITION = 0x010F;
const int GCS_RESULTREADSTR = 0x0200;
{
/// <summary>
///
/// </summary>
public class TextBoxEx : TextBox
{
const int WM_IME_COMPOSITION = 0x010F;
const int GCS_RESULTREADSTR = 0x0200;
[DllImport("Imm32.dll")]
public static extern int ImmGetContext(IntPtr hWnd);
[DllImport("Imm32.dll")]
public static extern int ImmGetCompositionString(
int hIMC,
int dwIndex,
StringBuilder lpBuf,
int dwBufLen
);
[DllImport("Imm32.dll")]
public static extern bool ImmReleaseContext(IntPtr hWnd,int hIMC);
public static extern int ImmGetContext(IntPtr hWnd);
[DllImport("Imm32.dll")]
public static extern int ImmGetCompositionString(
int hIMC,
int dwIndex,
StringBuilder lpBuf,
int dwBufLen
);
[DllImport("Imm32.dll")]
public static extern bool ImmReleaseContext(IntPtr hWnd,int hIMC);
[Serializable]
public class CompositionEventArgs:EventArgs
{
public string ImeStr;
}
public delegate void CompositionEventHandler(object sender,CompositionEventArgs e);
public event CompositionEventHandler CompositionEvent;
public class CompositionEventArgs:EventArgs
{
public string ImeStr;
}
public delegate void CompositionEventHandler(object sender,CompositionEventArgs e);
public event CompositionEventHandler CompositionEvent;
protected override void WndProc(ref Message m)
{
if (CompositionEvent != null && m.Msg == WM_IME_COMPOSITION)
{
if (((int)m.LParam & GCS_RESULTREADSTR)>0)
{
int Imc = ImmGetContext(this.Handle);
int sz = ImmGetCompositionString(Imc,GCS_RESULTREADSTR,null,0);
StringBuilder str = new StringBuilder(sz);
ImmGetCompositionString(Imc,GCS_RESULTREADSTR,str,str.Capacity);
ImmReleaseContext(this.Handle,Imc);
CompositionEventArgs args = new CompositionEventArgs();
args.ImeStr = str.ToString();
CompositionEvent(this,args);
}
}
base.WndProc(ref m);
}
}
{
if (CompositionEvent != null && m.Msg == WM_IME_COMPOSITION)
{
if (((int)m.LParam & GCS_RESULTREADSTR)>0)
{
int Imc = ImmGetContext(this.Handle);
int sz = ImmGetCompositionString(Imc,GCS_RESULTREADSTR,null,0);
StringBuilder str = new StringBuilder(sz);
ImmGetCompositionString(Imc,GCS_RESULTREADSTR,str,str.Capacity);
ImmReleaseContext(this.Handle,Imc);
CompositionEventArgs args = new CompositionEventArgs();
args.ImeStr = str.ToString();
CompositionEvent(this,args);
}
}
base.WndProc(ref m);
}
}
}