顯示具有 視窗 標籤的文章。 顯示所有文章
顯示具有 視窗 標籤的文章。 顯示所有文章

2013年8月21日 星期三

隨著使用者輸入文字的多少,改變TextBox的形狀大小

private void textBox1_TextChanged(object sender, EventArgs e) {

 Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font); 
 textBox1.Width = size.Width; textBox1.Height = size.Height; 

 }

參考
http://stackoverflow.com/questions/8048455/c-sharp-resize-textbox-to-fit-content

改變Textbox中的文字顏色

  • TextBox中的ForeColor屬性可以改變整個文字的顏色。
  • 要改變部分文字的顏色,只有使用RichTextBox控制項。選取要變色的文字,然後用SelectionColor屬性來改變所選取的文字顏色。

移除在Textbox中按下Enter鍵,會出現的嗶嗶聲

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
{  
    switch (e.KeyChar )  
    {  
 case '\r':  
     // perform necessary action  
     e.Handled = true;  
       
//do something  
//Run command  
     if (e.KeyCode.Equals(Keys.Enter))  
     {  
     }  
     break;  
   
    }  
}  

Textbox捲到最末行

//move the caret to the end of the text  
textBoxLog.SelectionStart = textBoxLog.TextLength;  
//scroll to the caret  
textBoxLog.ScrollToCaret();  

參考
http://stackoverflow.com/questions/1069497/how-to-scroll-down-in-a-textbox-by-code-in-c-sharp

C#使用Listview的Key


private void button1_Click(object sender, EventArgs e)   
{   
 for (int i = 0; i < 4; i++)   
 {   
  int test = i * i;   
  listView1.Items.Add("key" + i.ToString(), test.ToString(), i);   
 }   
}   
  
private void button2_Click(object sender, EventArgs e)   
{   
 for (int i = 0; i < 4; i++)   
 {   
  MessageBox.Show(listView1.Items["key" + i.ToString()].Text);   
 }   
} 
  

參考
http://zhidao.baidu.com/question/85025088

2011年7月4日 星期一

視窗截圖

/* 目標視窗必須顯示在最上層
 * 引入 Win32 API 中的 User32.DLL
 * 需要加上 using System.Runtime.InteropServices;
 */
[DllImport("user32.dll")]
public static extern Boolean GetWindowRect(IntPtr hWnd, ref Rectangle bounds);

public void CaptureWindow () {
  /* 取得目標視窗的 Handle
   * 需要加上 using System.Diagnostics;
   */
  Process[] process = Process.GetProcessesByName("notepad");

  /* 取得該視窗的大小與位置 */
  Rectangle bounds;
  GetWindowRect(process[0].MainWindowHandle, ref bounds);

  /* 抓取截圖 */
  Bitmap screenshot = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
  Graphics gfx = Graphics.FromImage(screenshot);
  gfx.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);

  /* 利用 PictureBox 顯示出來 */
  imageView.Image = (Image) screenshot;
  imageView.Update();
}

用視窗標題激活視窗

//最大化視窗無法使用
using System.Runtime.InteropServices;

[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string
        lpWindowName);

[DllImport("user32.dll")]
static extern bool ShowWindow(int hWnd, int nCmdShow);

private void SetActiveWindow()
{
            //If the window state of the inactive window is maximize, this code doesn't work.
            int WINDOW_HANDLER = FindWindow(null, @"Piriform CCleaner");
            if (WINDOW_HANDLER == 0)
            { }
            else
            {
                ShowWindow(WINDOW_HANDLER, 3);
            }
}

2011年3月17日 星期四

設定Listbox的拖放

設定Listbox的AllowDrop屬性為true。
this.listBox_FileList.AllowDrop = true;
編輯DragEnter事件。
private void listBox_FileList_DragEnter(object sender, DragEventArgs e)
{
 // 確定使用者抓進來的是檔案
 if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
 {
  // 允許拖拉動作繼續 (這時滑鼠游標應該會顯示 +)
  e.Effect = DragDropEffects.All;
 }
}
編輯DragDrop事件。
private void listBox_FileList_DragDrop(object sender, DragEventArgs e)
{
 string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
 foreach (string file in files)
 {
  listBox_FileList.Items.Add(file);
 }
}

