如果消除TextBox最后一个回车???

发财蛇 2005-09-05 02:05:06
textbox的Multiline属性为true
在 textbox 中敲入回车跳转到下一个control,在OnKeysPress事件中写入了相应的代码
但是当敲入回车后,它是先在textbox中先回车了一下,再跳转,因此,在textbox.text后面其实多了一个回车,请问该如何将回车消除,或者屏蔽掉这个回车??
...全文
376 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
MyLf 2005-09-06
  • 打赏
  • 举报
回复
在KeyPress事件中:
if(e.KeyChar==(char)13)e.Handled=true
xamaizi 2005-09-06
  • 打赏
  • 举报
回复
textBox1.Text.TrimEnd('\r\n')
寒星 2005-09-06
  • 打赏
  • 举报
回复
[DllImport("user32.dll")]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers,
uint vk);

[DllImport("user32.dll")]
static extern bool UnregisterHotKey(IntPtr hWnd, int id);

private const int VK_RETURN = 0xD;

private void Form1_Load(object sender, System.EventArgs e)
{
RegisterHotKey(textBox1.Handle, 1, 0, VK_RETURN);
}

private void Form1_Closed(object sender, System.EventArgs e)
{
UnregisterHotKey(textBox1.Handle, 1);
}

private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
if(richTextBox1.Text.Length != 0)
{
richTextBox1.Text += "\r\n";
}
richTextBox1.Text += textBox1.Text;
}
}

that's ok.
cdo 2005-09-05
  • 打赏
  • 举报
回复
textBox1.Text = textBox1.Text.TrimEnd('\n');
weisai 2005-09-05
  • 打赏
  • 举报
回复
按回車時,你是想執行“OnKeysPress事件中写入了相应的代码”,還是不執行這段代碼
JasonHeung 2005-09-05
  • 打赏
  • 举报
回复
textbox的Multiline属性为true
改成false.
wuyi8808 2005-09-05
  • 打赏
  • 举报
回复
textBox1.Text = textBox1.Text.TrimEnd('\r', '\n')
我不懂电脑 2005-09-05
  • 打赏
  • 举报
回复
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 2);
JzeroBiao 2005-09-05
  • 打赏
  • 举报
回复
在KEYDOWN事件下做

----------------------------------------------
<script>
function onTest()
{
if (event.keyCode == "13")
{
alert(document.all["test"].value);
}
}
</script>
<textarea id="test" onKeydown="onTest();"></textarea>
burningfire_wqf 2005-09-05
  • 打赏
  • 举报
回复
路过,帮你顶一下
pupo 2005-09-05
  • 打赏
  • 举报
