上色

Code Block

2013年6月30日 星期日

[VB.Net] 物件XML序列化及反序列化方法

可將物件序列化之後利用網路傳送至其他程序,或者另存為文件
 
Public Function Serialize(ByVal objType As Type, ByVal obj As Object) As String
    '將元件進行序列化
    Dim XMLString As String = String.Empty
    Dim mySerializer As XmlSerializer = New XmlSerializer(objType) '以objType建立XML序列化元件
    Dim writer As New IO.StringWriter '建立資料流
    mySerializer.Serialize(writer, obj) 'XML序列化
    XMLString = writer.ToString '轉為字串
    writer.Close() '關閉資料流
    Return XMLString '回傳
End Function

Public Function Deserialize(ByVal objType As Type, ByVal XMLString As String) As Object
    '將取得的內容進行反序列化
    Dim mySerializer As XmlSerializer = New XmlSerializer(objType) '以objType建立XML序列化元件
    Dim reader As New IO.StringReader(XMLString) '以序列化後的XMLString建立資料流
    Return mySerializer.Deserialize(reader) '回傳反序列化後的物件
End Function 

2013年5月1日 星期三

[圖像處理] BMP影像分散寬度(stride)與影像寬度(width)

Stride︰代表每列影像列的位元長度(bytes),等於argb(32bits)或rgb(24bits)乘上影像寬度
Width︰影像寬度

此處有個問題,若影像寬度乘上位元之後不是4的倍數(32bits),系統會自動補足到4的倍數;如影像寬度270,則Stride應為810,但810不是4的倍數,因此系統會自動補到812,若直接以Stride/Width取得位元組數或是要取得影像起始位置時就會出錯。

2013年4月19日 星期五

[PHP] 區分同一個表單上的兩個按鈕

以onclick設定不同按鈕所送出的參數,例如︰

按鈕1︰
<input type="submit" name="button" value="Choose" onclick="document.form.action='sign.php?no=1' "/>

按鈕2︰
<input type="submit" name="button" value="Choose" onclick="document.form.action='sign.php?no=2' "/> 

如此即可在目的地表單(sign.php)藉由$_GET[no]方法取得此表單所送出的no變數,即可輕易分辨不同按鈕

2013年4月18日 星期四

[SQL] select指令

SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy
 
what_to_select表示要搜尋的目標,也可以「*」表示所有的列
which_table表示要搜尋的資料表
conditions_to_satisfy是選擇性參數,代表搜尋條件 

[PHP] Session無法啟用 - Cannot send session

Warning: session_start() [function.session-start]: Cannot send session ... - headers already sent (output started at .... )

發生原因
session_start() 之前不能有任何字元輸出,UTF-8 編碼裡的 BOM 也會被認為是 headers,有上述狀況 Session 將無法順利傳遞,並會顯示錯誤訊息。

解決方法1
1. 確定 <?php 和 session_start() 之間沒有其他字元,空格也要移除。
2. 若是 UTF-8 編碼,請用編輯器(例 UltraEdit、Notepad++...)將檔案裡的 BOM 移除。

解決方法2
以 ob_start() 開啟緩衝區將輸出資訊寫入緩衝區,可避免 headers 先於 session_start() 輸出,寫入緩衝區的內容可由 flush() 或 ob_end_flush() 輸出至瀏覽器,以下範例不會顯示錯誤訊息:
<?php
ob_start();
echo "test";
session_start();
ob_end_flush();
?>
Reference:
http://www.pczone.com.tw/vbb3/thread/47/73367/

2013年4月14日 星期日

[Phone] 合併vCard檔案 (*.vcf)

利用copy命令的/b參數將多個vcf檔案以二進位形式合併

copy /b *.vcf all.vcf

以上命令的意義代表將該資料夾下所有的vcf檔案(vCard檔案)合併成為all.vcf

2013年4月9日 星期二

[VB.Net] 取得視窗大小資訊

Public x0, y0, x1, y1 As Integer '視窗四角座標
Public Client_x0, Client_y0 As Integer '視窗左上座標(不含標題邊框)
Public ClientX, ClientY As Integer '視窗長寬(不含標題邊框)
Public Border As Integer '視窗邊框寬度
Public Title As Integer '視窗標題高度
Public hwnd As Integer '視窗hwnd

'取得視窗大小(含邊框,功能表)
Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Integer, ByRef rectangle As RECT) As Integer

'取得視窗大小(不含邊框,功能表,左上角為0,0)
Public Declare Function GetClientRect Lib "user32 " (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer

Structure RECT '視窗大小資料結構
    Dim x1 As Integer
    Dim y1 As Integer
    Dim x2 As Integer
    Dim y2 As Integer
End Structure

'取得特定視窗資訊
Sub GetWindowInfo(hwnd As Integer)
    Dim R As RECT
    Dim RetVal As Integer
    RetVal = GetWindowRect(hwnd, R)
    x0 = R.x1
    x1 = R.x2
    y0 = R.y1
    y1 = R.y2
    RetVal = GetClientRect(hwnd, R)
    ClientX = R.x2
    ClientY = R.y2
    Border = ((x1 - x0) - ClientX) / 2
    Title = (y1 - y0) - ClientY - Border
    Client_x0 = x0 + Border
    Client_y0 = y0 + Title
End Sub