Silverlight的几个知识点(二)
1,Silverlight 与JS交互:
<1>, silverlight 调用js:
HtmlPage.Window.Invoke("函数名", new object[] { 参数数组});
HtmlPage.Window.CreateInstance("函数名", new object[] { 参数数组});
<2>,JS 调用silverlight方法:
HtmlPage.RegisterScriptableObject("RPFrame", this);
// 1,注册 [ScriptableMember] // 2,标记 public DateTime GetDataFrom() { return Utility.StartTime;
} function pluginLoaded(sender, args) { // 3,JS调用 slPlugin = sender.getHost();
__dtFrom = slPlugin.content.RPFrame.GetDataFrom();
}
2,silverlight hitTest
3, Control 控件Click 事件模拟
protected event RoutedEventHandler Click;
void RotationPlanningSlot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (Click != null && e.GetPosition(thisElement.Parent as UIElement) == beginMousePosition) // Add click envent { Click(this, e);
} }
4,Silverlight 3的打印,
[ScriptableMember] public void GetPrintPNG() { WriteableBitmap bitmap = new WriteableBitmap(this.MainPlaningCanvas,new TranslateTransform());
EditableImage imageData = https://www.it610.com/article/new EditableImage(bitmap.PixelWidth, bitmap.PixelHeight);
for (int y = 0;
y < bitmap.PixelHeight;
++y) { for (int x = 0;
x < bitmap.PixelWidth;
++x) { int pixel = bitmap.Pixels[bitmap.PixelWidth * y + x];
imageData.SetPixel(x, y, (byte)((pixel>> 16) & 0xFF), (byte)((pixel >> 8) & 0xFF), (byte)(pixel & 0xFF), (byte)((pixel >> 24) & 0xFF) );
} } Stream pngStream = imageData.GetStream();
byte[] binaryData = https://www.it610.com/article/new Byte[pngStream.Length];
long bytesRead = pngStream.Read(binaryData, 0, (int)pngStream.Length);
string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
// save the encoded PNG bytes to the page HtmlDocument document = HtmlPage.Document;
HtmlElement txtPNGBytes = document.GetElementById("txtPNGBytes");
txtPNGBytes.SetProperty("value", base64String);
// this calls the js function "postBackPrint" which will cause a postback //HtmlPage.Window.CreateInstance("postBackPrint", new string[] { });
}
PngEncoder.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Browser;
using System.Reflection;
namespace PlanningCtrl.UI { public class PngEncoder { private const int _ADLER32_BASE = 65521;
private const int _MAXBLOCK = 0xFFFF;
private static byte[] _HEADER = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
private static byte[] _IHDR = { (byte)'I', (byte)'H', (byte)'D', (byte)'R' };
private static byte[] _GAMA = { (byte)'g', (byte)'A', (byte)'M', (byte)'A' };
private static byte[] _IDAT = { (byte)'I', (byte)'D', (byte)'A', (byte)'T' };
private static byte[] _IEND = { (byte)'I', (byte)'E', (byte)'N', (byte)'D' };
private static byte[] _4BYTEDATA = https://www.it610.com/article/{ 0, 0, 0, 0 };
private static byte[] _ARGB = { 0, 0, 0, 0, 0, 0, 0, 0, 8, 6, 0, 0, 0 };
public static Stream Encode(byte[] data, int width, int height) { MemoryStream ms = new MemoryStream();
byte[] size;
// Write PNG header ms.Write(_HEADER, 0, _HEADER.Length);
// Write IHDR // Width: 4 bytes // Height: 4 bytes // Bit depth: 1 byte // Color type: 1 byte // Compression method: 1 byte // Filter method: 1 byte // Interlace method: 1 byte size = BitConverter.GetBytes(width);
_ARGB[0] = size[3];
_ARGB[1] = size[2];
_ARGB[2] = size[1];
_ARGB[3] = size[0];
size = BitConverter.GetBytes(height);
_ARGB[4] = size[3];
_ARGB[5] = size[2];
_ARGB[6] = size[1];
_ARGB[7] = size[0];
// Write IHDR chunk WriteChunk(ms, _IHDR, _ARGB);
// Set gamma = 1 size = BitConverter.GetBytes(1 * 100000);
_4BYTEDATA[0] = size[3];
_4BYTEDATA[1] = size[2];
_4BYTEDATA[2] = size[1];
_4BYTEDATA[3] = size[0];
// Write gAMA chunk WriteChunk(ms, _GAMA, _4BYTEDATA);
// Write IDAT chunk uint widthLength = (uint)(width*4)+1;
uint dcSize = widthLength * (uint)height;
// First part of ZLIB header is 78 1101 1010 (DA) 0000 00001 (01) // ZLIB info // // CMF Byte: 78 // CINFO = 7 (32K window size) // CM = 8 = (deflate compression) // FLG Byte: DA // FLEVEL = 3 (bits 6 and 7 - ignored but signifies max compression) // FDICT = 0 (bit 5, 0 - no preset dictionary) // FCHCK = 26 (bits 0-4 - ensure CMF*256+FLG / 31 has no remainder) // Compressed data // FLAGS: 0 or 1 // 00000 00 (no compression) X (X=1 for last block, 0=not the last block) // LEN = length in bytes (equal to ((width*4)+1)*height // NLEN = one's compliment of LEN // Example: 1111 1011 1111 1111 (FB), 0000 0100 0000 0000 (40) // Data for each line: 0 [RGBA] [RGBA] [RGBA] ... // ADLER32 uint adler = ComputeAdler32(data);
MemoryStream comp = new MemoryStream();
// Calculate number of 64K blocks uint rowsPerBlock = _MAXBLOCK/widthLength;
uint blockSize = rowsPerBlock*widthLength;
uint blockCount;
ushort length;
uint remainder=dcSize;
if ((dcSize % blockSize) == 0) { blockCount = dcSize/blockSize;
} else { blockCount = (dcSize/blockSize)+1;
} // Write headers comp.WriteByte(0x78);
comp.WriteByte(0xDA);
for (uint blocks=0;
blocks
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
namespace PlanningCtrl.UI { public class EditableImage { private int _width=0;
private int _height=0;
private bool _init=false;
private byte[] _buffer;
private int _rowLength;
public event EventHandler
5,为ScrollViewer增加MouseWheel 支持
【Silverlight的几个知识点(二)】 void MainScroll_MouseWheel(object sender, MouseWheelEventArgs e) { ScrollViewer viewer = sender as ScrollViewer;
if (!e.Handled) { double offsite = viewer.VerticalOffset - e.Delta;
viewer.ScrollToVerticalOffset(offsite);
e.Handled = true;
} }
6,ToolTip设计
7,自定义的MessageBox
详见:http://www.cnblogs.com/hedonister/archive/2009/08/02/1537142.html
8,定义Control的热区
protected void SetHotAreaCursor(FrameworkElement targetElement, double xOffsiteElment) { // Set the hot area if (xOffsiteElment > this.Width * 0.03 && xOffsiteElment < this.Width * 0.97) { targetElement.Cursor = Cursors.Hand;
} else { targetElement.Cursor = Cursors.SizeWE;
} }
9,child Window和父窗口传值
public partial class testChildWindow : ChildWindow { public string testString { get;
set;
} public testChildWindow() { InitializeComponent();
} private void OKButton_Click(object sender, RoutedEventArgs e) { testString = DateTime.Now.ToString();
this.DialogResult = true;
} private void CancelButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = false;
} } testChildWindow tc = new testChildWindow();
//实例化弹出窗口 public SilverlightControl1() { InitializeComponent();
tc.Closed += new EventHandler(tc_Closed);
//弹出窗口的关闭事件 this.Loaded += new RoutedEventHandler(SilverlightControl1_Loaded);
} void SilverlightControl1_Loaded(object sender, RoutedEventArgs e) { tc.Show();
//弹出窗口 } void tc_Closed(object sender, EventArgs e) { string val = tc.testString;
//返回的值 }
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量