星期二, 3月 30, 2010

Save & Read File (txt)


string dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //系統路徑
string path = dir + "\\autoPlan.ini";
if (File.Exists(path))
{
StreamReader sr = new StreamReader(File.OpenRead(path));
while ((data = sr.ReadLine()) != "")
{
//.....
}
}

============================================================

StreamWriter sw = new StreamWriter(File.OpenWrite(path));
sw.WriteLine("Something");
sw.Close();

星期四, 3月 18, 2010

silverlight Clipboard


Imports System.Windows.Browser

Public Class SLClipboard
Private Shared data As String = ""
Public Shared Function GetText() As String
Dim clipboarddata As ScriptObject = HtmlPage.Window.GetProperty("clipboardData")

If IsNothing(clipboarddata) Then
MessageBox.Show("Sorry, this functionality is only avaliable in Internet Explorer.")
Else
data = clipboarddata.Invoke("getData", "text")
End If
Return data
End Function

Public Shared Sub SetText(ByVal txt As String)
data = txt
Dim clipboarddata As ScriptObject = HtmlPage.Window.GetProperty("clipboardData")
If IsNothing(clipboarddata) Then
MessageBox.Show("Sorry, this functionality is only avaliable in Internet Explorer.")
Else
clipboarddata.Invoke("setData", "text", data)
End If
End Sub

End Class

=====
參...
http://weblogs.asp.net/manishdalal/archive/2008/11/12/cross-browser-copy-and-paste-in-datagrid-with-excel-support-part-1.aspx

星期五, 3月 12, 2010

VB函數用法

VB函數說明 / VB函數用法

絕對值 / Abs(number)

傳回陣列範本 / Array(arglist)

算字元的ASCII碼 / Asc( string)

轉Boolean型態值 / CBool( expression )

轉Byte型態值 / CByte( expression )

轉Currency型態值 / CCur (expression )

轉Date型態的值 / CDate (date )

轉 Double 型態的值 / CDbl (expression )

轉ASCII碼表示的字元 / Chr (charcode)

轉為整數型態 / CInt(expression)

轉為長整數型態 / Clng (expression )

傳回物件範本 / CreateObject (class)

轉為單精度型態 / CSng (expression)

轉為字串型態 / CStr (expression )

傳回目前系統時間 / Date

傳回更改的時間 / DateAdd (interval, number, date)

傳回兩日期的間隔 / DateDiff (interval,date1,date2)

傳回某時間的部分值 / DatePart (interval,date)

轉換某時間Date型態 / DateSerial(year, month, day)

傳回陣列內的字串位置 / Filter (InputStrings, Value)

傳回Date 型態的值 / DateValue(date)

傳回1~31的日期數字 / Day(date)

最大整數值 / Int (number )

無條件捨去小數 / Fix (number)

傳回表示式型式 / FormatCurrency(Expression)

傳回日期或時間表示式 / FormatDateTime (Date)

傳回數字的表示式 / FormatNumber(Expressiom)

傳回百分比的表示式 / FormatPercent(Expression)

取回檔案內的物件 / GetObject([pathname][,class])

轉為16進位 / Hex(number)

傳回時間的時數 / Hour(time)

顯示可供輸入的對話窗 / inputBox(prompt,......)

傳回搜尋字串的位置 / InStr(string,searchstr)

從尾端搜尋字串位置 / InstrRev(string,searchstr)

判定變數是否為陣列 / IsArray(varname)

判定是否為日期表示式 / IsDate(expression)

判定變數內是否有內容 / IsEmpty(expression)

判定是否為NULL值 / IsNull(expression)

判定是否為數字值 / IsNumeric(expression)

傳回一個圖片物件 / LoadPicture(picturename)

判定是否為一個變數 / IsObject(expression)

字串加入陣列內 / Join(list[,delimiter])

轉為小寫字串 / LCase(string)

字串取左邊長度 / Left(string,ength)

傳回字串長度 / Len(string|varname)

傳回自然對數值 / Log(number)

去除字串的左邊空白 / LTrim(string)

去除字串的右邊空白 / RTrim(string)

去除字串的左右邊空白 / Trim(string)

傳回指定位置的字串 / Mid(string,start[,length])

傳回時間的分鐘 / Minute(time)

傳回日期的月份 / Month(date)

