代码改变世界

silverlight系列(HTMLDom通信、鼠标位置的捕捉)

2010-03-09 11:28  key_sky  阅读(663)  评论(1编辑  收藏  举报

HtmlDom.aspx(更改silverlight布局):

<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width
="100%" height="200px">
<param name="source" value="ClientBin/CoummunicationHTMLDOM.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"
style
="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="获取 Microsoft Silverlight"
style
="border-style: none"/>
</a>
</object>
<iframe id="_sl_historyFrame" style='visibility:hidden;height:0;width:0;border:0px'>
</iframe>
获取的信息:
<input id="Text1" type="text" style="width:300px; color:Red;" />
<br />
<span id="span1" style="width:300px; height:50px; color:Green"></span>
</div>

HtmlDom.xaml:

<Grid x:Name="LayoutRoot" Background="Orange" HorizontalAlignment="Left" Width="Auto" Height="500"
ShowGridLines
="False" VerticalAlignment="Top" Margin="5,5,5,5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox x:Name="txtmessage" Width="150" Background="AliceBlue" Grid.Column="0">
</TextBox>
<Button x:Name="btnPost" Content="发送" Width="80" Grid.Column="1" Click="btnPost_Click">
</Button>
<Button x:Name="btnCreateButton" Content="创建按钮" Width="80" Grid.Column="2"
Click
="btnCreateButton_Click"></Button>
<Button x:Name="btnHello" Content="唤醒脚本" Width="80" Grid.Column="3"
Click
="btnHello_Click"></Button>
</Grid>
<!--
获取Javascript对象有Invoke和CreateInstance两种方法
-->

HtmlDom.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Browser;

namespace CoummunicationHTMLDOM
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private void btnPost_Click(object sender, RoutedEventArgs e)
{
HtmlElement element
= HtmlPage.Document.GetElementById("Text1");

element.SetAttribute(
"innerText", "Recevie From Silverlight:" + txtmessage.Text);
}

private void btnCreateButton_Click(object sender, RoutedEventArgs e)
{
HtmlElement span
= HtmlPage.Document.GetElementById("span1");

HtmlElement button
= HtmlPage.Document.CreateElement("button");
button.SetAttribute(
"innerText", "改变silverlight背景颜色");
button.SetAttribute(
"type", "button");

span.AppendChild(button);
button.AttachEvent(
"onclick", new EventHandler<HtmlEventArgs>(btnclick));
}

private void btnclick(object sender, HtmlEventArgs e)
{
if (((SolidColorBrush)LayoutRoot.Background).Color == Colors.Orange)
{
LayoutRoot.Background
= new SolidColorBrush(Colors.Green);
}
else
{
LayoutRoot.Background
= new SolidColorBrush(Colors.Orange);
}

}

private void btnHello_Click(object sender, RoutedEventArgs e)
{
ScriptObject script
= HtmlPage.Window.GetProperty("onHelloWorld") as ScriptObject;
script.InvokeSelf(txtmessage.Text);
}
}
}

运行效果:

点击发送按钮:

点击创建按钮,执行后点击更改silverlight颜色按钮:

点击唤醒脚本按钮:

GetMousePosition.xaml:

<Grid x:Name="LayoutRoot" HorizontalAlignment="Left" Width="Auto" Height="Auto"
ShowGridLines
="True" VerticalAlignment="Top" Margin="5,5,5,5"
MouseLeftButtonDown
="LayoutRoot_MouseLeftButtonDown" MouseMove="LayoutRoot_MouseMove">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition />
</Grid.ColumnDefinitions>

<Canvas Background="LightCyan" x:Name="canvas" HorizontalAlignment="Left"
  VerticalAlignment="Top"
Width
="1000" Height="750" Grid.Row="0" Grid.Column="1" >

<TextBlock x:Name="tb1" Foreground="Green" Width="Auto"
Height="25" Canvas.Top="30" ></TextBlock>
<TextBlock x:Name="tb2" Foreground="Red" Width="Auto" Height="25" ></TextBlock>

</Canvas>

</Grid>

GetMousePosition.cs:

private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element
= sender as FrameworkElement;

double pointY = e.GetPosition(null).Y - mousePostion.Y;

double pointX = e.GetPosition(null).X - mousePostion.X;

double newPointY = pointY + (double)element.GetValue(Canvas.TopProperty);

double newPointX = pointX + (double)element.GetValue(Canvas.LeftProperty);

tb2.Text
= "鼠标位置X:" + e.GetPosition(null).X + " Y:" + e.GetPosition(null).Y;


}

运行效果: