星期四, 10月 28, 2010

[Silverlight][VB]透過VisualTreeHelper找出DependencyObject內之Object

Private Function GetVisualTreeObject(Of T)(ByVal obj As DependencyObject)
    Dim child As DependencyObject
    Dim count As Integer = VisualTreeHelper.GetChildrenCount(obj)
    For index = 0 To count - 1
        child = VisualTreeHelper.GetChild(obj, index)
        If child.GetType().Name = GetType(T).Name Then
            Return child
        End If
        child = GetVisualTreeObject(Of T)(child)
        If Not IsNothing(child) Then
            Return child
        End If
    Next
    Return Nothing
End Function

Private Function FindInternalObjectByName(Of T)(ByVal name As String, ByVal obj As DependencyObject)
    Dim child As DependencyObject
    Dim count As Integer = VisualTreeHelper.GetChildrenCount(obj)
    For index = 0 To count - 1
        child = VisualTreeHelper.GetChild(obj, index)
        If child.GetType().Name = GetType(T).Name Then
            Dim fe As FrameworkElement = child
            If fe.Name = name Then
                Return child
            End If
        End If
        child = GetVisualTreeObject(child)
        If Not IsNothing(child) Then
            Return child
        End If
    Next
    Return Nothing
End Function

[Silverlight][VB]將Grid的ColumnDefinition的width轉為DependencyProperty

ColumnWidth轉為DependencyProperty

Private Shared ReadOnly ColumnWidthProperty As DependencyProperty = DependencyProperty.Register("ColumnWidth", GetType(Double), GetType(MainPage), New Windows.PropertyMetadata(AddressOf ColumnWidthChanged))
Private Property ColumnWidth() As Double
Get
Return DirectCast(GetValue(ColumnWidthProperty), Double)
End Get
Set(ByVal value As Double)
SetValue(ColumnWidthProperty, value)
End Set
End Property

Private Shared Sub ColumnWidthChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs) 'GridCol1 is a name of ColumnDefinition
DirectCast(d, MainPage).GridCol1.Width = New GridLength(DirectCast(e.NewValue, Double))
End Sub




星期二, 9月 07, 2010

[Silverlight] 將畫面上元件輸出為圖片

VB:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim bitmap As WriteableBitmap = New WriteableBitmap(can, Nothing)

        If Not IsNothing(bitmap) Then
            Dim savedia As SaveFileDialog = New SaveFileDialog()
            savedia.Filter = "JPEG Files (*.jpeg)|*.jpeg"
            savedia.DefaultExt = ".jpeg"

            If savedia.ShowDialog() Then
                Using st As System.IO.Stream = savedia.OpenFile()
                    SaveToFile(bitmap, st)
                    MessageBox.Show("done")
                End Using
            End If
        End If

    End Sub

    Private Shared Sub SaveToFile(ByVal bitmap As WriteableBitmap, ByVal st As System.IO.Stream)
        Dim wid As Integer = bitmap.PixelWidth
        Dim hei As Integer = bitmap.PixelHeight
        Dim bands As Integer = 2
        Dim raster()(,) As Byte = New Byte(bands)(,) {}

        For index = 0 To bands
            raster(index) = New Byte(wid, hei) {}

        Next

        For row = 0 To hei - 1

            For col = 0 To wid - 1
                Dim pixel As Integer = bitmap.Pixels(wid * row + col)
                Dim tmp As Integer

                tmp = (pixel >> 16) Mod 256
                raster(0)(col, row) = (256 + tmp) Mod 256
                tmp = (pixel >> 8) Mod 256
                raster(1)(col, row) = (256 + tmp) Mod 256
                tmp = pixel Mod 256
                raster(2)(col, row) = (256 + tmp) Mod 256

            Next

        Next
        Dim model As ColorModel = New ColorModel() With {.colorspace = ColorSpace.RGB}
        Dim img As FluxJpeg.Core.Image = New FluxJpeg.Core.Image(model, raster)

        Using ms As System.IO.MemoryStream = New System.IO.MemoryStream
            Dim encoder As FluxJpeg.Core.Encoder.JpegEncoder = New FluxJpeg.Core.Encoder.JpegEncoder(img, 100, ms)
            encoder.Encode()
            ms.Seek(0, IO.SeekOrigin.Begin)
            Dim binary() As Byte = New Byte(ms.Length) {}
            Dim byteread = ms.Read(binary, 0, ms.Length)
            st.Write(binary, 0, binary.Length)
        End Using

    End Sub

C#:網路找就有了...差異較大的地方就是轉byte,VB CByte很蠢就是了...
此外要去抓fjCore dll才能使用

星期四, 9月 02, 2010

取得本機使用者登入ID

VB:

dim UserID = System.Security.Principal.WindowsIdentity.GetCurrent.Name.Split("\")(1)


C#:

string UserID = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split("\\")(1);

星期四, 8月 12, 2010

NET 擴充方法

參見

C#:

public static class ExtendTest
{
 public static int Test(this string str) //for u want type
  { // ...}
}


VB:

<Extension()> _
Public Sub Test(byval str as String) 'for u want type
  ' .....
End Sub

星期三, 6月 23, 2010

取得時區時間

這是取得中國時區時間,其它時間輸入其id即可取得

Dim d2 As DateTime = DateTime.UtcNow.AddHours(TimeZoneInfo.FindSystemTimeZoneById("China Standard Time").BaseUtcOffset.Hours)


秀出所有的timezone資訊

Dim str As String = ""
Dim currentTime As DateTime
For Each tzi In TimeZoneInfo.GetSystemTimeZones
currentTime = DateTime.UtcNow.AddHours(tzi.BaseUtcOffset.Hours)
str = str & String.Format("ID:{0}, StandardName:{1}, Time:{2}", tzi.Id, tzi.StandardName, currentTime) & System.Environment.NewLine
Next

星期四, 4月 08, 2010

取得屬性名稱集合


Dim data As New MyObject
dim list as new List(of string)
For Each da In data.GetType().GetProperties().ToList()
list.Add(da.Name)
Next

透過屬性名稱 取得值


using System.Reflection;

object obj;
Type t = obj.GetType();
PropertyInfo pi = t.GetProperty("YourPropertyName");
object YourValue = pi.GetValue(obj,null)

星期二, 4月 06, 2010

Silverlight 使用imagebrush 填圖


Dim imgbrush As ImageBrush = New ImageBrush() With {.ImageSource = New BitmapImage(New Uri("Images/SomeImage.png", UriKind.Relative))}