傳回月份字串 / MonthName(month[,abbreviate)

顯示一個訊息窗 / MsgBox(prompt,...)

傳回系統日期與時間/ Now

轉為八進位數字 / Oct(number)

取代部份字串 / Replace(string,find,replacewith)

字串取右邊長度 / Right(string,ength)

傳回一個隨機數字 / Rnd[(number)]

指定字串轉成陣列 / Split(expression,...)

傳回使用的Script語言 / ScriptEngine

傳回時間的秒數 / Second(time)

判斷數字的正負符號 / Sgn(number)

傳回弧度數字的Sin值 / Sin(number)

產生空白字元字串 / Space(number)

算數值的二次方根植 / Sqr(number)

比較兩字串 / StuComp(string1,string2[,compare])

反過來排列字串 / StrReverse(string)

產生重複字元字串 / String(number,character)

傳回目前的系統時間 / Time

傳回時間Date型態 / TimeSerial(hour,minute,second)

傳回含時間的Date型態 / TimeValue(time)

傳回變數的資料型態 / TypeName(varname)

從尾端傳回陣列小索引 / UBound(anayname)

轉為大寫字元 / UCase(string)

傳回代表變數資料型態的數字 / VarType(varname)

傳回日期是禮拜幾? / Weekday(date,[firstdayofweek])

傳回日期內的年數值 / Year(date)

====
網路上隨便搜尋就有了

星期四, 3月 11, 2010

十六進位表示法

不同電腦系統、編程語言對於16進制數值有不同的表示方式:

* Ada用所基於的「數字引證」把16進位數包起來,例如「16#5A3#」。(註:Ada對整數和實數都可以使用從1到16中任何一個做為其基數。)
* VHDL中使用字首 X,例如,X"10",對應的二進位碼為:"00010000".
* C語言、Python語言及其他相近的語言使用字首「0x」,例如「0x5A3」。開頭的「0」令解析器更易辨認數,而「x」則代表十六進位(就如「O」代表八進位)。在「0x」中的「x」可以大寫或小寫
* 在VB中,使用字首「&H」來表示
* 在HTML,十六進位字元可以用「x」,例如֣和֣效果應該無異
* 某些版本的BASIC語言會採用「&H」詞頭,例如:「&H5A3」。
* Pascal和某些匯編器用字尾「h」來辨別16進位的數(若該數以字母起首,在前面會增加一個「0」)例如「0A3Ch」、「5A3h」
* 其他匯編器(AT&T、Motorola)和某些BASIC的版本使用字首「$」,例如「$5A3」
* 亦有用X'5A3'這類表示方式的,如PL/I

由於表示方式沒有單一、已協定的標準,所以以上的表示方式都有被使用,有時甚至在同一份論文上會出現幾個不同的表示方式。但因為各方式之間沒有太大的分歧,所以即使混合使用也沒有構成問題。

其中最常用(或常見)表示十六進位數值的方式是將 '0x' 加在數字前,或在數字後加上小字 16。例如 0x2BAD 和 2BAD16 都是表示十進位的11181(或1118110)。

在網頁設計上十六進制是很常用的。HTML和CSS使用十六進制的表示法來表示網頁上的特定顏色。使用 # 的符號來表示而非用個別的符號表示十六進制。24-bit 顏色可以用 #RRGGBB 的格式來表示,RR 是顏色中紅色成分的數值,GG 是顏色中綠色成分的數值,BB 顏色中藍色成分的數值。舉個例子, 紅色的陰影是十進制 238,9,63 可以編成 #EE093F。參見 網頁顏色模式.

在URL的特殊字元亦是用ASCII中的十六進位,每位元組都有百份比符號(%)在前,例如: 空格就表示為%20,而中文維基百科的首頁地址就是 http://zh.wikipedia.org/wiki/Wikipedia:%E9%A6%96%E9%A1%B5 (在一些現代的瀏覽器,如Firefox中,地址中十六進位編碼會被解碼成實際字元,所以在這些瀏覽器中中文維基百科的首頁地址顯示成 http://zh.wikipedia.org/wiki/Wikipedia:首頁 )

轉至Wiki

星期二, 3月 09, 2010

Silverlight Set Image


someImage = new New ImageBrush() With {.ImageSource = New BitmapImage(New Uri("folder/img.jpg", UriKind.Relative))}

星期二, 3月 02, 2010

GetURl

public class GetUrl
{
public static string GetAbsoluteUrl(string strRelativePath)
{
if (string.IsNullOrEmpty(strRelativePath))
return strRelativePath;

string strFullUrl;
if (strRelativePath.StartsWith("http:", StringComparison.OrdinalIgnoreCase)
|| strRelativePath.StartsWith("https:", StringComparison.OrdinalIgnoreCase)
|| strRelativePath.StartsWith("file:", StringComparison.OrdinalIgnoreCase)
)
{
// 已經是絕對路徑。
strFullUrl = strRelativePath;
}
else
{
// 是相對路徑,必須轉換成絕對路徑。
strFullUrl = System.Windows.Application.Current.Host.Source.AbsoluteUri;
if (strFullUrl.IndexOf("ClientBin") > 0)
strFullUrl = strFullUrl.Substring(0, strFullUrl.IndexOf("ClientBin")) + strRelativePath;
else
strFullUrl = strFullUrl.Substring(0, strFullUrl.LastIndexOf("/") + 1) + strRelativePath;
}

return strFullUrl;
}
}

===============================================
Public Class GetUrl
Public Shared Function GetAbsoluteUrl(ByVal strRelativePath As String) As String
Dim strFullUrl As String

If String.IsNullOrEmpty(strRelativePath) Then
Return strRelativePath
End If



If strRelativePath.StartsWith("http:", StringComparison.OrdinalIgnoreCase) _
Or strRelativePath.StartsWith("https:", StringComparison.OrdinalIgnoreCase) _
Or strRelativePath.StartsWith("file:", StringComparison.OrdinalIgnoreCase) Then
strFullUrl = strRelativePath
Else
strFullUrl = System.Windows.Application.Current.Host.Source.AbsoluteUri

If (strFullUrl.IndexOf("ClientBin") > 0) Then
strFullUrl = strFullUrl.Substring(0, strFullUrl.IndexOf("ClientBin")) + strRelativePath
Else
strFullUrl = strFullUrl.Substring(0, strFullUrl.LastIndexOf("/") + 1) + strRelativePath
End If
End If


Return strFullUrl
End Function
End Class

Lambda for VB

Dim checkName As Func(Of String, Boolean) = _
Function(x As String) _
If(x = "Cabernet Sauvignon", True, False)

星期五, 9月 19, 2008

自訂事件

public delegate void myEventHandle(object sender, myEventArgs e);
public event myEventHandle myEvent;

.
.
.

protected void onEvent(object sender, myEventArgs e)
{
if (myEvent!= null)
{
myEvent(sender, e);
}
}

星期三, 6月 04, 2008

以loop透過取代變數名稱設定值

Dictionary variables = new Dictionary();

foreach( string temp in words )
{
variables[ temp + "_buy" ] = "4";
}

星期四, 5月 15, 2008

畫透明背景範例

http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx

星期三, 5月 14, 2008

剪貼簿使用

string str = (string)Clipboard.GetData(DataFormats.StringFormat);

Clipboard.SetDataObject("test");

把檔案拖曳到視窗上

private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
string[] s = (string[]) e.Data.GetData(DataFormats.FileDrop, false);
int i;
for(i = 0; i < s.Length; i++)
listBox1.Items.Add(s[i]);
}

