如何初始化句柄 [英] How to Initialise a Handle
问题描述
错误:
Run-Time Check Failure #3 - The variable 'TextLabelVar' is being used without
being initialized.
说明代码:
HWND VarText;
char Disps[100];
float some_number;
WINAPI WndProc(..)
{
switch(...)
case WM_CREATE:
TextLabelVar=CreateWindowEx(WS_EX_WINDOWEDGE,TEXT("Edit"), TEXT("Val."), WS_CHILD \
| WS_VISIBLE, 380, 50, 140, 20, hwnd, NULL, NULL, NULL);
break;
case WM_MESSAGEFROMANOTHERWINDOW:
some_number=1298.123123;
sprintf(Disps,"%f",some_number);
SetWindowText(TextLabelVar,TEXT(Disps));
}
问题:
好的,我得到错误TextLabelVar正在使用,无需初始化。我得到类似的错误var Disps。然后我声明char Disps [100];这显示没有错误。现在,错误移动到句柄。我不知道我可以声明一个句柄作为数组,这是有道理的。我如何解决这个问题?
Question: Ok, I get the error TextLabelVar is being used without initialisation. I got similar error for var Disps. I then declared char Disps[100]; This showed no error. Now, the error moves onto a handle. I am not sure I can declare a handle as array and it makes sense. How do I solve this?
推荐答案
这是一个运行时错误。它告诉你在它被初始化之前,正在读取 TextLabelVar
。这意味着 WM_MESSAGEFROMANOTHERWINDOW
在 WM_CREATE
之前执行的case语句。
This is a run-time error. What it is telling you is that TextLabelVar
is being read before it has been initialised. And that means that the WM_MESSAGEFROMANOTHERWINDOW
case statement executed before WM_CREATE
.
从您分配 0
到 TextLabelVar
的注释中删除警告,到 SetWindowText
无法成功,因为您传递的窗口句柄是 0
。
From the comments you've assigned 0
to TextLabelVar
to suppress the warning, but then of course the call to SetWindowText
cannot succeed since the window handle you pass is 0
.
要解决这个问题,您需要了解为什么 WM_MESSAGEFROMANOTHERWINDOW
正在 WM_CREATE
之前处理。
To tackle the problem you need to understand why WM_MESSAGEFROMANOTHERWINDOW
is being handled before WM_CREATE
.
这篇关于如何初始化句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!