Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SetPixel not working on Vista

3 views
Skip to first unread message

Paul

unread,
Oct 20, 2009, 12:09:44 PM10/20/09
to
Hi,

SetPixel on Vista is acting strange. Certain areas it cannot draw on.
If I draw the entire window, then we can see the areas it can draw to,
which is vertical areas/bars that are a few hundred pixels wide. The
code works fine on my XP PC. So I thought there might be some window
app behind it preventing it from drawing in certain areas, but found
nothing. Changing the desktop to a solid color did nothing. Any ideas
what could be causing this? Here's the code,

void CTest1View::OnPaint()
{
CPaintDC dc(this); // device context for painting
if(IsIconic()){
return;
}
long x,y;
for(y=0; y < 1024; y++) {
for(x=0; x < 1024; x++) {
dc.SetPixel(x,y,0xff);
}
}
}


and another example,

void CTest2View::OnPaint()
{
CPaintDC dc(this); // device context for painting
if(IsIconic()){
return;
}
long x,y;
PAINTSTRUCT ps;
BeginPaint(&ps);
for(y=0; y < 1024; y++) {
for(x=0; x < 1024; x++) {
dc.SetPixel(x,y,0xff);
}
}
EndPaint(&ps);
}

Also I tried dc,
int save = dc.SaveDC(); // place this near start of code
...
dc.RestoreDC(save); // place this near end of code

Also I tried calling OnDraw the common way within OnPaint-- same
results.


I've tried everything I know. Change the window size. Regardless, the
above code always draws vertical bars that are black, white, black,
white, about ~ 200 pixels per bar.

Thanks for any suggestions.
Paul

Paul

unread,
Oct 20, 2009, 12:18:43 PM10/20/09
to

Ha ha, would you believe it, after search for hours how to solve this
issue, just moments after my above post I found some info on this
Vista issue,

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=452578

Well, looks like it's a Windows Vista issue. Okay, I'll stay away from
SetPixel, as it was used only to see if a function was working, but
being a bit stuborn I wanted to dig down and get the SetPixel to work.

Regards,
Paul

Joseph M. Newcomer

unread,
Oct 20, 2009, 1:46:27 PM10/20/09
to
See below...

On Tue, 20 Oct 2009 09:09:44 -0700 (PDT), Paul <energ...@gmail.com> wrote:

>Hi,
>
>SetPixel on Vista is acting strange. Certain areas it cannot draw on.
>If I draw the entire window, then we can see the areas it can draw to,
>which is vertical areas/bars that are a few hundred pixels wide. The
>code works fine on my XP PC. So I thought there might be some window
>app behind it preventing it from drawing in certain areas, but found
>nothing. Changing the desktop to a solid color did nothing. Any ideas
>what could be causing this? Here's the code,
>
>void CTest1View::OnPaint()
>{
> CPaintDC dc(this); // device context for painting
> if(IsIconic()){
> return;
> }
> long x,y;
> for(y=0; y < 1024; y++) {
> for(x=0; x < 1024; x++) {
> dc.SetPixel(x,y,0xff);
> }
> }
>}

***
I gather this is a test case to show the problem. This would be less than ideal in
practice. (besides, 0xff is not a color value; you should use the RGB macro). But in any
case, I just ran this on Vista and got a solid rectangle.
****


>
>
>and another example,
>
>void CTest2View::OnPaint()
>{
> CPaintDC dc(this); // device context for painting
> if(IsIconic()){
> return;
> }
> long x,y;
> PAINTSTRUCT ps;
> BeginPaint(&ps);

****
This code is erroneous. You already have a PAINTSTRUCT which has been set, and the second
BeginPaint is nonsensical. What do you think CPaintDC is doing?
****


> for(y=0; y < 1024; y++) {
> for(x=0; x < 1024; x++) {
> dc.SetPixel(x,y,0xff);
> }
> }
> EndPaint(&ps);

****
Of course, since the BeginPaint is wrong, the EndPaint is also wrong.
****


>}
>
>Also I tried dc,
>int save = dc.SaveDC(); // place this near start of code
>...
>dc.RestoreDC(save); // place this near end of code