回复
如果希望使用 ENTER 键激活特殊的按钮,可从 TextBox 派生一个类,并在发生 KeyPress 事件时为 ENTER 键提供事件处理代码。
没法下载,到这里折腾一把试试。 本文由abc2253130贡献 doc文档可能在WAP端浏览体验不佳。建议您优先选择TXT,或下载源文件到本机查看。 C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“值类型”和“引用类型”。值类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 型、接口类型、委托类型和数组类型。 变量的类型声明 变量的类型声明 每个变量必须预先声明其类型。如 int a; int b = 100; float j = 4.5; string s1; 用 object 可以表示所有的类型。 预定义类型 下表列出了预定义类型,并说明如何使用。 类型 object 说明 所有其他类型的最终 基类型 字符串类型; 字符串是 Unicode 字符序列 8 位有符号整型 16 位有符号整型 32 位有符号整型 64 位有符号整型 示例 object o = null; 范围 string sbyte short int long string s = "hello"; sbyte val = 12; short val = 12; int val = 12; long val1 = 12; -128 到 127 -32,768 到 32,767 -2,147,483,648 2,147,483,647 -9,223,372,036,854,775,808 到 第1页 C#(WINFORM)学习 long val2 = 34L; 到 9,223,372,036,854,775,807 byte ushort 8 位无符号整型 16 位无符号整型 byte val1 = 12; ushort val1 = 12; uint val1 = 12; uint 32 位无符号整型 uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong 64 位无符号整型 ulong val3 = 56L; ulong val4 = 78UL; float 单精度浮点型 float val = 1.23F;7 位 double val1 = 1.23; double 双精度浮点型 double val2 = ±5.0 × 10?324 ±1.7 × 10 308 0 到 255 0 到 65,535 0 到 4,294,967,295 0 到 18,446,744,073,709,551,615 ±1.5 × 10?45 ±3.4 × 10 38 到 到 4.56D;15-16 布尔型;bool 值或为 真或为假 字符类型;char 值是 一个 Unicode 字符 精确的小数类型, 具有 28 个有效数字 bool val1 = true; bool val2 = false; char val = 'h'; decimal val = bool char decimal DateTime ±1.0 × 10?28 ±7.9 × 10 28 到 1.23M;28-29 变量转换 简单转换: float f = 100.1234f; 可以用括号转换: short s = (short)f 也可以利用 Convert 方法来转换: string s1; s1=Convert.ToString(a); MessageBox.Show(s1); 常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 Convert.ToSByte Convert.ToSingle Convert.ToString Convert.ToUInt16 Convert.ToUInt32 Convert.ToUInt64 备注 Math 类 常用科学计算方法: C# Math.Abs Math.Sqrt Math.Round Math.Floor Math.Cos Math.Sin Math.Tan Math.Exp Math.Log Math.Pow(x,y) Math.Max(x,y) 备注 绝对值 开方 取整,四舍五入 取整,放弃小数 余弦 正弦 正切 返回 e 的指定次幂 对数 数字 x 的 y 次幂 返回较大者 第3页 C#(WINFORM)学习 Math.Min(x,y) 返回较小者 枚举型 一般为字符串,可以定义带数字的枚举型,示例为: enum Color { Red=1, Blue=2, Green=3 } class Shape { public int Fill(Color color) { int ii; switch(color) { case Color.Red: ii=10; break; case Color.Blue: ii=11; break; case Color.Green: ii=12; break; default: ii=-1; break; } return ii; } } private void button1_Click(object sender, System.EventArgs e) { int i; Shape s1=new Shape(); i=s1.Fill((Color)2); //i=s1.Fill(Color.Blue); MessageBox.Show(i.ToString()); 第4页 C#(WINFORM)学习 } Enum 需要放在 class 外面,才能被其它 class 的程序调用。 C#关键字 关键字 abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new struct null switch object this operator throw out true override try params typeof private uint protected ulong public unchecked readonly unsafe ref ushort return using sbyte virtual sealed volatile short void sizeof while stackalloc static string 数组 定义 数组是一种排列有序的数据结构,包含于数组中的变量被称为数组的元素, 它们都有相同的类型。 数组声明 int [] array1 = new int[5]; int [,,] array3 = new int[10,20,30]; int [] array1 = new int[] {1,2,4}; 第5页 C#(WINFORM)学习 数组引用 array1[0]="a1"; 注意,如果定义数组为 int[5] ,则从 0~4。 数组长度 line0.GetLength(1) 数组赋值 可以从一个已经赋值的数组 array2 向未赋值的同等数组 array1 赋值,用 array1=array2; 这时,array1 就变成和 array2 一样的数组了。 集合 集合的使用 集合可以看成是可以随意添加的数组,因此凡是在使用数组的场合,都可以 使用集合。而且集合的元素可以是任意对象,操作也比数组灵活的多。 使用集合时,必须注意集合的生命期问题。如果有两个集合 L1 和 L2,使用 了 L1=L2; 后,只要 L2 生命期没有终结,它的以后的变化就可能会影响到 L1 的数值。因 此在赋值后应该及时销毁或者初始化 L2,以免发生不可预见的错误。 比较 使用 Contains 方法。 ArrayList Array1=new ArrayList(); Array1.Add("as"); bool b1=Array1.Contains("as"); MessageBox.Show(b1.ToString()); 第6页 C#(WINFORM)学习 找到集合中数量最多的一个元素 利用方法来查找,可以返回两个变量。 object Jmax0(ArrayList v11,ref int jj) { int i; object j0=0; ArrayList y11=new ArrayList(); //各个不同的元素的集合 int [] y12=new int[v11.Count]; //记录各个元素数量的数组 int xmax=0; //最大的一个元素的数量 for (i=0;i 10) if (y > 20) Console.Write("Statement_1"); else Console.Write("Statement_2"); 关系运算符 <,,>= 等于:== 不等于:!= 判断字符串 string 和 char 用 Equals 方法。 逻辑运算符 与:a & b 或:a | b 第8页 C#(WINFORM)学习 非:! A 模数运算符 模数运算符 (%) 计算第二个操作数除第一个操作数后的余数。所有数值类 型都具有预定义的模数运算符。如 Console.WriteLine(5 % 2); Console.WriteLine(-5 % 2); Console.WriteLine(5.0 % 2.2); Console.WriteLine(-5.2 % 2.0); // =1 // =-1 // =0.6 // =-1.2 经常用模数运算符来判断整数为奇数(=1)或偶数(=0) 。 循环 无条件循环 int sum,x; sum=0; for(x=1;x<=100;x++) { sum+=x; } 有条件循环 private void button1_Click(object sender, System.EventArgs e) { int sum=0; int x=0; while ((sum<100) & (x<20)) { x++; sum+=x; } string s2=Convert.ToString(x); MessageBox.Show(s2); } 运行显示 14。 第9页 C#(WINFORM)学习 如果改为 while ((sum<100) | (x<20)) 运行显示 20。 多重选择 switch (i) { case 0: CaseZero(); break; case 1: CaseOne(); break; default: CaseOthers(); break; } 每个 case 后面,必须有 break 或者 goto,不允许贯穿。 Goto goto 语句将程序控制直接传递给标记语句。 for (int i = 0; i < x; i++) for (int j = 0; j =this.Width-10) { otherForm.Show(); otherForm.L1.Top=label1.Top; this.Hide(); } } private void button1_Click(object sender, System.EventArgs e) { label1.Left=label1.Left-10; } private void button3_Click(object sender, System.EventArgs e) { label1.Top=label1.Top-10; } private void button4_Click(object sender, System.EventArgs e) { label1.Top=label1.Top+10; } private void button5_Click(object sender, System.EventArgs e) { Application.Exit(); } 同样在 Form2 的 InitializeComponent()下面加上窗体定位语句: Point tempPoint = new Point(300,100); this.DesktopLocation = tempPoint; 然后把 Form2 的 StartPosition 属性改为 Manual。其余程序为: public Label L1 { get { return label1; } set { label1=value; 第 22 页 C#(WINFORM)学习 } } private void button2_Click(object sender, System.EventArgs e) { label1.Left=label1.Left+10; } private void button1_Click(object sender, System.EventArgs e) { Form1 otherForm1=new Form1(); label1.Left=label1.Left-10; if (label1.Left<=-10) { otherForm1.Show(); otherForm1.L2.Top=label1.Top; otherForm1.L2.Left=otherForm1.Width-20; this.Hide(); } } private void button3_Click(object sender, System.EventArgs e) { label1.Top=label1.Top-10; } private void button4_Click(object sender, System.EventArgs e) { label1.Top=label1.Top+10; } 动态产生窗体 public void CreateMyForm() { Form form1 = new Form(); Label label1 = new Label(); Button button1 = new Button (); TextBox text1 = new TextBox(); button1.Text = "确定"; button1.Location = new Point (110, 220); label1.Location = new Point (50,100); text1.Location = new Point (150,100); 第 23 页 C#(WINFORM)学习 form1.Text = "请输入"; label1.Text = "数据"; form1.FormBorderStyle = FormBorderStyle.FixedDialog; form1.ControlBox = false; form1.CancelButton = button1; form1.StartPosition = FormStartPosition.CenterScreen; form1.Controls.Add(button1); form1.Controls.Add(text1); form1.Controls.Add(label1); form1.ShowDialog(); ls=text1.Text; } private void button2_Click(object sender, System.EventArgs e) { CreateMyForm(); MessageBox.Show(ls); } ToolBar 普通使用 在窗体上加上 ToolBar 界面修改后的问题 在界面上修改后,最后要加上: toolBar1.Buttons.Add(toolBarButton1); toolBar1.Buttons.Add(toolBarButton2); toolBar1.Buttons.Add(toolBarButton3); // Add the event-handler delegate. toolBar1.ButtonClick (this.toolBar1_ButtonClick); += new ToolBarButtonClickEventHandler 或者把原有的程序 第 24 页 C#(WINFORM)学习 this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.toolBarButton1,this.toolBarButton2,this.toolBarButton3}); 改变位置,到 toolBar1 设置的最下面。 全部设置程序为: this.toolBar1.DropDownArrows = true; this.toolBar1.Location = new System.Drawing.Point(0, 0); this.toolBar1.Name = "toolBar1"; this.toolBar1.ShowToolTips = true; this.toolBar1.Size = new System.Drawing.Size(592, 42); this.toolBar1.TabIndex = 0; toolBar1.ButtonSize = new System.Drawing.Size(60, 50); // // toolBarButton1 // this.toolBarButton1.Text = "Open"; toolBarButton1 // // toolBarButton2 // this.toolBarButton2.Text = "Save"; toolBarButton2 // // toolBarButton3 // this.toolBarButton3.Text = "Print"; toolBar1.Buttons.Add(toolBarButton1); toolBar1.Buttons.Add(toolBarButton2); toolBar1.Buttons.Add(toolBarButton3); toolBar1.ButtonClick (this.toolBar1_ButtonClick); += new ToolBarButtonClickEventHandler 设置按钮大小 如下设置,可以正常居中显示 9 号字体。 toolBar1.ButtonSize = new System.Drawing.Size(60, 50); 用程序实现 可以用程序实现按钮的增加,但是无法全部实现自动化。 第 25 页 C#(WINFORM)学习 先需要手工添加 toolBar1 和 imageList1,然后把 imageList1 中的图片一一加 上。 void toolBarSet() { //添加按钮 ToolBarButton toolBarButton1=new ToolBarButton(); ToolBarButton toolBarButton2=new ToolBarButton(); toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { toolBarButton1,toolBarButton2}); toolBar1.DropDownArrows = true; toolBar1.ImageList = imageList1; toolBar1.Size = new System.Drawing.Size(408, 37); toolBar1.TabIndex = 0; toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(toolBar1_ButtonClick); // toolBarButton1 toolBarButton1.ImageIndex = 0; toolBarButton1.ToolTipText = "放大"; // toolBarButton2 toolBarButton2.ImageIndex = 1; toolBarButton2.ToolTipText = "缩小"; } private void Form1_Load(object sender, System.EventArgs e) { toolBarSet(); } private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { switch(toolBar1.Buttons.IndexOf(e.Button)) { case 0: //放大 MessageBox.Show("放大"); break; case 1: //缩小 MessageBox.Show("缩小"); break; default: MessageBox.Show("other"); break; 第 26 页 C#(WINFORM)学习 } } listBox 普通调用 在窗体上放置一个 listBox1,一个 button1 和一个 label1。以下程序实现添加 选项,双击选项就可以显示你的选择: private void button1_Click(object sender, System.EventArgs e) { listBox1.Items.Clear(); listBox1.Items.Add(""); listBox1.Items.Add("选择1"); listBox1.Items.Add("选择2"); listBox1.SelectedIndex=0; } private void listBox1_DoubleClick(object sender, System.EventArgs e) { Label1.Text=listBox1.SelectedIndex.ToString(); } 第一项是一个缺省空项,允许用户不选取退出。 Items 是一个集合,因此增减选项可以按照集合那样操作。 用数组添加选项 System.Object[] ItemObject = new System.Object[10]; for (int i = 0; i <= 9; i++) { ItemObject[i] = "Item" + i; } listBox1.Items.AddRange(ItemObject); 第 27 页 C#(WINFORM)学习 ScrollBar 基本定义 ScrollBar 是滚动条控件,分成 HScrollBar(水平)和 VScrollBar(垂直)两 种。有些控件如 ListBox,TextBox 等可以自动添加滚动条,但是有些控件则需 要用程序添加。主要属性意义为: Value:滚动条的数值,反映当前移动块的位置。初始值设定后,运行时停 留在这个位置。运行时拉动滚动条,由 Scroll 事件的 e.NewValue 参数传递过来。 Maximum:Value 的最大值,一般为 100。 Minimum: Value 的最小值, 即端点的数值。 如果 Maximum=100, Minimum=0, LargeChange=10,则从第一个端点开始 Value=0,到另一个端点的 Value=91。 SmallChange:每次点击移动的数值,一般为 1。 LargeChange:移动块的长度,一般为 10。 控件一起使用 和 PicturBox 控件一起使用 float vi; //每个单位的移动距离 float vk=0.8f; //PicturBox显示高度和实际高度的比例 int t0,ti; //PicturBox显示Top和Height。 private void vScrollBar1_Scroll(object sender,System.Windows.Forms.ScrollEventArgs e) { this.pictureBox1.Top = t0-Convert.ToInt32(e.NewValue*vi); this.pictureBox1.Height = ti+Convert.ToInt32(e.NewValue*vi); } private void button1_Click(object sender, System.EventArgs e) { Button oButton; TextBox oTextBox; for(int i=1;i<=8;i++) { oButton = new Button(); oButton.Text = "按钮"+ i.ToString(); oButton.Location = new System.Drawing.Point(50, i*50); oButton.Click += new System.EventHandler(oButton_Click); 第 28 页 C#(WINFORM)学习 this.pictureBox1.Controls.Add(oButton); oTextBox = new TextBox(); oButton.Tag = oTextBox; oTextBox.Text = "1000"; oTextBox.Location = new System.Drawing.Point(150, i*50); this.pictureBox1.Controls.Add(oTextBox); } } private void oButton_Click(object sender, System.EventArgs e) { Button btn = (Button)sender; TextBox txt = (TextBox)btn.Tag; txt.Text = Convert.ToString(Convert.ToInt32(txt.Text) + 1); } private void Form1_Load(object sender, System.EventArgs e) { vi=vk*pictureBox1.Height/vScrollBar1.Maximum; t0=pictureBox1.Top; ti=pictureBox1.Height; } Panel 基本定义 Windows 窗体 Panel(面板)控件用于为其他控件提供可识别的分组。在设 计时所有控件均可轻松地移动,当移动 Panel 控件时,它包含的所有控件也将 移动。分组在一个面板中的控件可以通过面板的 Controls 属性进行访问。 Panel 控件类似于 GroupBox 控件;但只有 Panel 控件可以有滚动条,而 且只有 GroupBox 控件显示标题。 将 AutoScroll 属性设置为 true,可以自动显示滚动条。但是这时右边界和 下边界顶头,不是太好看。这时需要增加一个不可见的控件或者图像来调整。 下例在 Panel 上用程序添加几个控件,产生滚动效果: private void button1_Click(object sender, System.EventArgs e) 第 29 页 C#(WINFORM)学习 { Button oButton; TextBox oTextBox; for(int i=1;i<=8;i++) { oButton = new Button(); oButton.Text = "按钮"+ i.ToString(); oButton.Location = new System.Drawing.Point(50, i*50); oButton.Click += new System.EventHandler(oButton_Click); this.panel1.Controls.Add(oButton); oTextBox = new TextBox(); oButton.Tag = oTextBox; oTextBox.Text = "1000"; oTextBox.Location = new System.Drawing.Point(150, i*50); this.panel1.Controls.Add(oTextBox); } //增加一个不可见按钮,调整右边界和下边界的位置 oButton = new Button(); oButton.Location = new System.Drawing.Point(260, 440); oButton.Height=0; oButton.Width=0; this.panel1.Controls.Add(oButton); } private void oButton_Click(object sender, System.EventArgs e) { Button btn = (Button)sender; TextBox txt = (TextBox)btn.Tag; txt.Text = Convert.ToString(Convert.ToInt32(txt.Text) + 1); } 控件上添加图像 在 Panel 控件上添加图像 在 Panel 控件上不能直接添加图像。 需要在 Panel 控件上添加一个 picturBox, 然后把其 SizeMode 设置为 AutoSize(随着图像大小调整控件大小)就可以实现 图像的随意滚动察看。 控件上画图 在 Panel 控件上画图 Panel 控件上也可以画图。但是滚动时遮盖的图像就消失了。这时候需要在 Panel 控 件 上 添 加 一 个 picturBox , 然 后 在 picturBox 上 画 图 , 然 后 用 一 个 LocationChanged 事件,每次滚动时重画一遍即可: 第 30 页 C#(WINFORM)学习 Pen pen1=new Pen(Color.Green,2); Graphics g1; void drawLine() { PointF p1=new PointF(0,0); PointF p2=new PointF(100,100); g1.DrawLine(pen1,p1,p2); } private void button2_Click(object sender, System.EventArgs e) { g1=this.pictureBox1.CreateGraphics(); drawLine(); } private void pictureBox1_LocationChanged(object sender, System.EventArgs e) { drawLine(); } 菜单 普通应用 手工添加即可。可以直接在其上写各个菜单项的名字,双击可以添加程序, 使用非常方便。 特殊功能 1.在设计时向菜单项添加选中标记 对于在“菜单设计器”内选定的菜单项(三级菜单以下) ,单击该菜单项左侧 的区域,选中标记√。或者在“属性”窗口中将 Checked 属性设置为 True。 以编程方式向菜单项添加选中标记 myMnuItem.Checked = true; 2.在设计时向菜单项添加快捷键 在“菜单设计器”内选择菜单项。在“属性”窗口中,将 Shortcut 属性设置为 第 31 页 C#(WINFORM)学习 下拉列表中提供的值之一。 以编程方式向菜单项添加快捷键 myMnuItem.Shortcut = System.Windows.Forms.Shortcut.F6; 3.向菜单项添加访问键 如键入“文件(&F)” ,显示“文件(F)” 。 若要定位到此菜单项,请按 ALT 键,将焦点移动到菜单栏,然后按该菜单 名称的访问键。当菜单打开并显示带访问键的项时,只需按该访问键就可选定该 菜单项。或者直接按 ALT+主菜单的访问键。 4.将分隔线作为菜单项添加 在菜单设计器中,右击需要有分隔线的位置,然后选择“插入分隔符”。或者 在设置菜单项的 Text 属性(在“属性”窗口中、菜单设计器中或代码中)时,输 入短划线 (–) 使该菜单项成为分隔线。 其它控件 单选按钮 单选按钮是布置一组按钮,只能选择一组控件。 本例放置 3 个单选按钮,Text 属性分别写上“已婚”“未婚”和“离异” 、 , 然后添加一个 Label 控件和一个 Button 控件,程序如下: public Form1() { InitializeComponent(); label1.Text="请选择"; …… private void button1_Click(object sender, System.EventArgs e) { if (radioButton1.Checked == true) label1.Text=radioButton1.Text; else if (radioButton2.Checked == true) label1.Text=radioButton2.Text; else label1.Text=radioButton3.Text; } 第 32 页 C#(WINFORM)学习 } 复选框 可以选择多个的一组控件。 本例放置 2 个复选按钮,Text 属性分别写上“加粗”和“斜体” ,然后添加 一个 Label 控件和一个 Button 控件,程序如下: private void button1_Click(object sender, System.EventArgs e) { if (checkBox1.Checked == true) { if (checkBox2.Checked == true) label1.Text=checkBox1.Text+checkBox2.Text; else if (checkBox2.Checked == false) label1.Text=checkBox1.Text; } else if (checkBox2.Checked == true) label1.Text=checkBox2.Text; else if (checkBox2.Checked == false) label1.Text=""; } 第 33 页 C#(WINFORM)学习 程序产生 checkBox CheckBox checkBox1=new CheckBox(); void checkSet() { this.Controls.Add(checkBox1); checkBox1.Location = new System.Drawing.Point(50, 64); checkBox1.Name = "checkBox1"; checkBox1.TabIndex = 2; checkBox1.Text = "图层1"; checkBox1.CheckedChanged += new System.EventHandler(checkBox1_CheckedChanged); } private void button1_Click(object sender, System.EventArgs e) { checkSet(); } private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { if (checkBox1.Checked) MessageBox.Show("yes"); else MessageBox.Show("no"); } 如果要实现标题在左边,用 check1.Width=90; check1.CheckAlign=ContentAlignment.MiddleRight; 要在其它控件显示: check3.BringToFront(); 动态产生控件 以下程序动态动态产生一组 Button 和 TextBox 控件,以及点击 Button 的事 件。 private void button2_Click(object sender, System.EventArgs e) { Button oButton; TextBox oTextBox; 第 34 页 C#(WINFORM)学习 for(int i=1;i<=5;i++) { oButton = new Button(); oButton.Text = "按钮"+ i.ToString(); oButton.Location = new System.Drawing.Point(50, i*50); oButton.Click += new System.EventHandler(oButton_Click); this.Controls.Add(oButton); oTextBox = new TextBox(); oButton.Tag = oTextBox; oTextBox.Text = "1000"; oTextBox.Location = new System.Drawing.Point(150, i*50); this.Controls.Add(oTextBox); } } private void oButton_Click(object sender, System.EventArgs e) { Button btn = (Button)sender; TextBox txt = (TextBox)btn.Tag; txt.Text = Convert.ToString(Convert.ToInt32(txt.Text) + 1); } Splitter Windows 窗体 splitter 控件用于在运行时调整停靠控件的大小。 Splitter 控 件常用于一类窗体,这类窗体上的控件所显示的数据长度可变,如 Windows 资 源管理器,它的数据窗格所包含的信息在不同的时间有不同的宽度。 如果一个控件可由 splitter 控件调整其大小,则当用户将鼠标指针指向该控 件的未停靠的边缘时,鼠标指针将更改外观,指示该控件的大小是可以调整的。 拆分控件允许用户调整该控件紧前面的停靠控件的大小。因此,为使用户能够在 运行时调整停靠控件的大小,请将要调整大小的控件停靠在容器的一条边缘上, 然后将拆分控件停靠在该容器的同一侧。 以下例子自动产生几个控件,可以在运行中调整大小。 private void CreateMySplitControls() { TreeView treeView1 = new TreeView(); ListView listView1 = new ListView(); Splitter splitter1 = new Splitter(); 第 35 页 C#(WINFORM)学习 treeView1.Dock = DockStyle.Left; splitter1.Dock = DockStyle.Left; splitter1.MinExtra = 100; splitter1.MinSize = 75; listView1.Dock = DockStyle.Fill; treeView1.Nodes.Add("TreeView Node"); listView1.Items.Add("ListView Item"); this.Controls.AddRange(new Control[]{listView1, splitter1, treeView1}); } private void button1_Click(object sender, System.EventArgs e) { CreateMySplitControls(); } tabControl Windows 窗体 TabControl 显示多个选项卡。 使用时, 先添加一个 TabControl 控件,把它拉的足够大。 然后在属性中添加按钮。每个按钮可以控制 TabControl 的其余页面, 作为一 个容器,可以添加其它空间。运行时只要点击按钮,就可以切换选项卡,实现不 同的功能。 StatusBar 可以向 statusBar 添加面板(窗格) ,以分类显示信息: public void CreateStatusBarPanels() { statusBar1.Panels.Add(""); statusBar1.Panels.Add("Two"); statusBar1.Panels.Add("Three"); statusBar1.Panels[0].Width=200; statusBar1.Panels[0].Text="One"; statusBar1.ShowPanels = true; } 第 36 页 C#(WINFORM)学习 三、字符和字符串 字符和字符串 字符串的操作在程序设计中非常有用,因此单独写成一章。 Char 基本定义 char 关键字用于声明一个字符。 char 类型的常数可以写成字符、十六进制换码序列或 Unicode 表示形式。 您也可以显式转换整数字符代码。 以下所有语句均声明了一个 char 变量并用字 符 X 将其初始化: char MyChar = 'X'; char MyChar = '\x0058'; char MyChar = (char)88; char MyChar = '\u0058'; // Character literal // Hexadecimal // Cast from integral type // Unicode char 类型可隐式转换为 ushort、int、uint、long、ulong、float、double 或 decimal 类型。但是,不存在从其他类型到 char 类型的隐式转换。 ToCharArray 将字符串的部分字符复制到 Unicode 字符数组。示例 string str = "012wxyz789"; char[] arr; arr = str.ToCharArray(3, 4); 显示:wxyz 计算字符串宽度 由于英文和中文的显示长度不一样,所以一些场合要区分。 要引用 using System.Globalization; 第 37 页 C#(WINFORM)学习 程序为: //计算一个字符的字符类型,=0汉字,=1英文 private int getCharType(char ch) { int i0; UnicodeCategory ca1=new UnicodeCategory(); ca1=System.Char.GetUnicodeCategory(ch); switch (ca1) { case UnicodeCategory.OtherPunctuation: i0=0; //汉字 break; case UnicodeCategory.OtherLetter: i0=0; //汉字 break; case UnicodeCategory.FinalQuotePunctuation: i0=0; //汉字 break; default: i0=1; //英文 break; } return i0; } //计算字符串(ss,包含中文)的实际宽度(返回)、起点(x0)和高度(height) //输入字号sz,只对于Pixel单位 public float StringWidth(string ss,float sz,ref float x0,ref float height) { char ch1; int i,i0=0; float width=0; float k1=1.02f; //汉字系数 float k2=0.55f; //英文系数 float k3=0.15f; //x0系数 float k4=1.10f; //高度系数 int i1=0; //汉字个数 int i2=0; //英文个数 height=k4*sz; x0=sz*k3; for(i=0;i一个point单位的字体的宽度 public float PStringWidth(string ss,float sz,ref float x0,ref float height) { float width=0; sz=sz*20/15; width=StringWidth(ss,sz,ref x0,ref height); return width; } 这个方法在 sz(字体大小)5~30 内比较准确。很大时有误差。 计算字符串中心 //根据给定点,找到实际标注点,使得以画出的字符串以给定点为中心 PointF StringCenter(string s1,int sz,PointF p0) { PointF p1=new PointF(); float x0=0; float height=0; float width=StringWidth(s1,sz,ref x0,ref height); p1.X=p0.X-+x0-width/2; p1.Y=p0.Y-height/2; return p1; } 计算字符串尺寸示例 1—画方框 — 以下示例利用以上方法,把字符串的长度和高度画成一个方框。 private void button2_Click(object sender, System.EventArgs e) { Graphics g= this.CreateGraphics(); SolidBrush myBrush=new SolidBrush(Color.Red); float x0=0; 第 39 页 C#(WINFORM)学习 float height=0; int sz=10; float px=0,py=50; PointF p0=new PointF(px,py); string s1="我们还34fd还是和平使者"; Font myFont1 = new Font("宋体 ",sz,FontStyle.Bold,GraphicsUnit.Pixel); float width=StringWidth(s1,sz,ref x0,ref height); g.DrawString(s1, myFont1, myBrush, p0); PointF p1=new PointF(px+x0,py); PointF p2=new PointF(px+x0+width,py); PointF p3=new PointF(px+x0+width,py+height); PointF p4=new PointF(px+x0,py+height); PointF[] cur ={p1,p2,p3,p4}; Pen pen1=new Pen(Color.Blue,2); g.DrawPolygon(pen1,cur); } 计算字符串尺寸示例 2—找中点 — private void button1_Click(object sender, System.EventArgs e) { Graphics g= this.CreateGraphics(); SolidBrush myBrush=new SolidBrush(Color.Red); PointF ps=new PointF(); int sz=10; PointF p0=new PointF(300,100); string s1="我们还34fd还是和平使者"; Font myFont1 = new Font("宋体 ",sz,FontStyle.Bold,GraphicsUnit.Pixel); ps=StringCenter(s1,sz,p0); g.DrawString(s1, myFont1, myBrush, ps); //以下画十字线表示中心位置 PointF p1=new PointF(0,p0.Y); PointF p2=new PointF(600,p0.Y); PointF p3=new PointF(p0.X,0); PointF p4=new PointF(p0.X,300); Pen pen1=new Pen(Color.Blue,1); g.DrawLine(pen1,p1,p2); g.DrawLine(pen1,p3,p4); } 第 40 页 C#(WINFORM)学习 分行操作 回车符和换行符 回车符和换行符 “\r\n” 显示换行的语句为: textBox1.Text="ok\\r\\n"; textBox1.Text+="ok1"; 字符串分行 string myString1 = "This is the first line of my string.\n" + "This is the second line of my string.\n" + "This is the third line of the string.\n"; string myString2 = @"This is the first line of my string. This is the second line of my string. This is the third line of the string."; 字符串操作 字符串表示 用@后边的字符串不被处理。 A1=@"c:\Docs\Source\a.txt"; string s1=@"c=""a.txt"; //显示:c=”a.txt string s1=@"c=""a.txt"""; //显示:c=”a.txt” If (s1 == @"""") Then //s1="" 求字符串长度 string s1="fdkls我们"; string s2=Convert.ToString(s1.Length); MessageBox.Show(s2); 第 41 页 C#(WINFORM)学习 运行显示为 7,所有字符个数。 裁剪字符串 String s = "123abc456"; Console.WriteLine(s.Remove(3, 3)); 打印“123456”。 Split 方法 标识此实例中的子字符串 (它们由数组中指定的一个或多个字符进行分隔) , 然后将这些子字符串放入一个 String 数组中。 简单的例子 可以按照“, ”分开,也可以去除空格。 private void button1_Click(object sender, System.EventArgs e) { string astring="123,456 78,789"; string [] split; Char [] chr=new Char [] {',',' '}; split = astring.Split(chr); MessageBox.Show("/"+split[0]+"/"); } 这时可以分成 123,456,78,789 四个字符串。 注意,前后空白也可以看成是一个字符串,要消除,用 astring=astring.Trim(); 就可以了。 复杂的例子 当存在两个空格时,就出现找出空字符串的错误。用以下方法可以去掉空的 字符串: string [] Split0(string [] sp) { string [] sp1=new string [sp.Length]; int i=0,j=0; foreach (string s1 in sp) { 第 42 页 C#(WINFORM)学习 if (s1!="") { sp1[i]=s1; i=i+1; } } string [] sp2=new string [i]; for (j=0;j一个.net 项目中,默认的路径是在\bin\debug 中,如果要放在项目文 件目录中,用 string sfile = Directory.GetCurrentDirectory() + @"\..\..\F8.txt"; 生成新文件 可以用: File.Create(path); 或者 FileStream fs = File.Create(path); 第 44 页 C#(WINFORM)学习 复制 File.Copy(path,path1,true); 如果最后用 false,则第二个文件有时,发生错误。 读写文本文件 using System; using System.IO; private void button1_Click(object sender, System.EventArgs e) { string s1 = @"D:\chen\mytest.txt"; string s2 = @"D:\chen\mytest1.txt"; String input; if (!File.Exists(s1)) { MessageBox.Show("File does not exist."); return; } StreamReader sr = File.OpenText(s1); StreamWriter wr = new StreamWriter(s2); while ((input=sr.ReadLine())!=null) { wr.WriteLine(input); } MessageBox.Show("The end."); sr.Close(); wr.Close(); } 读写中文 string s1 = @"d:\chen\a1.txt"; StreamReader sr = new StreamReader(s1,Encoding.GetEncoding("gb2312")); string rl; while((rl=sr.ReadLine())!=null) { MessageBox.Show(rl); } sr.Close(); 第 45 页 C#(WINFORM)学习 文件操作控件 打开文件 用 openFileDialog 控件。 private void button1_Click(object sender, System.EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory=Directory.GetCurrentDirectory(); openFileDialog1.Filter = "Cursor Files|*.cur"; openFileDialog1.Title = "Select a Cursor File"; if(openFileDialog1.ShowDialog() == DialogResult.OK) { System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName); MessageBox.Show(sr.ReadToEnd()); sr.Close(); } } 选择文件夹 在工具箱上选择 folderBrowserDialog: folderBrowserDialog1.ShowDialog(); string s1 = folderBrowserDialog1.SelectedPath.ToString()+@"\mytest.txt"; StreamWriter wr = new StreamWriter(s1); 第 46 页 C#(WINFORM)学习 六、绘图 基本绘图 画直线 在 Button1 中加入以下程序: System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawLine(myPen, 0, 0, 200, 200); myPen.Dispose(); formGraphics.Dispose(); 就可以在 Form1 上面画一条线了。用 myPen = new System.Drawing.Pen(System.Drawing.Color.Red,3); 可以改变线的宽度。 最简单的画线程序 Pen pen1=new Pen(Color.Green,2); Graphics g1=this.CreateGraphics(); PointF p1=new PointF(0,0); PointF p2=new PointF(100,100); g1.DrawLine(pen1,p1,p2); pen1.Dispose(); g1.Dispose(); 最后两句可以不写,程序关闭时自动完成。 画椭圆 System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.DrawEllipse(myPen, new Rectangle(0,0,200,300)); myPen.Dispose(); formGraphics.Dispose(); 以下画一个椭圆并填充。 System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); 第 47 页 C#(WINFORM)学习 System.Drawing.Graphics formGraphics = this.CreateGraphics(); formGraphics.FillEllipse(myBrush, new Rectangle(0,0,200,300)); myBrush.Dispose(); formGraphics.Dispose(); 以椭圆的中心点画图 //以给定点找画椭圆的原始点,使得椭圆的中心点是给定点 PointF EllipseCenter(int xs,int ys,PointF p0) { float ek=0.5f; PointF p1=new PointF(); p1.X=p0.X-xs*ek; p1.Y=p0.Y-ys*ek; return p1; } private void button1_Click(object sender, System.EventArgs e) { Graphics g= this.CreateGraphics(); Pen pen1=new Pen(Color.Yellow,1); Pen pen2=new Pen(Color.Red,1); PointF ps=new PointF(); int xs=1,ys=1; //半轴 PointF p0=new PointF(300,100); ps=EllipseCenter(xs,ys,p0); //以下画十字线表示中心位置 PointF p1=new PointF(0,p0.Y); PointF p2=new PointF(600,p0.Y); PointF p3=new PointF(p0.X,0); PointF p4=new PointF(p0.X,300); g.DrawLine(pen1,p1,p2); g.DrawLine(pen1,p3,p4); g.DrawEllipse(pen2,ps.X,ps.Y,xs,ys); } 点弧线 System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); 第 48 页 C#(WINFORM)学习 formGraphics.DrawArc(myPen, 100, 50, 140, 70, 30, 180); myPen.Dispose(); formGraphics.Dispose(); 多点直线 System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); PointF point1 = new PointF( 50.0F, 50.0F); PointF point2 = new PointF(100.0F, 25.0F); PointF point3 = new PointF(200.0F, 5.0F); PointF point4 = new PointF(250.0F, 50.0F); PointF point5 = new PointF(300.0F, 100.0F); PointF point6 = new PointF(350.0F, 200.0F); PointF point7 = new PointF(250.0F, 250.0F); PointF[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 }; formGraphics.DrawLines(myPen, curvePoints); myPen.Dispose(); formGraphics.Dispose(); 第 49 页 C#(WINFORM)学习 也可以用以下方式给数组赋值: PointF[] pt=new PointF[]{new PointF(2,2),new PointF(25,150),new PointF(100,100)}; 多点弧线 数据同上,修改如下: int offset = 1; //开始点(从0开始) int numSegments = 5; //包含后续点数 float tension = 1.0F; formGraphics.DrawCurve(myPen, curvePoints, offset, numSegments, tension); 第 50 页 C#(WINFORM)学习 以下程序可以画一个封闭曲线: private void button1_Click(object sender, System.EventArgs e) { System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red); System.Drawing.Graphics formGraphics = this.CreateGraphics(); PointF point1 = new PointF( 50.0F, 50.0F); PointF point2 = new PointF(100.0F, 25.0F); PointF point3 = new PointF(200.0F, 5.0F); PointF point4 = new PointF(250.0F, 50.0F); PointF point5 = new PointF(300.0F, 100.0F); PointF point6 = new PointF(350.0F, 200.0F); PointF point7 = new PointF(250.0F, 250.0F); PointF point8 = new PointF(40.0F, 150.0F); PointF[] curvePoints = { point1, point2, point3, point4, point5, point6, point7, point8, point1 }; int offset = 0; 第 51 页 C#(WINFORM)学习 int numSegments = 8; float tension = 1.0F; formGraphics.DrawCurve(myPen, curvePoints, offset, numSegments, tension); // formGraphics.DrawLines(myPen, curvePoints); myPen.Dispose(); formGraphics.Dispose(); } 如果是任意 3 点(或多点) ,在起始点不容易圆滑。可以用以下方法画封闭 曲线: PointF[] curvePoints = { point3, point1, point2, point3, point1 }; int offset = 1; int numSegments = 3; float tension = 0.5F; 第 52 页 C#(WINFORM)学习 这样可以保证第一个点处比较圆滑。 参数设置 设置线段宽度: myPen.Width =3; 在 pictrueBox 上面画线,修改 this: System.Drawing.Graphics formGraphics = picBox1.CreateGraphics(); 填充 System.Drawing.SolidBrush myBrush = new ystem.Drawing.SolidBrush(System.Drawing.Color.LightPink); System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red,1); System.Drawing.Graphics formGraphics = pictureBox1.CreateGraphics(); PointF point1 = new PointF( 50.0F, 50.0F); PointF point2 = new PointF(100.0F, 25.0F); PointF point3 = new PointF(200.0F, 5.0F); PointF point4 = new PointF(250.0F, 50.0F); PointF point5 = new PointF(300.0F, 100.0F); PointF point6 = new PointF(350.0F, 200.0F); PointF point7 = new PointF(250.0F, 250.0F); PointF[] curvePoints = { 第 53 页 C#(WINFORM)学习 point1, point2, point3, point4, point5, point6, point7 }; formGraphics.FillPolygon(myBrush,curvePoints); formGraphics.DrawPolygon(myPen,curvePoints); myPen.Dispose(); myBrush.Dispose(); formGraphics.Dispose(); 颜色 颜色基本设置 普通颜色设置可以直接选取系统定义的颜色。高级设置采用 RGB 颜色。 Color myColor; myColor = Color.FromArgb(23,56,78); 每个数字均必须是从 0 到 255 之间的一个整数,分别表示红、绿、蓝三种 原色。其中 0 表示没有该颜色,而 255 则为所指定颜色的完整饱和度。如 Color.FromArgb(0,255,0) 表 示 绿 色 , Color.FromArgb(255,255,0) 表 示 黄 色 , Color.FromArgb(0,0,0) 呈现为黑色,而 Color.FromArgb(255,255,255) 呈现为白 色。 还可以设置透明度,如 Color myColor; myColor = Color.FromArgb(127, 23, 56, 78); 127 表示 50%的透明度,255 表示完全不透明。 选择颜色 以下程序可以从调色板选择一个颜色,在 textBox 上面显示。 private void button1_Click(object sender, System.EventArgs e) { ColorDialog MyDialog = new ColorDialog(); 第 54 页 C#(WINFORM)学习 MyDialog.AllowFullOpen = false ; MyDialog.ShowHelp = true ; MyDialog.Color = textBox1.ForeColor ; if (MyDialog.ShowDialog() == DialogResult.OK) textBox1.ForeColor = MyDialog.Color; } 产生窗体选择颜色 本程序在一个菜单里调用选择程序,产生一个动态的窗体,选择颜色后返回 主程序,刷新原来的页面。 由于没有掌握控制动态控件集合的方法,只好用枚举的方法定义动态控件, 对于多于 10 个的颜色序列,需要修改程序。 Form form1; bool Cform=true; //颜色设置 private void menuItem31_Click(object sender, System.EventArgs e) { while (Cform) { CreateColorForm(); Tuli1(); DisLine(); } Cform=true; } //产生颜色输入窗体 private void CreateColorForm() { int i; form1=new Form(); Button [] ColorButton Label [] ColorLabel = new Button [myPloys.marks1.Count]; = new Label [myPloys.marks1.Count]; Button button0 = new Button (); form1.Width=130; form1.Height=130+myPloys.marks1.Count*30; form1.Text = "等值线颜色输入"; form1.FormBorderStyle = FormBorderStyle.FixedDialog; 第 55 页 C#(WINFORM)学习 form1.ControlBox = false; form1.StartPosition = FormStartPosition.CenterScreen; button0.Text = "退出"; button0.Width=80; button0.Location=new Point(25,50+myPloys.marks1.Count*30); form1.Controls.Add(button0); form1.CancelButton = button0; for (i=0;i一个范围(如矩形)内写字。 Font myFont = new Font("Times New Roman", 14); Graphics g = this.CreateGraphics(); Pen myPen=new Pen(Color.Black); System.Drawing.SolidBrush myBrush=new SolidBrush(Color.Red); g.DrawRectangle(myPen,10, 10, 100, 200); g.DrawString("Look at this text!", myFont, myBrush, new RectangleF(10, 10, 100, 200)); g.DrawString("Look at this text!", myFont, myBrush, 10, 250); 画点和标注 System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red,1); System.Drawing.Graphics formGraphics = this.CreateGraphics(); System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 8); string drawString = "201"; float x = 100.0f; 第 58 页 C#(WINFORM)学习 float y = 100.0f; formGraphics.DrawEllipse(myPen, x, y,4,4); formGraphics.DrawString(drawString, drawFont, drawBrush, x, y); myPen.Dispose(); drawFont.Dispose(); drawBrush.Dispose(); formGraphics.Dispose(); Font 定义 public Font( FontFamily family, float emSize, FontStyle style, GraphicsUnit unit ); 以下的例子可以改变字体,大小、字形、单位,还可以设置成垂直。 // Font myFont = new Font("Times New Roman",14,FontStyle.Italic,GraphicsUnit.Millimeter); Font myFont = new Font("隶书",34,FontStyle.Underline,GraphicsUnit.Pixel); Graphics g = this.CreateGraphics(); System.Drawing.SolidBrush myBrush=new SolidBrush(Color.Red); System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat (StringFormatFlags.DirectionVertical); g.DrawString("Hello!你好", myFont, myBrush, 10, 10,drawFormat); 根据给定点找到实际标注点 //根据给定点,找到实际标注点,使得以画出的字符串以给定点为中心 public PointF StringCenter(string s1,float sz,PointF p0) { PointF p1=new PointF(); float x0=0; float height=0; float width=StringWidth(s1,sz,ref x0,ref height); p1.X=p0.X-+x0-width/2; p1.Y=p0.Y-height/2; return p1; } 第 59 页 C#(WINFORM)学习 pictureBox 用 pictureBox 画图 和在 form 上面画图类似,只是把 this 替换成 pictureBox1: System.Drawing.Pen myPen; myPen = new System.Drawing.Pen(System.Drawing.Color.Red,1); System.Drawing.Graphics formGraphics = pictureBox1.CreateGraphics(); System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 8); string drawString = "201"; float x = 200.0f; float y = 100.0f; formGraphics.DrawEllipse(myPen, x, y,4,4); formGraphics.DrawString(drawString, drawFont, drawBrush, x, y); myPen.Dispose(); drawFont.Dispose(); drawBrush.Dispose(); formGraphics.Dispose(); 用 pictureBox 显示一个图片 有固定模式和缩放模式,通过设置 SizeMode 实现。程序如下: pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //按图像文件大小缩放 // pictureBox1.Image = Image.FromFile ("abc.bmp"); //文件放在\bin\Debug文件夹 //文件放在程序文件夹 pictureBox1.Image = Image.FromFile (@"..\..\abc1.bmp"); Region 基本概念 指示由矩形和由路径构成的图形形状的内部。 基本操作 基本操作 System.Drawing.Graphics e = this.CreateGraphics(); 第 60 页 C#(WINFORM)学习 Rectangle regionRect = new Rectangle(20, 20, 100, 100); e.DrawRectangle(Pens.Black, regionRect); RectangleF complementRect = new RectangleF(90, 30, 100, 100); e.DrawRectangle(Pens.Red,Rectangle.Round(complementRect)); Region myRegion = new Region(regionRect); myRegion.Intersect(complementRect); SolidBrush myBrush = new SolidBrush(Color.Blue); e.FillRegion(myBrush, myRegion); 主要图形操作还有 Union 和 Xor。 Polygon 的镂空 采用异或(Xor)运算。需要把 polygon 转换成路径: Graphics g1 = this.CreateGraphics(); GraphicsPath myPath1=new GraphicsPath(); PointF[] pts=new PointF[]{new PointF(2,20),new PointF(250,0) ,new PointF(100,100),new PointF(90,150),new PointF(10,70)}; myPath1.AddPolygon(pts); g1.DrawPath(Pens.Black,myPath1); GraphicsPath myPath2=new GraphicsPath(); PointF[] pts1=new PointF[]{new PointF(20,30),new PointF(50,70) ,new PointF(100,40)}; myPath2.AddPolygon(pts1); g1.DrawPath(Pens.Black,myPath2); Region myRegion = new Region(myPath1); myRegion.Xor(myPath2); SolidBrush myBrush = new SolidBrush(Color.Blue); g1.FillRegion(myBrush, myRegion); 如果要镂空多个空洞,需要把这些空洞都加入到 myPath2 中,其余操作同样。 采用交、并和异或运算,都可以把两个对象互换。 显示数据 public void DisplayRegionData(Graphics e, int len,RegionData dat) { int i; float x = 20, y = 140; 第 61 页 C#(WINFORM)学习 Font myFont = new Font("Arial", 8); SolidBrush myBrush = new SolidBrush(Color.Black); e.DrawString("myRegionData = ",myFont, y = 160; for(i = 0; i 300) { y += 20; x = 20; } e.DrawString(dat.Data[i].ToString(),myFont, x += 30; } } myBrush,new PointF(x, y)); myBrush,new PointF(x, y)); 路径 基本应用 用 GraphicsPath 可以把各种绘图元素 (包括文字) 包含进来, 最后用 DrawPath 画出来。 需要:using System.Drawing.Drawing2D; private void button2_Click(object sender, System.EventArgs e) { Pen myPen = new Pen(System.Drawing.Color.Red,1); System.Drawing.Graphics formGraphics = this.CreateGraphics(); GraphicsPath myPath1=new GraphicsPath(); myPath1.AddLine(0, 0, 10, 20); myPath1.AddEllipse(20,20,10,10); myPath1.AddLine(40, 40, 50, 120); formGraphics.DrawPath(myPen, myPath1); myPath1.Dispose(); myPen.Dispose(); formGraphics.Dispose(); } 第 62 页 C#(WINFORM)学习 不连续线条 用一个半径为 0 的点加在中间: myPath1.AddLine(0, 0, 10, 20); myPath1.AddEllipse(20,20,0,0); myPath1.AddLine(40, 40, 50, 120); 也可以用两个 path,然后用 path2.AddPath(path3,false); 复杂应用 路径可以包含其它路径,汇总成一个大的图形。 还可以通过矩阵变换进行缩放、平移和旋转。 简单 Matrix 变换 可以通过矩阵变换进行图形的变换。 private void button1_Click(object sender, System.EventArgs e) { System.Drawing.Graphics e1 = this.CreateGraphics(); Pen myPen = new Pen(Color.Blue, 1); Pen myPen2 = new Pen(Color.Red, 1); Matrix myMatrix1 = new Matrix(1.0f, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f); // x和y方向都 放大3倍 // Matrix myMatrix1 = new Matrix(1.0f, 0.0f, 0.0f, 1.0f, 50.0f, 50.0f); // 平移50,50 e1.DrawRectangle(myPen, 0, 0, 100, 100); e1.Transform = myMatrix1; e1.DrawRectangle(myPen2, 0, 0, 100, 100); myPen.Dispose(); e1.Dispose(); } 复杂 Matrix 变换 下例显示缩放、旋转和移动变换。 public void MultiplyExample(PaintEventArgs e) { Pen myPen = new Pen(Color.Blue, 2); Pen myPen2 = new Pen(Color.Red, 1); 第 63 页 C#(WINFORM)学习 Matrix myMatrix1 = new Matrix(3.0f, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f); // Scale Matrix myMatrix2 = new Matrix(0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f); // Rotate 90, Matrix myMatrix3 = new Matrix(1.0f, 0.0f, 0.0f, 1.0f, 250.0f, 50.0f); // Translate ListMatrixElements(e, myMatrix1, "Beginning Matrix", 6, 40); myMatrix1.Multiply(myMatrix2, MatrixOrder.Append); ListMatrixElements(e,myMatrix1,"Matrix After 1st Multiplication",6,60); myMatrix1.Multiply(myMatrix3, MatrixOrder.Append); ListMatrixElements(e, myMatrix1,"Matrix After 2nd Multiplication",6,80); e.Graphics.DrawRectangle(myPen, 0, 0, 100, 100); e.Graphics.Transform = myMatrix1; e.Graphics.DrawRectangle(myPen2, 0, 0, 100, 100); } public void ListMatrixElements(PaintEventArgs e,Matrix matrix,string matrixName, int numElements,int y) { int i; float x = 20, X = 200; Font myFont = new Font("Arial", 8); SolidBrush myBrush = new SolidBrush(Color.Black); e.Graphics.DrawString(matrixName + ": for(i=0; i一个点集 (其中自己加上缩放和平移计算) 来做。 从路径转换到点集用 PathPoints 属性。 第 64 页 C#(WINFORM)学习 Image 简单保存 可以利用 Image 类的 Save 方法保存目前显示的文件。需要在 Form 中定义: public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.PictureBox pictureBox1; private Image cuurimage; 然后在程序中引用即可。 cuurimage=pictureBox1.Image; cuurimage.Save("abc2.bmp"); 利用动态 Image 画图 PointF p1 = new PointF(); PointF p2 = new PointF(); Pen myPen = new Pen(Color.Black,1); cuurimage = Image.FromFile("abc.bmp"); Graphics g = Graphics.FromImage(cuurimage); Color [] c ={Color.LightPink,Color.Red,Color.Blue,Color.Brown,Color.Black}; for(k=0;k<=LineColor.Count-1;k++) { myPen.Color = c[(int)LineColor[k]]; p1=(PointF)LineArray[i]; i++; p2=(PointF)LineArray[i]; i++; g.DrawLine(myPen,p1,p2); } myPen.Dispose(); g = CreateGraphics(); g.DrawImage(cuurimage,50,0,cuurimage.Width+100,cuurimage.Height+10); g.Dispose(); 第 65 页 C#(WINFORM)学习 画图保存 在一个 pictureBox 上保存所画图像。 先需要加载一个背景图片,把图画在这个图片上,最后保存到另外一个文件 中。 private Image cuurimage; private void button2_Click(object sender, System.EventArgs e) { Pen myPen = new Pen(System.Drawing.Color.Red,2); pictureBox1.Image = Image.FromFile (@"..\..\abc1.bmp"); cuurimage = pictureBox1.Image; Graphics g = Graphics.FromImage(cuurimage); g.DrawEllipse(myPen, 100, 100,44,14); cuurimage.Save("abc0.bmp"); myPen.Dispose(); g.Dispose(); } 利用 Image 擦除图像 下例要先准备一个 jpg 图像文件。 先
Javascript小技巧一箩筐 事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.srcElement.releaseCapture(); 事件按键 event.keyCode event.shiftKey event.altKey event.ctrlKey 事件返回值 event.returnValue 鼠标位置 event.x event.y 窗体活动元素 document.activeElement 绑定事件 document.captureEvents(Event.KEYDOWN); 访问窗体元素 document.all("txt").focus(); document.all("txt").select(); 窗体命令 document.execCommand 窗体COOKIE document.cookie 菜单事件 document.oncontextmenu 创建元素 document.createElement("SPAN"); 根据鼠标获得元素: document.elementFromPoint(event.x,event.y).tagName=="TD document.elementFromPoint(event.x,event.y).appendChild(ms) 窗体图片 document.images[索引] 窗体事件绑定 document.onmousedown=scrollwindow; 元素 document.窗体.elements[索引] 对象绑定事件 document.all.xxx.detachEvent("onclick",a); 插件数目 navigator.plugins 取变量类型 typeof($js_libpath) == "undefined" 下拉框 下拉框.options[索引] 下拉框.options.length 查找对象 document.getElementsByName("r1"); document.getElementById(id); 定时 timer=setInterval("scrollwindow()",delay); clearInterval(timer); UNCODE编码 escape() ,unescape 父对象 obj.parentElement(dhtml) obj.parentNode(dom) 交换表的行 TableID.moveRow(2,1) 替换CSS document.all.csss.href = "a.css"; 并排显示 display:inline 隐藏焦点 hidefocus=true 根据宽度换行 style="word-break:break-all" 自动刷新 简单邮件 快速转到位置 obj.scrollIntoView(true) 锚 anchors 网页传递参数 location.search(); 可编辑 obj.contenteditable=true 执行菜单命令 obj.execCommand 双字节字符 /[^x00-xff]/ 汉字 /[u4e00-u9fa5]/ 让英文字符串超出表格宽度自动换行 word-wrap: break-word; word-break: break-all; 透明背景 获得style内容 obj.style.cssText HTML标签 document.documentElement.innerHTML 第一个style标签 document.styleSheets[0] style标签里的第一个样式 document.styleSheets[0].rules[0] 防止点击空链接时,页面往往重置到页首端。 word 上一网页源 asp: request.servervariables("HTTP_REFERER") javascript: document.referrer 释放内存 CollectGarbage(); 禁止右键 document.oncontextmenu = function() { return false;} 禁止保存 地址栏图标 favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 收藏栏图标 查看源码 关闭输入法 自动全选 ENTER键可以让光标移到下一个输入框 文本框的默认值 title换行 obj.title = "123 sdfs " 获得时间所代表的微秒 var n1 = new Date("2004-10-10".replace(/-/g, "/")).getTime() 窗口是否关闭 win.closed checkbox扁平
获取选中内容 document.selection.createRange().duplicate().text 自动完成功能 打开该功能 关闭该功能 窗口最大化 无关闭按钮IE window.open("aa.htm", "meizz", "fullscreen=7"); 统一编码/解码 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe"))) encodeURIComponent对":"、"/"、";" 和 "?"也编码 表格行指示 //各种尺寸 s += " 网页可见区域宽:"+ document.body.clientWidth; s += " 网页可见区域高:"+ document.body.clientHeight; s += " 网页可见区域高:"+ document.body.offsetWeight +" (包括边线的宽)"; s += " 网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)"; s += " 网页正文全文宽:"+ document.body.scrollWidth; s += " 网页正文全文高:"+ document.body.scrollHeight; s += " 网页被卷去的高:"+ document.body.scrollTop; s += " 网页被卷去的左:"+ document.body.scrollLeft; s += " 网页正文部分上:"+ window.screenTop; s += " 网页正文部分左:"+ window.screenLeft; s += " 屏幕分辨率的高:"+ window.screen.height; s += " 屏幕分辨率的宽:"+ window.screen.width; s += " 屏幕可用工作区高度:"+ window.screen.availHeight; s += " 屏幕可用工作区宽度:"+ window.screen.availWidth; //过滤数字 //特殊用途 //不缓存 //正则匹配匹配中文字符的正则表达式: [u4e00-u9fa5] 匹配双字节字符(包括汉字在内):[^x00-xff] 匹配空行的正则表达式: [s| ]* 匹配HTML标记的正则表达式:/<(.*)>.*|<(.*) />/ 匹配首尾空格的正则表达式:(^s*)|(s*$)(像vbscript那样的trim函数) 匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* 匹配网址URL的正则表达式:http://([w-]+.)+[w-]+(/[w- ./?%&=]*)? 以下是例子: 利用正则表达式限制网页表单里的文本框输入内容: 用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,"")" onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^u4E00-u9FA5]/g,""))" 1.用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,"")" onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^uFF00-uFFFF]/g,""))" 2.用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^d]/g,"") "onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^d]/g,""))" 3.用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[W]/g,"") "onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^d]/g,""))" //消除图像工具栏 or //无提示关闭 function Close() { var ua=navigator.userAgent var ie=navigator.appName=="Microsoft Internet Explorer"?true:false if(ie) { var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE ")+5,ua.indexOf(";",ua.indexOf("MSIE ")))) if(IEversion< 5.5) { var str = "" str += ""; document.body.insertAdjacentHTML("beforeEnd", str); document.all.noTipClose.Click(); } else { window.opener =null; window.close(); } } else { window.close() } } //取得控件得绝对位置(1) <script language="javascript"> function getoffset(e) { var t=e.offsetTop; var l=e.offsetLeft; while(e=e.offsetParent) { t+=e.offsetTop; l+=e.offsetLeft; } var rec = new Array(1); rec[0] = t; rec[1] = l; return rec } //光标停在文字最后 <script language="javascript"> function cc() { var e = event.srcElement; var r =e.createTextRange(); r.moveStart("character",e.value.length); r.collapse(true); r.select(); } //页面进入和退出的特效 进入页面 推出页面 这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使 用哪种特效,取值为1-23:   0 矩形缩小   1 矩形扩大   2 圆形缩小   3 圆形扩大   4 下到上刷新   5 上到下刷新   6 左到右刷新   7 右到左刷新   8 竖百叶窗   9 横百叶窗   10 错位横百叶窗   11 错位竖百叶窗   12 点扩散   13 左右到中间刷新   14 中间到左右刷新   15 中间到上下   16 上下到中间   17 右下到左上   18 右上到左下   19 左上到右下   20 左下到右上   21 横条   22 竖条   23 //网页是否被检索   其中属性值有以下一些:   属性值为"all": 文件将被检索,且页上链接可被查询;   属性值为"none": 文件不被检索,而且不查询页上的链接;   属性值为"index": 文件将被检索;   属性值为"follow": 查询页上的链接;   属性值为"noindex": 文件不检索,但可被查询链接;   属性值为"nofollow": //打印分页

