读书人

初学API,该怎么解决

发布时间: 2013-01-11 11:57:35 作者: rapoo

初学API
程序:

#include "stdafx.h"
#include "First.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance); //函数参数未使用不报错
UNREFERENCED_PARAMETER(lpCmdLine); //函数参数未使用不报错
UNREFERENCED_PARAMETER(hInstance); //函数参数未使用不报错
UNREFERENCED_PARAMETER(nCmdShow);//函数参数未使用不报错

//获取桌面的句柄
HWND hWnd = GetDesktopWindow();
//显示一行消息
::MessageBox(hWnd, _T("第一个应用程序"), _T("例子"), MB_OK);
return 0;
}

/*
那么我想请问下,程序怎么才算获得句柄,而且,::MessageBox(hWnd, _T("第一个应用程序"), _T("例子"), MB_OK);
第一个参数是hWnd也就是窗口而非窗口,那么只有执行完毕这个窗口才可以在桌面上移动吧。
那么程序会怎么执行找到句柄?
最后就是::MessageBox(hWnd, _T("第一个应用程序"), _T("例子"), MB_OK);为什么会有::我并没有定义对象
MessageBox(hWnd, _T("第一个应用程序"), _T("例子"), MB_OK);我一般都是这样写的
*/

来个人帮下
[解决办法]
楼主提问提的不是很清楚呀,我给你解释下代码吧。

//获取桌面的句柄
HWND hWnd = GetDesktopWindow();

这个系统会保存每个句柄,因此使用相应的API函数很容易查到句柄的内容呀。


//显示一行消息
::MessageBox(hWnd, _T("第一个应用程序"), _T("例子"), MB_OK);

这句代码会弹出对话框,要查找这个对话框的句柄,可以用FindWindow函数。
HWND hwndDlg = FindWindow(NULL, "例子");
详细见MSDN上的解释:
The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.

To search child windows, beginning with a specified child window, use the FindWindowEx function.


Syntax

HWND FindWindow(
LPCTSTR lpClassName,
LPCTSTR lpWindowName
);

读书人网 >C语言

热点推荐