//參考http://support.microsoft.com/kb/307966/en-us


星期三, 3月 19, 2008

十捨五入文字輸出

//十捨五入文字輸出
public string toString(double d)
{
string str = string.Format("{0:###0.000}", d);
if (str.ToCharArray()[str.Length - 1] >= '5')
d += 0.005;


return string.Format("{0:###0.##}", d);
}

星期六, 3月 01, 2008

c# 控制滑鼠

[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, int extraInfo);
private enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}

//mouse move
public void myMoseMove(Point p)
{
mouse_event(MouseEventFlag.Move, p.X - Cursor.Position.X, p.Y - Cursor.Position.Y, 0, 0);
}

//mouse click
public void myMouseClick(Point p)
{
Cursor.Position = p;
mouse_event(MouseEventFlag.LeftDown, p.X, p.Y, 0, 0);
mouse_event(MouseEventFlag.LeftUp, p.X, p.Y, 0, 0);
}

//mouse down
public void myMouseDown(Point p)
{
Cursor.Position = p;
mouse_event(MouseEventFlag.LeftDown, p.X, p.Y, 0, 0);
}

//mouse up
public void myMouseUp(Point p)
{
Cursor.Position = p;
mouse_event(MouseEventFlag.LeftUp, p.X, p.Y, 0, 0);
}

//mouse double click
public void myMouseDoubleClick(Point p)
{
Cursor.Position = p;
myMouseClick(p);
myMouseClick(p);
}

星期五, 2月 29, 2008

c# 跨執行序處理

CheckForIllegalCrossThreadCalls = false;


如果不是要做啥特殊的處理,就暫時這樣做了

星期日, 2月 24, 2008

c# 執行檔案

const int ERROR_FILE_NOT_FOUND = 2;
const int ERROR_ACCESS_DENIED = 5;