page1

page2

//设置打印 <script language=javascript> function window.onload() { // -- advanced features factory.printing.SetMarginMeasure(2) // measure margins in inches factory.printing.SetPageRange(false, 1, 3) // need pages from 1 to 3 factory.printing.printer = "HP DeskJet 870C" factory.printing.copies = 2 factory.printing.collate = true factory.printing.paperSize = "A4" factory.printing.paperSource = "Manual feed" // -- basic features factory.printing.header = "居左显示&b居中显示&b居右显示页码,第&p页/共&P页" factory.printing.footer = "(自定义页脚)" factory.printing.portrait = false factory.printing.leftMargin = 0.75 factory.printing.topMargin = 1.5 factory.printing.rightMargin = 0.75 factory.printing.bottomMargin = 1.5 } function Print(frame) { factory.printing.Print(true, frame) // print with prompt }
具体使用手册,更多信息,点这里 //自带的打印预览 WebBrowser.ExecWB(1,1) 打开 Web.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口 Web.ExecWB(4,1) 保存网页 Web.ExecWB(6,1) 打印 Web.ExecWB(7,1) 打印预览 Web.ExecWB(8,1) 打印页面设置 Web.ExecWB(10,1) 查看页面属性 Web.ExecWB(15,1) 好像是撤销,有待确认 Web.ExecWB(17,1) 全选 Web.ExecWB(22,1) 刷新 Web.ExecWB(45,1) 关闭窗体无提示

