Sunday, 7 November 2010

Thủ thuật Winform C# - P2

Tiếp theo những thủ thuật winform với C#

>>>Làm thế nào cho form xuất hiện tại bên dưới góc phải trên taskbar nhỉ?
Đặt thuộc tính SetPosition của form = Manual

Code:
SetBounds( Screen.GetWorkingArea( this ).Width - Width,
  Screen.GetWorkingArea( this ).Height - Height, Width, Height );
>>>Cấm user di chuyển form?
Code:
protected override void WndProc( ref Message m )
{
  const int WM_NCLBUTTONDOWN = 161;
  const int WM_SYSCOMMAND = 274;
  const int HTCAPTION = 2;
  const int SC_MOVE = 61456;

  if ( (m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE) )
    return;
  if ( (m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION) )
    return;

  base.WndProc( ref m );
}
>>Làm thế nào di chuyển một form mà không có border?
Code:
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTCAPTION = 0x2;

[ DllImport( "user32.dll" ) ]
public static extern bool ReleaseCapture();

[ DllImport( "user32.dll" ) ]
public static extern int SendMessage( IntPtr hWnd, int Msg, int wParam, int lParam );

private void Form1_MouseDown( object sender, MouseEventArgs e )
{
  if ( e.Button == MouseButtons.Left )
  {
    ReleaseCapture();
    SendMessage( Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0 );
  }
}
>>>Làm thế nào thay đổi cursor của 1 control?
Code:
button1.Cursor = new Cursor( @"C:\winnt\cursors\hnodrop.cur" );
>>>Làm thế nào để convert một class cursor sang file *.cur nhỉ?
Code:
protected void WriteCursorToFile( Cursor cursor, string fileName )
{
  TypeConverter converter = TypeDescriptor.GetConverter( typeof( Cursor ) );
  byte[] blob = converter.ConvertTo( cursor, typeof( byte[] ) ) as byte[];
  if ( blob == null )
  {
    MessageBox.Show( "Unable to convert Cursor to byte[]" );
    return;
  }
  FileStream fileStream = new FileStream( fileName, FileMode.Create );
  fileStream.Write( blob, 0, blob.Length );
  fileStream.Flush();
  fileStream.Close();
}

No comments:

Post a Comment