请教:窗口句柄是怎么来的?

Ah 2004-04-07 08:46:19
突然想起这个问题
下面这个是VC6生成的代码,是一个窗口,然后没有看到任何关于窗口句柄的操作,
突然想起来,窗口句柄的是谁分配的?怎么和一个窗口连系起来的?系统怎么知道哪条消息是发往哪个窗口的?
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)

{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LKHSERVICE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_LKHSERVICE);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
...全文
112 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsaturn 2004-04-07
  • 打赏
  • 举报
回复
MSG
The MSG structure contains message information from a thread's message queue.

typedef struct tagMSG {
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG, *PMSG;
kathywp 2004-04-07
  • 打赏
  • 举报
回复
用线程消息
Ah 2004-04-07
  • 打赏
  • 举报
回复
To gufengduyu(聪明的呆呆龙):
都说了这个消息循环是在非窗口的程序中,没有hwnd
wenxi2004 2004-04-07
  • 打赏
  • 举报
回复
关注
gufengduyu 2004-04-07
  • 打赏
  • 举报
回复
SendMessage(hWnd,WM_XXX,0,0);
hWnd指定了向那个窗口发送,DispatchMessage(&msg)会把消息发送到它所属的窗口
Ah 2004-04-07
  • 打赏
  • 举报
回复
如果一个消息循环,就象下面这样的
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

不是在一个窗口中的,怎么向它发送消息?
byf2002 2004-04-07
  • 打赏
  • 举报
回复
实例指针是操作系统给分配的.

窗口句柄是程序中分配的.

一般完整的代码是这样的:
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if (!hPrevInstance)
if (!InitApplication(hInstance))
return (FALSE)

if (!InitInstance(hInstance, nCmdShow))
return (FALSE);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage (&msg);
}
return (msg.wParam);
}

BOOL InitApplication(HINSTANCE hInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, "jjhouricon");
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = "GenericMenu";
wc.lpszClassName = "MyWindow";
return (RegisterClass(&wc));
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hWnd = CreateWindow("MyWindow","This is Windows SDK",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if (hWnd == NULL)
return (FALSE);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return (TRUE);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowPro(hWnd, message, wParam, lParam);
}
return 0;
}

看到没有,在InitInstance函数中分配了窗口句柄指针hWnd.
Ah 2004-04-07
  • 打赏
  • 举报
回复
比如有个消息循
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
如果不是在窗口中的,怎么向它发送消息?
ndy_w 2004-04-07
  • 打赏
  • 举报
回复
if (!InitInstance (hInstance, nCmdShow))
这里。
kathywp 2004-04-07
  • 打赏
  • 举报
回复
窗口是系统调用api以后填充的一个窗口结构指针。简单讲就是创建了窗口以后给了一个在系统中的唯一标识。
消息和窗口的联系是靠发送消息方把消息发送的目的地,也就是窗口句柄指定之后来联系的
mfc把这个封装了,用api的时候比如SendMessage(hwnd,。。。)
hwnd就是窗口句柄。
NOMADBLUE 2004-04-07
  • 打赏
  • 举报
回复
窗口句柄是程序创建的,通常以H开头,如HWND。
在你的程序里,是在InitInstance (hInstance, nCmdShow) 中和窗口连系起来的:
InitInstance 中又有其他函数,把一个句柄和资源联系在一起,并向Windows注册。
当Windows系统向你的程序发消息时,通过消息中附带的目的窗口信息将消息发往正确的
窗口













快乐鹦鹉 2004-04-07
  • 打赏
  • 举报
回复
窗口句柄实际上就是一个指针。指向窗口实例所在的内存空间。
ndy_w 2004-04-07
  • 打赏
  • 举报
回复
The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure. This function can retrieve both messages associated with a specified window and thread messages posted via the PostThreadMessage function. The function retrieves messages that lie within a specified range of message values. GetMessage does not retrieve messages for windows that belong to other threads or applications.

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