//去掉打印时的页眉页脚 <script language="JavaScript"> var HKEY_Root,HKEY_Path,HKEY_Key; HKEY_Root="HKEY_CURRENT_USER"; HKEY_Path="\Software\Microsoft\Internet Explorer\PageSetup\"; //设置网页打印的页眉页脚为空 function PageSetup_Null() { try { var Wsh=new ActiveXObject("WScript.Shell"); HKEY_Key="header"; Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); HKEY_Key="footer"; Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); } catch(e){} } //设置网页打印的页眉页脚为默认值 function PageSetup_Default() { try { var Wsh=new ActiveXObject("WScript.Shell"); HKEY_Key="header"; Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b页码,&p/&P"); HKEY_Key="footer"; Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d"); } catch(e){} } //浏览器验证 function checkBrowser() { this.ver=navigator.appVersion this.dom=document.getElementById?1:0 this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0; this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0; this.ie4=(document.all && !this.dom)?1:0; this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0; this.ns4=(document.layers && !this.dom)?1:0; this.mac=(this.ver.indexOf("Mac") > -1) ?1:0; this.ope=(navigator.userAgent.indexOf("Opera")>-1); this.ie=(this.ie6 || this.ie5 || this.ie4) this.ns=(this.ns4 || this.ns5) this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope) this.nbw=(!this.bw) return this; } //计算内容宽和高 //无模式的提示框 function modelessAlert(Msg) { window.showModelessDialog("javascript:alert(""+escape(Msg)+"");window.close();","","status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;"); } //屏蔽按键 屏蔽鼠标右键、Ctrl+N、Shift+F10、Alt+F4、F11、F5刷新、退格键 <script language="Javascript"> //可编辑SELECT //设置光标位置 function getCaret(textbox) { var control = document.activeElement; textbox.focus(); var rang = document.selection.createRange(); rang.setEndPoint("StartToStart",textbox.createTextRange()) control.focus(); return rang.text.length; } function setCaret(textbox,pos) { try { var r =textbox.createTextRange(); r.moveStart("character",pos); r.collapse(true); r.select(); } catch(e) {} } function selectLength(textbox,start,len) { try { var r =textbox.createTextRange(); r.moveEnd("character",len-(textbox.value.length-start)); r.moveStart("character",start); r.select(); } catch(e) {//alert(e.description)} } function insertAtCaret(textbox,text) { textbox.focus(); document.selection.createRange().text = text; } //页内查找 function findInPage(str) { var txt, i, found,n = 0; if (str == "") { return false; } txt = document.body.createTextRange(); for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) { txt.moveStart("character", 1); txt.moveEnd("textedit"); } if (found) { txt.moveStart("character", -1); txt.findText(str); txt.select(); txt.scrollIntoView(); n++; } else { if (n > 0) { n = 0; findInPage(str); } else { alert(str + "... 您要找的文字不存在。 请试着输入页面中的关键字再次查找!"); } } return false; } //书 http://www.itpub.net/attachment.php?s=&postid=1894598 http://www.wrclub.net/down/listdown.aspx?id=1341//操作EXECL <script language="javascript"> function jStartExcel() { var xls = new ActiveXObject ( "Excel.Application" ); xls.visible = true; var newBook = xls.Workbooks.Add; newBook.Worksheets.Add; newBook.Worksheets(1).Activate; xls.ActiveWorkBook.ActiveSheet.PageSetup.Orientation = 2; xls.ActiveWorkBook.ActiveSheet.PageSetup.PaperSize = 5; newBook.Worksheets(1).Columns("A").columnwidth=50; newBook.Worksheets(1).Columns("A").WrapText = true; newBook.Worksheets(1).Columns("B").columnwidth=50; newBook.Worksheets(1).Columns("B").WrapText = true; newBook.Worksheets(1).Range("A1:B1000").NumberFormat = "0"; newBook.Worksheets(1).Range("A1:B1000").HorizontalAlignment = -4131; newBook.Worksheets(1).Cells(1,1).Interior.ColorIndex="15"; newBook.Worksheets(1).Cells(1,1).value="First Column, First Cell"; newBook.Worksheets(1).Cells(2,1).value="First Column, Second Cell"; newBook.Worksheets(1).Cells(1,2).value="Second Column, First Cell"; newBook.Worksheets(1).Cells(2,2).value="Second Column, Second Cell"; newBook.Worksheets(1).Name="My First WorkSheet"; } tip <script Language="JavaScript"> //***********默认设置定义.********************* tPopWait=50;//停留tWait豪秒后显示提示。 tPopShow=5000;//显示tShow豪秒后关闭提示 showPopStep=20; popOpacity=99; //***************内部变量定义***************** sPop=null; curShow=null; tFadeOut=null; tFadeIn=null; tFadeWaiting=null; document.write(""); document.write("
"); function showPopupText(){ var o=event.srcElement; MouseX=event.x; MouseY=event.y; if(o.alt!=null && o.alt!=""){o.dypop=o.alt;o.alt=""}; if(o.title!=null && o.title!=""){o.dypop=o.title;o.title=""}; if(o.dypop!=sPop) { sPop=o.dypop; clearTimeout(curShow); clearTimeout(tFadeOut); clearTimeout(tFadeIn); clearTimeout(tFadeWaiting); if(sPop==null || sPop=="") { dypopLayer.innerHTML=""; dypopLayer.style.filter="Alpha()"; dypopLayer.filters.Alpha.opacity=0; } else { if(o.dyclass!=null) popStyle=o.dyclass else popStyle="cPopText"; curShow=setTimeout("showIt()",tPopWait); } } } function showIt(){ dypopLayer.className=popStyle; dypopLayer.innerHTML=sPop; popWidth=dypopLayer.clientWidth; popHeight=dypopLayer.clientHeight; if(MouseX+12+popWidth>document.body.clientWidth) popLeftAdjust=-popWidth-24 else popLeftAdjust=0; if(MouseY+12+popHeight>document.body.clientHeight) popTopAdjust=-popHeight-24 else popTopAdjust=0; dypopLayer.style.left=MouseX+12+document.body.scrollLeft+popLeftAdjust; dypopLayer.style.top=MouseY+12+document.body.scrollTop+popTopAdjust; dypopLayer.style.filter="Alpha(Opacity=0)"; fadeOut(); } function fadeOut(){ if(dypopLayer.filters.Alpha.opacity0) { dypopLayer.filters.Alpha.opacity-=1; tFadeIn=setTimeout("fadeIn()",1); } } document.onmouseover=showPopupText; 插入文字"); } } //netscapte下操作xml doc = new ActiveXObject("Msxml2.DOMDocument"); doc = new ActiveXObject("Microsoft.XMLDOM") ->> doc = (new DOMParser()).parseFromString(sXML,"text/xml") //判断键值 <script language="javascript"> var ie =navigator.appName=="Microsoft Internet Explorer"?true:false; function keyDown(e) { if(!ie) { var nkey=e.which; var iekey="现在是ns浏览器"; var realkey=String.fromCharCode(e.which); } if(ie) { var iekey=event.keyCode; var nkey="现在是ie浏览器"; var realkey=String.fromCharCode(event.keyCode); if(event.keyCode==32){realkey="" 空格""} if(event.keyCode==13){realkey="" 回车""} if(event.keyCode==27){realkey="" Esc""} if(event.keyCode==16){realkey="" Shift""} if(event.keyCode==17){realkey="" Ctrl""} if(event.keyCode==18){realkey="" Alt""} } alert("ns浏览器中键值:"+nkey+" "+"ie浏览器中键值:"+iekey+" "+"实际键为"+realkey); } document.onkeydown = keyDown; //Javascript Document.

