c#改变系统鼠标
---------------------------------------------------------------------------------------------------
using System.Runtime.InteropServices;
[DllImport("User32.DLL")]
public static extern bool SetSystemCursor(IntPtr hcur, uint id);
public const uint OCR_NORMAL = 32512;
public const uint OCR_IBEAM = 32513;
[DllImport("User32.DLL")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam,
IntPtr pvParam, uint fWinIni);
public const uint SPI_SETCURSORS = 87;
public const uint SPIF_SENDWININICHANGE = 2;
private void button1_Click(object sender, EventArgs e)
{
//设置
SetSystemCursor(Cursors.WaitCursor.CopyHandle(), OCR_NORMAL);
SetSystemCursor(Cursors.WaitCursor.CopyHandle(), OCR_IBEAM);
//..可以根据情况加
}
private void button2_Click(object sender, EventArgs e)
{
//恢复
SystemParametersInfo(SPI_SETCURSORS, 0, IntPtr.Zero, SPIF_SENDWININICHANGE);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zswang/archive/2007/03/20/1535309.aspx
------------------------------------------------------------------------------------------------------------
C#鼠标拖动控件改变位置并绘制虚框
-------------------------------------------------------------------------------------------------------------
private Point downPoint;
private Rectangle downRectangle;
private Rectangle lastRectangle;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
downPoint = e.Location;
downRectangle =
new Rectangle(0, 0, ((Control)sender).Width, pictureBox1.Height);
downRectangle.Offset(((Control)sender).PointToScreen(new Point(0, 0)));
ControlPaint.DrawReversibleFrame(
downRectangle, Color.White, FrameStyle.Thick);
lastRectangle = downRectangle;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
ControlPaint.DrawReversibleFrame(
lastRectangle, Color.White, FrameStyle.Thick);
Rectangle rectangle = downRectangle;
rectangle.Offset(e.X - downPoint.X, e.Y - downPoint.Y);
ControlPaint.DrawReversibleFrame(
rectangle, Color.White, FrameStyle.Thick);
lastRectangle = rectangle;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
ControlPaint.DrawReversibleFrame(
lastRectangle, Color.White, FrameStyle.Thick);
pictureBox1.Location = new Point(
((Control)sender).Location.X + e.X - downPoint.X,
((Control)sender).Location.Y + e.Y - downPoint.Y);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zswang/archive/2007/03/31/1547770.aspx
=------------------------------------------------------------------------------------------------
枚举当前系统用户
-------------------------------------------------------------------------------------------------
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct USER_INFO_0
{
public string Username;
}
[DllImport("Netapi32.dll")]
extern static int NetUserEnum(
[MarshalAs(UnmanagedType.LPWStr)]
string servername,
int level,
int filter,
out IntPtr bufptr,
int prefmaxlen,
out int entriesread,
out int totalentries,
out int resume_handle);
[DllImport("Netapi32.dll")]
extern static int NetApiBufferFree(IntPtr Buffer);
private void button1_Click(object sender, EventArgs e)
{
int EntriesRead;
int TotalEntries;
int Resume;
IntPtr bufPtr;
NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead,
out TotalEntries, out Resume);
if (EntriesRead > 0)
{
USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
IntPtr iter = bufPtr;
for (int i = 0; i < EntriesRead; i++)
{
Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter,
typeof(USER_INFO_0));
iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
textBox1.AppendText(Users[i].Username + "/r/n");
}
NetApiBufferFree(bufPtr);
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zswang/archive/2007/04/23/1576495.aspx
-------------------------------------------------------------------------------------------------------
C#中获得系统当前鼠标的图案
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public int cbSize;
public int flags;
public IntPtr hCursor;
public Point ptScreenPos;
}
[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);
private const int CURSOR_SHOWING= 0x00000001;
private void button1_Click(object sender, EventArgs e)
{
CURSORINFO vCurosrInfo;
vCurosrInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
GetCursorInfo(out vCurosrInfo);
if ((vCurosrInfo.flags & CURSOR_SHOWING) != CURSOR_SHOWING) return;
Cursor vCursor = new Cursor(vCurosrInfo.hCursor);
Graphics vGraphics = Graphics.FromHwnd(Handle);
Rectangle vRectangle = new Rectangle(0, 0, 32, 32);
vGraphics.FillRectangle(new SolidBrush(BackColor), vRectangle);
vCursor.Draw(vGraphics, vRectangle);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zswang/archive/2007/04/26/1585096.aspx
-------------------------------------------------------------------------------------------------------------
C#中给进度条加上文字
-----------------------------------------------------------------------------------------------------------
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
public class SubWindow : NativeWindow
{
private Control control;
private string text = "null";
private Color foreColor = SystemColors.ControlText;
private Font font = SystemFonts.MenuFont;
public Font Font
{
set
{
font = value;
if (control != null)
control.Invalidate();
}
}
public Color ForeColor
{
set
{
foreColor = value;
if (control != null)
control.Invalidate();
}
}
public string Text
{
set
{
text = value;
if (control != null)
control.Invalidate();
}
}
public Control Control {
set
{
control = value;
if (control != null)
{
AssignHandle(control.Handle);
control.Invalidate();
}
}
}
protected override void WndProc(ref Message m)
{
const int WM_PAINT = 0x000F;
base.WndProc(ref m);
if (control == null) return;
switch (m.Msg)
{
case WM_PAINT:
IntPtr vDC = GetWindowDC(m.HWnd);
Graphics vGraphics = Graphics.FromHdc(vDC);
StringFormat vStringFormat = new StringFormat();
vStringFormat.Alignment = StringAlignment.Center;
vStringFormat.LineAlignment = StringAlignment.Center;
vGraphics.DrawString(text, font, new SolidBrush(foreColor),
new Rectangle(0, 0, control.Width, control.Height), vStringFormat);
ReleaseDC(m.HWnd, vDC);
break;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
SubWindow vSubWindow = new SubWindow();
vSubWindow.Font = Font;
vSubWindow.Text = "Zswang 路过";
vSubWindow.ForeColor = Color.Blue;
vSubWindow.Control = progressBar1;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zswang/archive/2007/06/17/1655904.aspx
------------------------------------------------------------------------------------------------------
获得快捷方式指向的路径
------------------------------------------------------------------------------------------------------
using System.Runtime.InteropServices;
[Flags()]
public enum SLR_FLAGS
{
SLR_NO_UI = 0x1,
SLR_ANY_MATCH = 0x2,
SLR_UPDATE = 0x4,
SLR_NOUPDATE = 0x8,
SLR_NOSEARCH = 0x10,
SLR_NOTRACK = 0x20,
SLR_NOLINKINFO = 0x40,
SLR_INVOKE_MSI = 0x80
}
[Flags()]
public enum SLGP_FLAGS
{
SLGP_SHORTPATH = 0x1,
SLGP_UNCPRIORITY = 0x2,
SLGP_RAWPATH = 0x4
}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
// Unicode version
public struct WIN32_FIND_DATA
{
public int dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwReserved0;
public int dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
private const int MAX_PATH = 260;
}
[
ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("000214F9-0000-0000-C000-000000000046")
]
// Unicode version
public interface IShellLink
{
void GetPath(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
int cchMaxPath,
out WIN32_FIND_DATA pfd,
SLGP_FLAGS fFlags);
void GetIDList(
out IntPtr ppidl);
void SetIDList(
IntPtr pidl);
void GetDescription(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName,
int cchMaxName);
void SetDescription(
[MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir,
int cchMaxPath);
void SetWorkingDirectory(
[MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs,
int cchMaxPath);
void SetArguments(
[MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotkey(
out short pwHotkey);
void SetHotkey(
short wHotkey);
void GetShowCmd(
out int piShowCmd);
void SetShowCmd(
int iShowCmd);
void GetIconLocation(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
int cchIconPath,
out int piIcon);
void SetIconLocation(
[MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,
int iIcon);
void SetRelativePath(
[MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,
int dwReserved);
void Resolve(
IntPtr hwnd,
SLR_FLAGS fFlags);
void SetPath(
[MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
[
ComImport(),
Guid("00021401-0000-0000-C000-000000000046")
]
public class ShellLink
{
}
private void Form1_Load(object sender, EventArgs e)
{
IShellLink vShellLink = (IShellLink)new ShellLink();
UCOMIPersistFile vPersistFile = vShellLink as UCOMIPersistFile;
vPersistFile.Load(@"c:/temp/temp.lnk", 0);
StringBuilder vStringBuilder = new StringBuilder(260);
WIN32_FIND_DATA vWIN32_FIND_DATA;
vShellLink.GetPath(vStringBuilder, vStringBuilder.Capacity,
out vWIN32_FIND_DATA, SLGP_FLAGS.SLGP_RAWPATH);
Text = vStringBuilder.ToString();
}
------------------------------------------------------------------------------------------------------
显示系统文件属性对话框
------------------------------------------------------------------------------------------------------------
using System.Runtime.InteropServices;
public struct ShellExecuteInfo
{
public int cbSize;
public uint fMask;
public IntPtr hwnd;
public string lpVerb;
public string lpFile;
public string lpParameters;
public string lpDirectory;
public int nShow;
public int hInstApp;
public int lpIDList;
public string lpClass;
public int hkeyClass;
public uint dwHotKey;
public int hIcon;
public int hProcess;
}
public const int SW_SHOW= 5;
public const uint SEE_MASK_INVOKEIDLIST= 12;
[DllImport("shell32.dll")]
public static extern bool ShellExecuteEx(ref ShellExecuteInfo lpExecInfo);
private void button1_Click(object sender, EventArgs e)
{
ShellExecuteInfo vShellExecuteInfo = new ShellExecuteInfo();
vShellExecuteInfo.cbSize = Marshal.SizeOf(vShellExecuteInfo);
vShellExecuteInfo.lpVerb = "properties";
vShellExecuteInfo.lpFile = @"c:/temp/temp.log";
vShellExecuteInfo.nShow = SW_SHOW;
vShellExecuteInfo.fMask = SEE_MASK_INVOKEIDLIST;
ShellExecuteEx(ref vShellExecuteInfo);
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zswang/archive/2007/07/26/1709491.aspx
-------------------------------------------------------------------------------------------------------------
获得剪贴板来源进程
-------------------------------------------------------------------------------------------------------------
using System.Runtime.InteropServices;
using System.Diagnostics;
[DllImport("user32.dll")]
public static extern IntPtr GetClipboardOwner();
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr handle,
out int processId);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr handle);
private void button1_Click(object sender, EventArgs e)
...{
IntPtr vOwner = GetClipboardOwner();
if (vOwner == IntPtr.Zero) return;
int vProcessId;
GetWindowThreadProcessId(vOwner, out vProcessId);
Process vProcess = Process.GetProcessById(vProcessId);
Text = vProcess.MainModule.FileName;
}
-----------------------------------------------------------------------------------------------------------------