拖放圖片到PictureBox

設定Form1的AllowDrop屬性為true。
 Form1.AllowDrop = true;
編輯DragEnter事件。
private void Form1_DragEnter(object sender, DragEventArgs e)
{
 e.Effect = DragDropEffects.Move;
}
編輯DragDrop事件。
private void Form1_DragDrop(object sender, DragEventArgs e)
{
 int x = this.PointToClient(new Point(e.X, e.Y)).X;
 int y = this.PointToClient(new Point(e.X, e.Y)).Y;
 if(x >= pictureBox1.Location.X && x <= pictureBox1.Location.X + pictureBox1.Width && y >= pictureBox1.Location.Y && y <= pictureBox1.Location.Y + pictureBox1.Height)
 {
  string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  pictureBox1.Image = Image.FromFile(files[0]);
 }
}

2011年1月25日 星期二

在Form加入MouseWhell事件

在建構式中加入(InitalizeComponents();之後):
this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);

2011年1月3日 星期一

ListBox自動捲到最後一個項目

ListBox lbLog;
int itemsPerPage = (int)(lbLog.Height / lbLog.ItemHeight);
lbLog.TopIndex = lbLog.Items.Count - itemsPerPage;

2010年12月31日 星期五

設定控制項Panel有ScrollBar

將屬性AutoScroll設為true

為控制項畫背景

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace DevDistrict.Sample
{
 public class GradientPanel : System.Windows.Forms.Panel
 {
  protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint (e);

   Graphics g = e.Graphics;

   g.SmoothingMode = SmoothingMode.HighQuality;
   GraphicsPath gPath = new GraphicsPath();

   Rectangle r = new Rectangle(0,0,this.Width,this.Height);
   gPath.AddRectangle(r);

   LinearGradientBrush lb = new LinearGradientBrush(r,Color.White,Color.Blue,LinearGradientMode.Vertical);

   g.FillPath(lb,gPath);
  }
 }
}

使用NotifyIcon

private void niTray_Click(object sender, EventArgs e)
{
 Show();
 this.ShowInTaskbar = true;
 this.WindowState = FormWindowState.Normal;
}

使用ProgressBar控制項

this.progressBar1 = new System.WinForms.ProgressBar();
progressBar1.Maximum = 200;
progressBar1.Manimum=0;
progressBar1.Step=20;

//使用ProgressBar控制項
protected void timer1_Tick (object sender, System.EventArgs e)
{
 if (progressBar1.Value >= 200 )
 {
  progressBar1.Value = 0;
 }
 progressBar1.Value += 20;//同progressBar1.PerformStep();
 Console.WriteLine(progressBar1.Value.ToString() );//取得值
}

使用剪貼簿

設定剪貼簿
private void button1_Click ( object sender , System.EventArgs e )
{
 if ( textBox1.SelectedText != "" )
 {
  Clipboard.SetDataObject ( textBox1.SelectedText ) ;
  //第二個參數為true時,複製後的內容能被其他程式使用, 為false表示複製後的內容不為其他程式使用
  Clipboard.SetDataObject("hello",true);
 }
 else
 {
  textBox2.Text = "在textBox1中沒有選定的內容";
 }
}

讀取剪貼簿
private void button2_Click ( object sender , System.EventArgs e )
{
 IDataObject iData = Clipboard.GetDataObject ( ) ;
 //檢測數據是否是可以使用的格式,即文本格式
 if ( iData.GetDataPresent ( DataFormats.Text ) )
 {
  //是可以使用的格式,就在textbox2中顯示剪貼簿中的內容
  textBox2.Text = ( String ) iData.GetData ( DataFormats.Text ) ;
 }
 else
 {
  //如果不是
  textBox2.Text = "沒有從剪貼簿中接收到數據!" ;
 }
}

2010年12月30日 星期四

用LinkLabel控制項寄信與開網頁

//用預設郵件程式寄信
System.Diagnostics.Process.Start("Mailto:no@spam.com")

//用預設瀏覽器開網頁
System.Diagnostics.Process.Start("http:\\msdn.microsoft.com")

單鍵關閉視窗

public Form1()
{
InitializeComponent();
this.KeyPreview = true; //Form handles keyinput 

}

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
Close();
}
}