windows api static text控件的使用
小弟是个新手,刚学,想问下,请哪位大侠给个静态文本的简单例子,包括控件的创建之类的。(要求一定要控件,不要TEXTOUT。。。)...
小弟是个新手,刚学,想问下,请哪位大侠给个静态文本的简单例子,包括控件的创建之类的。(要求一定要控件,不要TEXTOUT。。。)
展开
1个回答
展开全部
MSDN中的API说明最完全 不过都是英文的。。。
写了个程序 可以枚举当前操作系统中的所有顶层窗口和子窗口(控件)
是个完整的程序。
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace EnumWindows
{
class Program
{
private static Form form;
private static TreeView tree;
private static TextBox text;
private const uint WM_GETTEXT = 0x000D;
private static WNDENUMPROC wEnumProc;
private delegate bool WNDENUMPROC(IntPtr hwnd, TreeNode lParam); // 枚举过程委托
[DllImport("user32.dll")]
private static extern bool EnumChildWindows(IntPtr hWndParant, WNDENUMPROC lpEnumFunc, TreeNode lParam); // 枚举子窗口(控件)
[DllImport("user32.dll")]
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam); // 枚举顶层窗口
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam); // 获得窗口标题(文本)
static void Main(string[] args)
{
form = new Form();
tree = new TreeView();
text = new TextBox();
form.Text = "Enum Windows";
form.ClientSize = new Size(600, 500);
form.Controls.Add(tree);
form.Controls.Add(text);
form.Load += new EventHandler(form_Load);
tree.Location = Point.Empty;
tree.Size = new Size(300, 500);
tree.AfterSelect += new TreeViewEventHandler(tree_AfterSelect);
text.Location = new Point(300, 0);
text.Size = new Size(300, 500);
text.Multiline = true;
text.Enabled = false;
Application.Run(form);
}
private static void form_Load(object sender, EventArgs e)
{
wEnumProc = new WNDENUMPROC(EnumWindowProc);
EnumWindows(wEnumProc, 0);
}
private static void tree_AfterSelect(object sender, EventArgs e)
{
text.Clear();
if (null == tree.SelectedNode) return;
text.Text = tree.SelectedNode.Tag.ToString();
}
private static bool EnumWindowProc(IntPtr hwnd, TreeNode lParam)
{
StringBuilder wndtext = new StringBuilder(1024);
SendMessage(hwnd, WM_GETTEXT, 128, wndtext);
if (0 == wndtext.Length) wndtext.Append("null");
if (null == lParam)
{
lParam = new TreeNode("0x" + hwnd.ToString("X8") + " - " + wndtext);
lParam.Tag = wndtext;
tree.Nodes.Add(lParam);
}
else
{
TreeNode node = new TreeNode("0x" + hwnd.ToString("X8") + " - " + wndtext);
node.Tag = wndtext;
lParam.Nodes.Add(node);
}
EnumChildWindows(hwnd, wEnumProc, lParam); // 不想枚举子窗口(控件)就把这行注释了
return true;
}
}
}
写了个程序 可以枚举当前操作系统中的所有顶层窗口和子窗口(控件)
是个完整的程序。
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace EnumWindows
{
class Program
{
private static Form form;
private static TreeView tree;
private static TextBox text;
private const uint WM_GETTEXT = 0x000D;
private static WNDENUMPROC wEnumProc;
private delegate bool WNDENUMPROC(IntPtr hwnd, TreeNode lParam); // 枚举过程委托
[DllImport("user32.dll")]
private static extern bool EnumChildWindows(IntPtr hWndParant, WNDENUMPROC lpEnumFunc, TreeNode lParam); // 枚举子窗口(控件)
[DllImport("user32.dll")]
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam); // 枚举顶层窗口
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam); // 获得窗口标题(文本)
static void Main(string[] args)
{
form = new Form();
tree = new TreeView();
text = new TextBox();
form.Text = "Enum Windows";
form.ClientSize = new Size(600, 500);
form.Controls.Add(tree);
form.Controls.Add(text);
form.Load += new EventHandler(form_Load);
tree.Location = Point.Empty;
tree.Size = new Size(300, 500);
tree.AfterSelect += new TreeViewEventHandler(tree_AfterSelect);
text.Location = new Point(300, 0);
text.Size = new Size(300, 500);
text.Multiline = true;
text.Enabled = false;
Application.Run(form);
}
private static void form_Load(object sender, EventArgs e)
{
wEnumProc = new WNDENUMPROC(EnumWindowProc);
EnumWindows(wEnumProc, 0);
}
private static void tree_AfterSelect(object sender, EventArgs e)
{
text.Clear();
if (null == tree.SelectedNode) return;
text.Text = tree.SelectedNode.Tag.ToString();
}
private static bool EnumWindowProc(IntPtr hwnd, TreeNode lParam)
{
StringBuilder wndtext = new StringBuilder(1024);
SendMessage(hwnd, WM_GETTEXT, 128, wndtext);
if (0 == wndtext.Length) wndtext.Append("null");
if (null == lParam)
{
lParam = new TreeNode("0x" + hwnd.ToString("X8") + " - " + wndtext);
lParam.Tag = wndtext;
tree.Nodes.Add(lParam);
}
else
{
TreeNode node = new TreeNode("0x" + hwnd.ToString("X8") + " - " + wndtext);
node.Tag = wndtext;
lParam.Nodes.Add(node);
}
EnumChildWindows(hwnd, wEnumProc, lParam); // 不想枚举子窗口(控件)就把这行注释了
return true;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询