开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 6015|回复: 7
收起左侧

[C#源码] C# FormWindow Class 公开调用方法 12种 写了差不多一小时

[复制链接]
结帖率:33% (3/9)
发表于 2012-11-30 19:42:50 | 显示全部楼层 |阅读模式   四川省成都市
1.png
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Runtime.InteropServices;

  8. namespace FormWindow.Cs
  9. {
  10.     public class FormWindow
  11.     {
  12.         public FormWindow FormWindow = new FormWindow();
  13.         [DllImport("user32.dll", EntryPoint = "FindWindowA")]
  14.         private static extern int FindWindowA(string a, string b);
  15.         public int 标题取窗口句柄(string Text)
  16.         {
  17.             string[] a = new string[2];
  18.             a[1]=Text;
  19.             return FindWindowA(a[0],a[1]);
  20.         }
  21.         public int 类名取窗口句柄(string ClassName)
  22.         {
  23.             string[] a = new string[2];
  24.             a[0] = ClassName;
  25.             return FindWindowA(a[0], a[1]);
  26.         }
  27.         [DllImport("user32.dll", EntryPoint = "IsWindow")]
  28.         private static extern int IsWindow(int Hwnd);
  29.         public bool 句柄是否有效(int Hwnd)
  30.         {
  31.             if (IsWindow(Hwnd) > 0)
  32.             {
  33.                 return true;
  34.             }
  35.             else
  36.             {
  37.                 return false;
  38.             }
  39.         }
  40.         [DllImport("user32.dll", EntryPoint = "FlashWindow")]
  41.         private static extern int FlashWindow(int HWnd, bool IsTrue);
  42.         public void 闪烁窗口(int 窗口句柄)
  43.         {
  44.             FlashWindow(窗口句柄, true);
  45.         }
  46.         [DllImport("user32.dll", EntryPoint = "GetActiveWindow")]
  47.         private static extern int GetActiveWindow();
  48.         public int 取活动窗口句柄()
  49.         {
  50.             return GetActiveWindow();
  51.         }
  52.         [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
  53.         private static extern int WindowFromPoint(int x, int y);
  54.         public int 坐标取窗口句柄(int x, int y)
  55.         {
  56.             return WindowFromPoint(x, y);
  57.         }
  58.         [DllImport("user32.dll", EntryPoint = "AnimateWindow")]
  59.         private static extern int AnimateWindow(int hwnd, int dwTime, int dwFlags);
  60.         public int 动画窗口(int 窗口句柄, int 持续时间, int 显示方式)
  61.         {
  62.             return AnimateWindow(窗口句柄, 持续时间, 显示方式);
  63.         }
  64.         [DllImport("user32.dll", EntryPoint = "MessageBoxA")]
  65.         private static extern int MessageBoxA(int Hwnd, string Tip, string Text, int val);
  66.         public int 信息框
  67.         (int 窗口句柄, string 提示信息, string 信息框标题, int 按钮值)
  68.         {
  69.             return MessageBoxA(窗口句柄, 提示信息, 信息框标题, 按钮值);
  70.         }
  71.         [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
  72.         private static extern int GetDesktopWindow();
  73.         public static extern int 取屏幕句柄()
  74.         {
  75.             return GetDesktopWindow();
  76.         }
  77.         [DllImport("user32.dll", EntryPoint = "IsZoomed")]
  78.         private static extern int IsZoomed(int Hwnd);
  79.         public bool 窗口是否最大化(int Hwnd)
  80.         {
  81.             if(IsZoomed (Hwnd)!=0)
  82.             {
  83.                 return true;
  84.             }
  85.             else
  86.             {
  87.                 return false;
  88.             }

  89.         }
  90.         [DllImport("user32.dll", EntryPoint = "GetWindowTextA")]
  91.         private static extern int GetWindowTextA(int Hwnd, string cch, int lp);
  92.         public string 取窗口标题(int Hwnd)
  93.         {
  94.             string a = "";
  95.             GetWindowTextA(Hwnd, a, 255);
  96.             return a;
  97.         }
  98.     }
  99. }
复制代码
FormWindow.Cs.rar (7.69 KB, 下载次数: 15)

点评

还有数据库的操作方面的C#代码 本人是不考虑发的...作为程序员 代码要保密的...不然自己白写代码?   四川省成都市  发表于 2012-11-30 19:44

结帖率:100% (2/2)
发表于 2013-5-5 10:10:58 | 显示全部楼层   湖南省湘西土家族苗族自治州
本帖最后由 陌笔圣手 于 2013-5-5 11:04 编辑

测了下 GetWindowTextA 无效,查了下MSDN第二个参数是 要求传址 ref

//补充,经测试我也是错的。。
   百度文库看了参数说明,

hWnd [in]
Type: HWND
A handle to the window or control containing the text.
一个指向特定窗口或控件的句柄。
lpString [out]Type: LPTSTR
The buffer that will receive the text. If the string is as long or longer than the buffer, the string is truncated and terminated with a null character.
一个用来接受文本的缓冲区。如果(取回的)字符串与缓冲区等长或者比缓冲区长,那么该字串会被截断,并用NULL字符结束。
nMaxCount [in]Type: int
The maximum number of characters to copy to the buffer, including the null character. If the text exceeds this limit, it is truncated.
将要拷贝到缓冲区中的字符的最大个数,包括NULL字符。如果目标TEXT超出限制则会被截断。
最后 经测试正确的

[DllImport("user32.dll")]
        public static extern int GetWindowTextA(int hwnd, StringBuilder Title,int Length);
           StringBuilder Title=new StringBuilder(256);//缓冲区
           GetWindowTextA(hwnd, Title, 256);
           MessageBox.Show( Title );



回复 支持 反对

使用道具 举报

签到天数: 9 天

发表于 2013-3-30 15:36:28 | 显示全部楼层   广西壮族自治区南宁市
有用吗?
回复 支持 反对

使用道具 举报

结帖率:0% (0/1)
发表于 2013-3-17 12:42:13 | 显示全部楼层   广东省广州市
本帖最后由 猫星人 于 2013-3-17 12:43 编辑

额,如何使用呢。。。路过

打开编译说:错误        1        “FormWindow”: 成员名称不能与它们的封闭类型相同        xxxC#==\FormWindow.Cs\FormWindow.Cs\FormWindow.cs        13        27        FormWindow.Cs
回复 支持 反对

使用道具 举报

发表于 2013-2-24 08:57:28 | 显示全部楼层   河南省驻马店市
算了,用VS2008的没戏了
回复 支持 反对

使用道具 举报

发表于 2013-2-5 18:49:54 | 显示全部楼层   河北省廊坊市
  刷主题可好玩
回复 支持 反对

使用道具 举报

结帖率:21% (7/33)
发表于 2012-11-30 20:14:01 | 显示全部楼层   福建省厦门市
刷主题可好玩
回复 支持 反对

使用道具 举报

结帖率:100% (12/12)
发表于 2012-11-30 19:46:04 | 显示全部楼层   广东省珠海市
          刷主题可好玩
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表