像素数量过后,SetPixel崩溃 [英] SetPixel crashes after an amount of pixels
问题描述
我是MFC/C ++的新手,我正在尝试用像素填充窗口.我发现有一个叫做:
Im new in MFC/C++ an Im trying to fill my windows with pixels.I found out that there is a function which is called :
SetPixel(X,Y,RGB(,,));
SetPixel(X,Y,RGB(,,));
尝试在循环中使用它后,我发现此功能在经过一定像素后会停止,因此它并没有给我我真正想要达到的结果. 这是我的代码:
After I tried to put use it in my loop I found out that this function stops after an amount of pixels.So It don't give me the result I actually want to reach. Here is my code :
void PIXELPROG::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
CStatic * XText = (CStatic *)GetDlgItem(IDC_X);
CStatic * YText = (CStatic *)GetDlgItem(IDC_YWERT);
CString XYWert;
for (int x=0,y=0;;)
{
GetDC()->SetPixelV(x, y, RGB(y,x,y));
XYWert.Format(L"%d",y);
XText->SetWindowTextW(XYWert);
++x;
if (x == 500)
{
++y;
x = 0;
}
if (y == 100)
{
break;
}
}
}
我也没有收到任何错误,它只是停止了. 我也用
I also don't get any errors.It is just stopping. I also tried it with
SetPixelV()
SetPixelV()
但两者都没有帮助. 有人知道吗?
But didn't helped neither. Someone got an idea ?
推荐答案
来自
除非设备上下文属于窗口类,否则必须调用ReleaseDC成员函数以在绘制后释放上下文.
Unless the device context belongs to a window class, the ReleaseDC member function must be called to release the context after painting. 由于您没有将 Since you're not assigning the return value from 如果这是对WM_PAINT消息的响应,则不应首先调用 If this is in response to a WM_PAINT message, you shouldn't be calling 这篇关于像素数量过后,SetPixel崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
GetDC
的返回值分配给任何东西,因此您无法调用ReleaseDC
.由于尚未发布,因此它们会累积-您的应用程序可以使用的GDI对象总数受到限制,请参见GetDC
to anything, there's no way for you to call ReleaseDC
. Since they're not released, they build up - there's a limit to the total number of GDI objects your application can use, see GDI Objects. Once you hit that limit, things go bad very fast (Don't ask me how I know).GetDC
.您应该使用创建的CPaintDC
对象.通常,不要在自己的OnPaint
处理程序中调用父OnPaint
方法,因为您只能生成一个CPaintDC
.GetDC
in the first place. You should be using the CPaintDC
object that you create. As a general rule, don't call the parent OnPaint
method in your own OnPaint
handler, because you can only generate a single CPaintDC
.