是否可以为TreeViewControl设置BackgroudImage? [英] Is it Possible to Set BackgroudImage for TreeViewControl?

查看:83
本文介绍了是否可以为TreeViewControl设置BackgroudImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一些节点绑定到我的Treeview控件.
我不想向每个节点显示imagekey或imageindex.

我想要一个图像,作为整个TreeView控件的背景.

如何将背景图片设置为Treeview控件?是否可以?

如果可能的话
让我知道.
如果不尝试以其他方式向我建议..

纯Windows应用程序,仅C#.Net或VB.Net.

真诚的
Pawan.

Hi all,

I have some nodes binded to my Treeview Control.
i don''t want to show imagekey or imageindex to each of my node.

i want an image which acts as an backgroud for entire TreeView Control.

how to set background image to Treeview control?Possible or not?

if Possible
do let me know.
if not try to suggest me the other way..

Purely Windows Application only C#.Net or VB.Net.

Sincerely,
Pawan.

推荐答案

是的,有可能-我认为.当然,这并不容易,而且我认为付出的努力不会值得结果...

TreeView确实具有可以设置的BackgroundImage属性-它没有在intellisense中列出,因此您必须输入整个内容.不幸的是,由于我无法理解的原因,它什么也没做. MS对TreeView派生自控件的官方"解释并没有真正成立,因为该属性存在,但是呵呵.

您可以通过重写WndProc并将WM_ERASEBKGND捕获在派生类中来提供自己的背景:
Yes it is possible - I think. It certainly isn''t going to be easy, and I don''t think the effort is going to be worth the results...

TreeView does have a property BackgroundImage which you can set - it isn''t listed in intellisense so you have to type the whole thing. Unfortunately, it does nothing, for reasons I cannot fathom. The "official" MS explanation that TreeView is derived from control does not really hold water, as the property exists, but ho hum.

You can provide your own background by overriding WndProc and trapping WM_ERASEBKGND in a derived class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace GUITester
    {
    public partial class MyTreeView : TreeView
        {
        private Image background;

        public Image Background
            {
            get { return background; }
            set { background = value; }
            }

        public MyTreeView()
            {
            InitializeComponent();
            background = Bitmap.FromFile(@"F:\Temp\XXX.jpg");
            }

        const int WM_ERASEBKGND = 20;

        protected override void WndProc(ref Message m)
            {
            if (m.Msg == WM_ERASEBKGND)
                {
                using (Graphics g = CreateGraphics())
                    {
                    g.DrawImage(background, new Point(0, 0));
                    }
                }
            else
                {
                base.WndProc(ref m);
                }
            }
        }
    }

这可行-直到添加节点.试试看-看起来很可怕.因此,显而易见的事情是从TreeNode派生并提取相同的技巧.只有您不能-TreeNode不公开WndProc.因此,您将必须变得非常聪明,并找到其他方法!

如果您确实必须执行此操作,则可以编写自己的TreeView或购买其中一个-我敢肯定有这样的事情!

如果您自己编写,则可以从中获得不错的文章!

And this works - right up until you add nodes. Try it - it looks pretty horrible. So the obvious thing to do is to derive from TreeNode and pull the same trick. Only you can''t - TreeNode doesn''t expose WndProc. So you will have to get really clever and find some other way!

If you really must do this, then either write your own TreeView, or buy one in - I am sure there is such a thing out there!

If you do write it yourself, you could get a good article out of it!


在WPF中,这确实很容易做到.如果这对您的项目确实很关键,则可能需要重新考虑使用的技术.使用正确的技术是项目分析阶段的重要组成部分.
In WPF, it''s really easy to do. If it''s really critical to your project, you might want to reconsider the technology you use. Using the right technology is an important part of a project analysis phase.


是的,有可能.

您需要创建一个样式表
例如.
.logo
{
background-image:url(''images/logo.gif'');
宽度:180像素;
高度:50px;
z-index:1;
}


并在您的Treeview控件上

添加div标签并调用class = logo

您的treeview控件

结束div标签

这会将背景图像添加到树视图控件中.
Yes, It is possible.

You need to create a stylesheet
For eg.
.logo
{
background-image:url(''images/logo.gif'');
width:180px;
height:50px;
z-index:1;
}


and on your treeview control

Add div tag and call class =logo

your treeview control

end div tag

This will add background image to your treeview control.


这篇关于是否可以为TreeViewControl设置BackgroudImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