首页
社区
课程
招聘
[求助]标题栏的最小化、最大化、关闭按钮是谁创建的?
发表于: 2010-6-10 15:45 4034

[求助]标题栏的最小化、最大化、关闭按钮是谁创建的?

2010-6-10 15:45
4034
已解决,谢谢各位的关注!
========================

最近是折腾窗口编程,程序中要判断鼠标光标下面的窗口是否为我的程序里生出来的。我使用的函数和语句是:
方法1:
if(GetWindowThreadProcessId(hWnd,&dwProcessID1)==(GetWindowThreadProcessId(hTempWnd,&dwProcessID2)
      MessageBox(NULL, "窗口同宗!","窗口比较",MB_OK);
方法2:
GetWindowThreadProcessId(hWnd,&dwProcessID1);
GetWindowThreadProcessId(hTempWnd,&dwProcessID2);
if(dwProcessID1==dwProcessID2)
       MessageBox(NULL, "窗口同宗!","窗口比较",MB_OK);

可发现,在处理过程中发现一些很奇怪的事情:
当鼠标指向自身程序主窗口的标题栏最小化、最大化、关闭按钮以及窗口的左边框、下边框时,以上两个关系表达式竟然都不成立!那:
1、标题栏的最小化、最大化、关闭按钮,以及窗口的左边框、下边框究竟是谁创建的?
2、使用什么办法才能判断出鼠标光标下面的窗口,是否为我自己的程序里生出来的呢?

请好心指点一二,先谢谢了!


#include "windows.h"

LRESULT CALLBACK WndProc ( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
	WNDCLASS wndclass;
	HWND hWnd;
	MSG msg;
	static TCHAR szClassName[] = TEXT ( "程序类" );
	
	RtlZeroMemory ( &wndclass, sizeof ( wndclass ) );
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.lpszClassName = szClassName;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = NULL;
	wndclass.hCursor = LoadCursor ( NULL, IDC_ARROW );
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //CreateSolidBrush ( RGB ( 160, 190, 160 ) ); //
	wndclass.lpszMenuName = NULL;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	
	if ( !RegisterClass ( &wndclass ) )
	{
		MessageBox ( NULL, TEXT ( "本程序需要运行在WinNT系列操作系统上!" ), TEXT ( "Error" ), MB_OK | MB_ICONWARNING );
	}
	
	
	hWnd = CreateWindow ( szClassName, TEXT ( "我的第N个程序" ), WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL, NULL, hInstance, NULL );
	ShowWindow ( hWnd, nShowCmd );
	UpdateWindow ( hWnd );
	
	while ( GetMessage ( &msg, NULL, 0, 0 ) )
	{
		TranslateMessage ( &msg );
		DispatchMessage ( &msg );
	}
	
	return msg.wParam;
}

#define IDE_KEY 100
LRESULT CALLBACK WndProc ( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	HINSTANCE hInstance=NULL;

	POINT pt;
	HWND hTempWnd,hEdit;
	unsigned long dwProcessID1,dwProcessID2;
	int iLeng=0;
	char strKey[100]="";

	switch(msg)
	{
	case WM_CREATE:
		hInstance=((LPCREATESTRUCT)lParam)->hInstance;
		// 创建两个编辑框显示跟踪的窗口值:进程ID号
		CreateWindow("Edit","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,100,60,240,20,hWnd,(HMENU)(IDE_KEY+100),hInstance,0);
		CreateWindow("Edit","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,100,90,240,20,hWnd,(HMENU)(IDE_KEY+200),hInstance,0);
		break;

	case WM_LBUTTONDOWN:
//	case WM_NCLBUTTONDOWN:
		SetCapture(hWnd); // 开始捕获(独占)鼠标信息
		break;

	case WM_LBUTTONUP:
	case WM_NCLBUTTONUP:
		ReleaseCapture(); // 结束捕获状态
		break;

	case WM_MOUSEMOVE:
	case WM_NCMOUSEMOVE:

		pt.x=LOWORD(lParam); // 相对于当前客户区的鼠标坐标
		pt.y=HIWORD(lParam);
		ClientToScreen(hWnd,&pt); // 客户坐标转换为屏幕坐标

		hTempWnd=WindowFromPoint(pt); // 获取鼠标下的窗口句柄

//		if(GetWindowThreadProcessId(hTempWnd,NULL)==GetWindowThreadProcessId(hWnd,NULL)) // 如是程序本身就不获取了
//			break;

		hEdit=GetDlgItem(hWnd,IDE_KEY+100); 
		GetWindowThreadProcessId(hWnd,&dwProcessID1);// 程序的主窗口句柄
		ultoa(dwProcessID1, strKey,10);
		if(hWnd==NULL)
			SetWindowText(hEdit,"Sorry!, No Find Window."); // 显示到本身程序里的文本框1里
		else
			SetWindowText(hEdit,strKey);

		hEdit=GetDlgItem(hWnd,IDE_KEY+200); 
		GetWindowThreadProcessId(hTempWnd,&dwProcessID2);// 鼠标下的窗口句柄
		ultoa(dwProcessID2, strKey,10);
		if(hWnd==NULL)
			SetWindowText(hEdit,"Sorry!, No Find Window."); // 显示到本身程序里的文本框2里
		else
			SetWindowText(hEdit,strKey);

		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc ( hWnd, msg, wParam, lParam );
}

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//