为什么下面的代码不起作用?
我已经使用以下样式创建了一个静态控件...
picBoxDisp = CreateWindow("STATIC", "image box",
WS_VISIBLE |WS_CHILD | SS_BITMAP |WS_TABSTOP | WS_BORDER,
50, 50, 250, 300,
hwnd , (HMENU)10000, NULL, NULL);
SetWindowLongPtr(picBoxDisp,GWLP_WNDPROC,(LONG) dispWndProc);
从我的程序中的某个地方我有以下代码..
SendMessage(picBoxDisp,STM_SETIMAGE, (WPARAM) IMAGE_BITMAP,(LPARAM) hBitmap);
现在在 dispWndProc 中我有以下代码..
LRESULT CALLBACK dispWndProc(HWND hwnd,UINT msg, WPARAM wParam, LPARAM lParam)
{
static HDC hdc;
static PAINTSTRUCT paintSt;
static RECT aRect;
switch(msg)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&paintSt);
GetClientRect(hwnd,&aRect);
// the code for painting
EndPaint(hwnd,&paintSt);
}
break;
case STM_SETIMAGE:
{
//painting code;
HBITMAP img = (HBITMAP)lParam;
BITMAP bmp;
GetObject(img,sizeof(bmp),&bmp);
HDC imgDC = GetDC((HWND)img);
HDC memDC = CreateCompatibleDC(imgDC);
SelectObject(memDC,img);
if((img==NULL))// ||(imgDC==NULL)||(memDC==NULL))
{
MessageBox(NULL,"img is NULL","Bad Programming!!! Error",MB_OK);
}
else
{
StretchBlt(hdc,0,0,aRect.right,aRect.bottom,
memDC,0,0,bmp.bmWidth,bmp.bmHeight,
SRCCOPY);
}
}
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
谁能告诉我为什么 lParam 不类型转换回 HBITMAP。 ...为什么 img 是 NULL ?
提前感谢,
I have Created a static control using following styles...
picBoxDisp = CreateWindow("STATIC", "image box",
WS_VISIBLE |WS_CHILD | SS_BITMAP |WS_TABSTOP | WS_BORDER,
50, 50, 250, 300,
hwnd , (HMENU)10000, NULL, NULL);
SetWindowLongPtr(picBoxDisp,GWLP_WNDPROC,(LONG) dispWndProc);
from someplace in my program I have the following code..
SendMessage(picBoxDisp,STM_SETIMAGE, (WPARAM) IMAGE_BITMAP,(LPARAM) hBitmap);
now inside the dispWndProc I have the following code..
LRESULT CALLBACK dispWndProc(HWND hwnd,UINT msg, WPARAM wParam, LPARAM lParam)
{
static HDC hdc;
static PAINTSTRUCT paintSt;
static RECT aRect;
switch(msg)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&paintSt);
GetClientRect(hwnd,&aRect);
// the code for painting
EndPaint(hwnd,&paintSt);
}
break;
case STM_SETIMAGE:
{
//painting code;
HBITMAP img = (HBITMAP)lParam;
BITMAP bmp;
GetObject(img,sizeof(bmp),&bmp);
HDC imgDC = GetDC((HWND)img);
HDC memDC = CreateCompatibleDC(imgDC);
SelectObject(memDC,img);
if((img==NULL))// ||(imgDC==NULL)||(memDC==NULL))
{
MessageBox(NULL,"img is NULL","Bad Programming!!! Error",MB_OK);
}
else
{
StretchBlt(hdc,0,0,aRect.right,aRect.bottom,
memDC,0,0,bmp.bmWidth,bmp.bmHeight,
SRCCOPY);
}
}
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
can anyone tell why the lParam doesnt typecast back to HBITMAP.... why img is NULL ?
thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
某些其他代码也可能将
STM_SETIMAGE
发送到您的窗口。计算调用SendMessage(STM_SETIMAGE)
的次数以及达到case STM_SETIMAGE
的次数。另外,HDC imgDC = GetDC((HWND)img); 永远不会起作用。
HBITMAP
不是HWND
。It's possible that some other code is also sending
STM_SETIMAGE
to your window. Count the number of times you callSendMessage(STM_SETIMAGE)
and the number of times you reachcase STM_SETIMAGE
.Also,
HDC imgDC = GetDC((HWND)img);
is never going to work. AnHBITMAP
is not anHWND
.这段代码存在多个问题。
WM_PAINT
之外,您不能在任何地方使用BeginPaint
/EndPaint
。在考虑其他问题之前先解决这个问题。NULL
。从这些事情开始,你就会更接近正轨。
There are multiple issues with this code.
BeginPaint
/EndPaint
anywhere except for handlingWM_PAINT
. Fix that before even considering other problems.CallWindowProc
on the old window proc.NULL
to begin with.Start with these things, and you will get closer to being on track.