前言
一、效果
二、界面设计
三、代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 自练1
{
public partial class treeview控件 : Form
{
public treeview控件()
{
InitializeComponent();
TreeNode RootNode1 = new TreeNode("根节点1");
TreeNode RootNode2 = new TreeNode("根节点2");
TreeNode ChildNode1 = new TreeNode("子节点1");
TreeNode ChildNode2 = new TreeNode("子节点2");
TreeNode ChildNode1_1 = new TreeNode("子节点1_1");
TreeNode ChildNode1_2 = new TreeNode("子节点1_2");
treeView1.Nodes.Add(RootNode1);
treeView1.Nodes.Add(RootNode2);
RootNode1.Nodes.AddRange(new TreeNode[] { ChildNode1, ChildNode2 });
ChildNode1.Nodes.AddRange(new TreeNode[] { ChildNode1_1, ChildNode1_2 });
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hello,world","弹出的对话框");
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void treeview控件_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult choice = MessageBox.Show("确定要关闭窗体吗?", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (choice == DialogResult.Cancel) //判断是否单击“取消按钮”
{
e.Cancel = true;
}
}
}
public class MessageFilter : System.Windows.Forms.IMessageFilter //禁止窗体拖动的类,继承自System.Windows.Forms.IMessageFilter 接口
{
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTCAPTION = 2;
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_NCLBUTTONDOWN && m.WParam.ToInt32() == HTCAPTION)
return true;
return false;
}
}
}
总结