代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
利用API给其它线程的各种控件赋值
作者:
dezai
/ 发布于
2014/1/26
/
336
[DllImport("User32.dll",EntryPoint="SendMessage")] publicstaticexternintSendMessage(IntPtrhWnd,intMsg,IntPtrwParam,stringlParam); [DllImport("user32.dll")] publicstaticexternIntPtrSendMessage(IntPtrhWnd,intMsg,intwParam, intlParam); //为可输入控件赋值 constintWM_SETTEXT=0x000C; //SendMessage((IntPtr)hwnd,WM_SETTEXT,(IntPtr)0,"123"); //模拟单击 constintWM_CLICK=0x00F5; //SendMessage((IntPtr)hwnd,WM_CLICK,(IntPtr)0,"0"); //为下拉控件赋值 constintWM_CLICK2=0x014D; //SendMessage((IntPtr)hwnd,WM_CLICK2,(IntPtr)0,"123"); //向下拉控件添加值 constintWM_CLICK3=0x0180; //SendMessage((IntPtr)hwnd,WM_CLICK3,(IntPtr)0,"123"); //展开日期下拉 constintwm_syskeydown=0x0104,vk_down=0x28; //SendMessage((IntPtr)hwnd,wm_syskeydown,vk_down,0); //为下拉列表和TreeView赋值 public class TreeApi { #region "API函数定义 " [DllImport("user32.dll ", CharSet = CharSet.Unicode)] private static extern int SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lparam); [DllImport("user32.dll ", CharSet = CharSet.Unicode)] private static extern int SendMessage(int hWnd, int msg, IntPtr wParam, TVITEM lparam); [DllImport("user32.dll ")] public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32.dll ")] public static extern int GetClassNameA(int hwnd, StringBuilder lpClassName, int nMaxCount); [DllImport("kernel32 ", CharSet = CharSet.Unicode)] public static extern int CopyMemory(StringBuilder Destination, IntPtr Source, int Length); [DllImport("kernel32 ", CharSet = CharSet.Unicode)] public static extern int GlobalAlloc(int wFlags, int dwBytes); [DllImport("kernel32 ", CharSet = CharSet.Unicode)] public static extern int GlobalFree(IntPtr hMem); #endregion #region "常量定义 " private const int TVGN_CARET = 0x00000009; private const int TVM_SELECTITEM = 0x0000110b; private const int TVGN_ROOT = 0x00000000; private const int TVM_GETNEXTITEM = 0x0000110a; private const int TVIF_TEXT = 0x0001; private const int TVGN_NEXT = 0x00000001; private const int TVM_GETITEM = 0x0000110c; private const int TVGN_CHILD = 0x00000004; private const int GMEM_FIXED = 0x0000; private const int TV_FIRST = 0x1100;// TreeView messages private const int TVM_DELETEITEM = (TV_FIRST + 1); private const int WM_CLICK = 0x00F5; private const int TVM_GETITEMRECT = (TV_FIRST + 4); #endregion /// <summary> /// 选取TreeView指定项 /// </summary> /// <param name= "TreeViewHwnd "> 树对象句柄 </param> /// <param name= "ItemHwnd "> 节点对象句柄 </param> /// <returns> 成功选中返回true 没找到返回false </returns> public static bool SelectNode(IntPtr TreeViewHwnd, IntPtr ItemHwnd) { int result = SendMessage(TreeViewHwnd.ToInt32(), TVM_SELECTITEM, new IntPtr(TVGN_CARET), ItemHwnd); if (result == 0) { return false; } else { return true; } } /// <summary> /// 获取TreeView根节点 /// </summary> /// <param name= "TreeViewHwnd "> 树对象句柄 </param> /// <returns> 成功返回根节点句柄 否则返回 0 </returns> public static IntPtr GetRoot(IntPtr TreeViewHwnd) { TVITEM tv = new TVITEM(); IntPtr hStr = Marshal.AllocHGlobal(1024); tv.hItem = TreeViewHwnd; tv.mask = TVIF_TEXT; tv.pszText = hStr; tv.cchTextMax = 1024; int result = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETNEXTITEM, new IntPtr(TVGN_ROOT), tv); Marshal.FreeHGlobal(hStr); return new IntPtr(result); } /// <summary> /// 获取同级下一节点句柄 /// </summary> /// <param name= "TreeViewHwnd "> 树对象句柄 </param> /// <param name= "PrevHwnd "> 上一项节点句柄 </param> /// <returns> 成功返回下一项节点句柄 否则返回 0 </returns> public static IntPtr GetNextItem(IntPtr TreeViewHwnd, IntPtr PrevHwnd) { int mbHwnd = 0; mbHwnd = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETNEXTITEM, new IntPtr(TVGN_NEXT), PrevHwnd); return new IntPtr(mbHwnd); } /// <summary> /// 获取第一个子节点句柄 /// </summary> /// <param name= "TreeViewHwnd "> 树对象句柄 </param> /// <param name= "ParentHwnd "> 父节点句柄 </param> /// <returns> 成功返回第一个子节点句柄 否则返回 0 </returns> public static IntPtr GetFirstChildItem(IntPtr TreeViewHwnd, IntPtr ParentHwnd) { int result = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETNEXTITEM, new IntPtr(TVGN_CHILD), ParentHwnd); return new IntPtr(result); } /// <summary> /// 获取指定项文本 /// </summary> /// <param name= "TreeViewHwnd "> 树对象句柄 </param> /// <param name= "ItemHwnd "> 节点句柄 </param> /// <returns> </returns> public static string GetItemText(IntPtr TreeViewHwnd, IntPtr ItemHwnd) { IntPtr hStr = Marshal.AllocHGlobal(1024); TVITEM tv = new TVITEM(); tv.hItem = ItemHwnd; tv.mask = TVIF_TEXT; tv.pszText = hStr; tv.cchTextMax = 1024; StringBuilder sb = new StringBuilder(1024); int result = SendMessage(TreeViewHwnd.ToInt32(), TVM_GETITEM, new IntPtr(0), tv); int iCopy = CopyMemory(sb, hStr, 1024); Marshal.FreeHGlobal(hStr); return sb.ToString(); } /// <summary> /// TV_ITEM结构 /// </summary> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class TVITEM { public int mask; public IntPtr hItem; public int state; public int stateMask; public IntPtr pszText; public int cchTextMax; public int iImage; public int iSelectedImage; public int cChildren; public IntPtr lParam; public int HTreeItem; } }
试试其它关键字
利用API给控件赋值
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3