当前位置: 移动技术网 > IT编程>开发语言>.net > 拖拽TreeViewItem到OCX控件

拖拽TreeViewItem到OCX控件

2018年11月28日  | 移动技术网IT编程  | 我要评论

茂名长途汽车站,bt种子核工厂,铁骆驼蓄电池修复

由于c#在性能方面,和c++还是有不少的差距,所以在项目中有一块是用c++的ocx控件实现,然后包括在wpf项目中。由于c++,c#属于不同的体系架构,造成了许多问题,特使是拖拽treeviewitem到ocx控件上面,两者的渲染方式不同,ocx控件一直显示在最前面,所以拖拽的时候,看不见拖拽的adornerlayer,并且鼠标还显示禁止状态。下面的内容主要是解决这两个问题的经历:

1.解决拖拽treeviewitem到ocx控件上面,鼠标是禁止状态。没有显示adornerlayer,用户考虑到我们使用技术的原因,也还理解,只是这个禁止状态,用户接受不了,所以解决这个问题成为重中之重。

(1)刚刚看到禁止图标,一眼就以为是allowdrop没有设置为true,然后就把这个控件有这个属性的地方都设置了一遍,结果鼠标拖拽的时候移上去还是禁止状态。

(2)依旧对allowdrop属性不死心,让同事看ocx控件中有没有对应的属性,最终找到一个acceptfile,虽然和allowdrop不太一样,但是活马当死马医,只能要求同事生成ocx控件,给我试一把。最终结果还是不能解决问题。

(3)在没有什么好的想法的时候,依旧对ocx的属性设置还抱有一点希望,到处问同事,搜google,bing,msdn,最终一上午都没有看到有用的信息,只能无奈放弃。

(4)后面也没有什么好的方法,就在博客园,msdn上面提问,具体可以见,https://social.msdn.microsoft.com/forums/zh-cn/02959b72-a46c-4a27-bef5-cf8e246e8977/22312wpf200132530225341treeviewitem21040ocx2551120214200136529240736?forum=wpfzhchs#c8c87cb0-6ed5-492f-9f3e-1c40857db1f1,然后根据msdn上面技术支持给的建议,参考中的

在项目中的treeview中,我也重写了该方法,强制将鼠标设置为箭头,结果启动程序试了一下,拖拽treeviewitem到ocx控件上面,鼠标还是箭头,不是禁止状态。

1 protected override void ongivefeedback(givefeedbackeventargs e)
2         {
3             base.ongivefeedback(e);
4             mouse.setcursor(cursors.arrow);
5             e.handled = true;           
6         }

2.解决拖拽项所在的adornerlayer,被ocx控件遮挡的问题

由于ocx是通过windowsformshost的方式加到wpf程序中,两者的渲染机制不同,导致ocx控件显示在最前面,wpf中zindex也就英雄无用武之地。

(1)考虑到窗口可以显示在ocx的前面,想过在鼠标进入ocx的时候,在鼠标下面添加一个窗口,跟随鼠标移动,这种想法是可行的,但是在ocx控件的enter和leave事件中都没有响应到拖拽过程中,鼠标的进入控件,离开控件的事件,导致这个没有向下执行下去。

(2)后面还考虑直接将鼠标式样改成拖拽项的图标,也因此放弃了。