try
{
process1.StartInfo.FileName = Path;
process1.StartInfo.CreateNoWindow = true;
process1.Start();
}
catch (Win32Exception ee)
{
if (ee.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(ee.Message + ". Check the path.");
}

else if (ee.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(ee.Message +
". You do not have permission to print this file.");
}
}

c# 畫透明圖

private void drawTransparentImage()
{
Graphics g = pictureBox1.CreateGraphics();

Bitmap bitmap = new Bitmap(Resources.PDA1, pictureBox1.Size);
float[][] ptsArray ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, f, 0},
new float[] {0, 0, 0, 0, 1}};

ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
ImageAttributes imgAttributes = new ImageAttributes();
imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0, 0, bitmap.Width, bitmap.Height,
GraphicsUnit.Pixel, imgAttributes);

g.Dispose();
}

c# 計算兩張圖箭頭連線的點

private Point calpointer(PictureBox pics, PictureBox picd)
{
Point pd = new Point(picd.Location.X + picd.Size.Width / 2, picd.Location.Y + picd.Size.Height / 2);
Point ps = new Point(pics.Location.X + pics.Size.Width / 2, pics.Location.Y + pics.Size.Height / 2);
Point p1 = new Point();
Point p2 = new Point();

float m = (float)(pd.Y - ps.Y) / (pd.X - ps.X);
float d1, d2;
float x1 = 0, x2 = 0, y1 = 0, y2 = 0;


if (ps.X == pd.X)
{
if (ps.Y > pd.Y)
return new Point(pd.X, picd.Location.Y + picd.Size.Height);
else
return new Point(pd.X, picd.Location.Y);
}
else if (ps.Y == pd.Y)
{
if (ps.X > pd.X)
return new Point(picd.Location.X + picd.Size.Width, pd.Y);
else
return new Point(picd.Location.X, pd.Y);
}
else if (ps.X <= pd.X && ps.Y <= pd.Y)
{
x1 = pd.X - picd.Size.Width / 2;
y1 = pd.Y - (picd.Size.Width / 2) * m;
if (m != 0)
x2 = pd.X - picd.Height / (2 * m);
else
x2 = pd.X;
y2 = pd.Y - picd.Height / 2;

}
else if (ps.X >= pd.X && ps.Y <= pd.Y)
{
x1 = pd.X + picd.Size.Width / 2;
y1 = pd.Y + (picd.Size.Width / 2) * m;
if (m != 0)
x2 = pd.X - picd.Height / (2 * m);
else
x2 = pd.X;
y2 = pd.Y - picd.Height / 2;
}
else if (ps.X >= pd.X && ps.Y >= pd.Y)
{
x1 = pd.X + picd.Size.Width / 2;
y1 = pd.Y + (picd.Size.Width / 2) * m;
if (m != 0)
x2 = pd.X + picd.Height / (2 * m);
else
x2 = pd.X;
y2 = pd.Y + picd.Height / 2;
}
else if (ps.X <= pd.X && ps.Y >= pd.Y)
{
x1 = pd.X - picd.Size.Width / 2;
y1 = pd.Y - (picd.Size.Width / 2) * m;
if (m != 0)
x2 = pd.X + picd.Height / (2 * m);
else
x2 = pd.X;
y2 = pd.Y + picd.Height / 2;
}

p1.X = (int)x1;
p1.Y = (int)y1;
p2.X = (int)x2;
p2.Y = (int)y2;

d1 = (p1.X - pd.X) * (p1.X - pd.X) + (p1.Y - pd.Y) * (p1.Y - pd.Y);
d2 = (p2.X - pd.X) * (p2.X - pd.X) + (p2.Y - pd.Y) * (p2.Y - pd.Y);



if (d1 > d2)
return p2;
else
return p1;

}

星期六, 2月 23, 2008

c# 自畫圖片在PictureBox上

PictureBox pic = new PictureBox();
Bitmap bitmap = new Bitmap(Resources.Ellipse_blue_1); //隨便一張圖
Graphics g = Graphics.FromImage(bitmap);
pic.Image = bitmap;
pic.SizeMode = PictureBoxSizeMode.AutoSize;
g.Dispose();

c# 計算字串的寬度

public static int CharWidth(string str, System.Drawing.Font font)
{
string target = str;
System.Windows.Forms.Control control = new System.Windows.Forms.Control();
System.Drawing.Graphics g = control.CreateGraphics();
System.Drawing.SizeF sizef = g.MeasureString(target, font);
g.Dispose(); // Necesary to destroy the graphics object
int charWidth = (int)sizef.Width; // gets the width of the size object.
return charWidth;
}