***
Irrelevant.
****


>
>Also I tried calling OnDraw the common way within OnPaint-- same
>results.

****
Actually, you should not be doing any drawing in the OnPaint of a view; you should be
doing it exclusively in the OnDraw.
****


>
>
>I've tried everything I know. Change the window size. Regardless, the
>above code always draws vertical bars that are black, white, black,
>white, about ~ 200 pixels per bar.

****
There's something missing in your description, because I was able to use the code you
wrote and I get a solid rectangle (I used RGB(255, 0, 0) and got a solid red rectangle)
joe
****


>
>Thanks for any suggestions.
>Paul

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

David Ching

unread,
Oct 20, 2009, 4:39:25 PM10/20/09
to
"Paul" <energ...@gmail.com> wrote in message
news:02e2d860-1112-4155...@z4g2000prh.googlegroups.com...

> Ha ha, would you believe it, after search for hours how to solve this
> issue, just moments after my above post I found some info on this
> Vista issue,
>
> https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=452578
>

Don't you just love it when they say "Closed as Fixed" without telling you
what version the fix is in?

-- David

Paul

unread,
Oct 21, 2009, 9:15:02 AM10/21/09
to
> email: newco...@flounder.com


As the other guy stated, it occurs in certain Vista packages, and only
when in certain theme modes.

Paul

Paul

unread,
Oct 21, 2009, 9:19:39 AM10/21/09
to
On Oct 20, 1:39 pm, "David Ching" <d...@remove-this.dcsoft.com> wrote:
> "Paul" <energymo...@gmail.com> wrote in message

>
> news:02e2d860-1112-4155...@z4g2000prh.googlegroups.com...
>
> > Ha ha, would you believe it, after search for hours how to solve this
> > issue, just moments after my above post I found some info on this
> > Vista issue,
>
> >https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx...

>
> Don't you just love it when they say "Closed as Fixed" without telling you
> what version the fix is in?
>
> -- David


Good point. Maybe it's a future version.

Paul

João Neto

unread,
Nov 21, 2009, 6:56:53 PM11/21/09
to

Try this... works fine for me !
/* SetPixel problem with Vista:
(found here : http://www.codeproject.com/Messages/2515738/Re-Strange-bug-in-SetPixel-in-Visual-Cplusplus-pro.aspx)
I have hacked a temporary solution, thus, but I would prefer a proper
permanent solution:
*/
void Set_Pixel(HDC dc,int x,int y,COLORREF c);
#define SetPixel(dc,x,y,c) Set_Pixel(dc,x,y,c)

/*-----*/
void Set_Pixel(HDC dc,int x,int y,COLORREF c)
{
HPEN p=CreatePen(PS_SOLID,1,c),q;
q=(HPEN)SelectObject(dc,p);
MoveToEx(dc,x,y,0);
LineTo(dc,x,y+1);
SelectObject(dc,q);
DeleteObject(p);
}

Paul

unread,
Nov 25, 2009, 1:32:11 PM11/25/09
to
> (found here :http://www.codeproject.com/Messages/2515738/Re-Strange-bug-in-SetPixe...)

> I have hacked a temporary solution, thus, but I would prefer a proper
> permanent solution:
> */
> void Set_Pixel(HDC dc,int x,int y,COLORREF c);
> #define SetPixel(dc,x,y,c) Set_Pixel(dc,x,y,c)
>
> /*-----*/
> void Set_Pixel(HDC dc,int x,int y,COLORREF c)
> {
>         HPEN p=CreatePen(PS_SOLID,1,c),q;
>         q=(HPEN)SelectObject(dc,p);
>         MoveToEx(dc,x,y,0);
>         LineTo(dc,x,y+1);
>         SelectObject(dc,q);
>         DeleteObject(p);
>
> }


Nice! Thanks, I see no reason why that would not work. :-)

Regards,
Paul

0 new messages