请按任意一个键。。。。

//禁止FSO 1.注销组件 regsvr32 /u scrrun.dll 2.修改PROGID HKEY_CLASSES_ROOTScripting.FileSystemObject Scripting.FileSystemObject 3.对于使用object的用户,修改HKEY_CLASSES_ROOTScripting. //省略号
就是比如有一行文字,很长,表格内一行显示不下.
//检测media play版本 //图象按比例 <script language="JavaScript"> //细线SELECT function getComputerName() { var objWMIService = GetObject("Winmgmts:rootcimv2"); for(e = new Enumerator(objWMIService) ; !e.atEnd() ; e.moveNext()) { var getComputer = e.item(); return getComputer.Name; } } //条件编译 <script language=javascript> /*@cc_on @*/ /*@if (@_win32 && @_jscript_version>5) function window.confirm(str) { execScript("n = msgbox(""+ str +"", 257)", "vbscript"); return(n == 1); } @end @*/ //mergeAttributes 复制所有读/写标签属性到指定元素。
This is a sample DIV element.
This is another sample DIV element.
以上内容可以随意转载,转载后请注名来源和出处! 原文链接:http://ttyp.cnblogs.com/archive/2004/11/15/63900.aspx //Import function Import() { for( var i=0; i"); else document.write(""); } }; //js枚举 function getComputerName() { var objWMIService = GetObject("Winmgmts:rootcimv2"); for(e = new Enumerator(objWMIService) ; !e.atEnd() ; e.moveNext()) { var getComputer = e.item(); return getComputer.Name; } } //条件编译 <script language=javascript> /*@cc_on @*/ /*@if (@_win32 && @_jscript_version>5) function window.confirm(str) { execScript("n = msgbox(""+ str +"", 257)", "vbscript"); return(n == 1); } @end @*/ //mergeAttributes 复制所有读/写标签属性到指定元素。
This is a sample DIV element.
This is another sample DIV element.
电子书制作:源码爱好者

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