(3)最后在拖拽的过程中,我发现拖拽到ocx控件的e.effects == dragdropeffects.none,其他的wpf部分,可以直接在窗口上面将allowdrop属性设置为true就行,因此可以根据这个区分拖拽是在wpf上面还是ocx上面,当在ocx上面的时候修改鼠标的式样。因为treeviewitem是一张图片和其名称够成,平时设置鼠标式样的时候,是直接将图片设置为鼠标式样,看了一下cursor里面的属性,没有发现可以设置文字的地方,只能先将字符串转化为图片,再将两张图片合并在一起。其中关键的代码如下所示:

  1 using system;
  2 using system.drawing;
  3 using system.runtime.interopservices;
  4 using system.windows.input;
  5 using system.windows.interop;
  6 
  7 namespace viewmodels
  8 {
  9     public static class cursorhelper
 10     {
 11         public static cursor createbitmapcursor(string filepath)
 12         {
 13             bitmap bmp = null;
 14             try
 15             {
 16                 bmp = bitmap.fromfile(filepath) as bitmap;
 17                 if (bmp != null)
 18                 {
 19                     return bitmapcursor.createbmpcursor(bmp);
 20                 }
 21 
 22                 return cursors.arrow;
 23             }
 24             catch (exception)
 25             {
 26                 return cursors.arrow;
 27             }
 28         }
 29 
 30         public static cursor createbitmapcursor(string filepath, string name)
 31         {
 32             bitmap bmp = null;
 33             try
 34             {
 35                 bmp = bitmap.fromfile(filepath) as bitmap;
 36                 if (bmp != null)
 37                 {
 38                     var namebmp = stringtoimg(name);
 39                     var mergebmp = combinimage(bmp, namebmp);
 40                     if (mergebmp != null)
 41                     {
 42                         return bitmapcursor.createbmpcursor(mergebmp);
 43                     }
 44                 }
 45 
 46                 return cursors.arrow;
 47             }
 48             catch (exception)
 49             {
 50                 return cursors.arrow;
 51             }
 52         }
 53 
 54         public static bitmap stringtoimg(string name)
 55         {
 56             graphics g = graphics.fromimage(new bitmap(1, 1));
 57             font font = new font("宋体", 12);
 58             sizef sizef = g.measurestring(name, font); //测量出字体的高度和宽度
 59             brush brush = brushes.white;
 60             pointf pf = new pointf(0, 0);
 61             bitmap img = new bitmap(convert.toint32(sizef.width), convert.toint32(sizef.height));
 62             g = graphics.fromimage(img);
 63             g.drawstring(name, font, brush, pf);
 64 
 65             return img;
 66         }
 67 
 68         public static bitmap combinimage(image icoimg, image stringimg)
 69         {
 70             int height = math.max(icoimg.height, stringimg.height);
 71             bitmap bmp = new bitmap(icoimg.width + stringimg.width + 6, height);
 72 
 73             graphics g = graphics.fromimage(bmp);
 74             g.clear(color.fromargb(150, 201, 252));
 75             g.drawimage(icoimg, 0, (height - icoimg.height) / 2, icoimg.width, icoimg.height);
 76             g.drawimage(stringimg, icoimg.width + 6, (height - stringimg.height) / 2, stringimg.width, stringimg.height);            
 77 
 78             return bmp;
 79         }
 80 
 81         public static cursor getcursor(fastreeviewitemviewmodel data)
 82         {
 83             if (data.nodetype == nodetype.camera)
 84             {
 85                 if (data.devicetype == devicetype.normal)
 86                 {
 87                     if (data.state == treeitemstate.online)
 88                     {
 89                         return createbitmapcursor(string.format(@"{0}device_ipc_on.png", appviewmodel.instance.basedirectory), data.displayname);
 90                     }
 91                     else if (data.state == treeitemstate.offline)
 92                     {
 93                         return createbitmapcursor(string.format(@"{0}device_ipc_off.png", appviewmodel.instance.basedirectory), data.displayname);
 94                     }
 95                 }
 96                 else if (data.devicetype == devicetype.ptz)
 97                 {
 98                     if (data.state == treeitemstate.online)
 99                     {
100                         return createbitmapcursor(string.format(@"{0}device_ptzipc_on.png", appviewmodel.instance.basedirectory), data.displayname);
101                     }
102                     else if (data.state == treeitemstate.offline)
103                     {
104                         return createbitmapcursor(string.format(@"{0}device_ptzipc_off.png", appviewmodel.instance.basedirectory), data.displayname);
105                     }
106                 }
107             }
108             else if (data.nodetype == nodetype.pollinggroup)
109             {
110                 if (data.state == treeitemstate.normal)
111                 {
112                     return createbitmapcursor(string.format(@"{0}device_patrol.png", appviewmodel.instance.basedirectory), data.displayname);
113                 }
114                 else if (data.state == treeitemstate.running)
115                 {
116                     return createbitmapcursor(string.format(@"{0}device_patrol_play.png", appviewmodel.instance.basedirectory), data.displayname);
117                 }
118             }
119 
120             return cursors.arrow;
121         }
122     }
123 
124     internal class bitmapcursor : safehandle
125     {
126         public override bool isinvalid
127         {
128             get
129             {
130                 return handle == (intptr)(-1);
131             }
132         }
133 
134         public static cursor createbmpcursor(bitmap cursorbitmap)
135         {
136             bitmapcursor c = new bitmapcursor(cursorbitmap);
137             return cursorinterophelper.create(c);
138         }
139 
140         protected bitmapcursor(bitmap cursorbitmap)
141             : base((intptr)(-1), true)
142         {
143             handle = cursorbitmap.gethicon();
144         }
145 
146         protected override bool releasehandle()
147         {
148             bool result = destroyicon(handle);
149 
150             handle = (intptr)(-1);
151 
152             return result;
153         }
154 
155         [dllimport("user32")]
156         private static extern bool destroyicon(intptr hicon);
157     }
158 }
 1 protected override void ongivefeedback(givefeedbackeventargs e)
 2         {
 3             base.ongivefeedback(e);
 4             if (e.effects.hasflag(dragdropeffects.move))
 5             {
 6                 mouse.setcursor(cursors.arrow);
 7             }
 8             else
 9             {
10                 var dataitem = this.selecteditem as treeviewitemviewmodelbase;
11                 if (dataitem != null)
12                 {
13                     mouse.setcursor(dataitem.getcurrentcursor());
14                 }
15             }
16 
17             e.handled = true;
18         }

通过这两种方式,就解决上述的两个问题,可以满足项目的要求。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网