tags:

views:

18

answers:

3

CreatePen GDI function does not work on Windows 7 when pen width is 3 or 4 (LineTo draws nothing). It works for 0 - 2 width. In all cases PS_SOLID pen style was used.

A: 

LineTo does not include the final point of the line. Try a different endcap setting to see if it makes a difference.

Mark Ransom
Thank you Mark for good idea. The problem resolved by replacing LineTo function call with PolyLine call. I used plain CreatePen API which does not take as parameter end cap setting like ExtCreatePen does. Intertstingly that this code worked fine on Vista and XP. Only Windows 7 with width 3 and above has problem. –
A: 

does it return a handle or NULL?
have you considered using CreatePenIndirect and the structure LOGPEN?
http://msdn.microsoft.com/en-us/library/dd183510(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/dd145041(v=VS.85).aspx
this just works fine on my computer, Windows 7 x64:

Option Explicit

Private Declare Function CreatePen Lib "gdi32" ( _
   ByVal nPenStyle As Long, _
   ByVal nWidth As Long, _
   ByVal crColor As Long) As Long

Private Declare Function LineTo Lib "gdi32" ( _
   ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long

Private Declare Function SelectObject Lib "gdi32" ( _
   ByVal hDC As Long, ByVal hObject As Long) As Long

Private Declare Function DeleteObject Lib "gdi32" ( _
   ByVal hObject As Long) As Long

Dim x As Long
Dim y As Long
Dim w As Long

Private Sub Command1_Click()
   w = w + 1
   Dim hpen As Long: hpen = CreatePen(0, w, 0)
   Dim ret As Long: ret = SelectObject(Me.hDC, hpen)
   If ret <> 0 Then
       x = x + 10
       y = y + 10
       Call LineTo(Me.hDC, x, y)
   End If
   Call DeleteObject(hpen)
End Sub

click the button more times

Oops
Thank you Mark for good idea. The problem resolved by replacing LineTo function call with PolyLine call. I used plain CreatePen API which does not take as parameter end cap setting like ExtCreatePen does. Intertstingly that this code worked fine on Vista and XP. Only Windows 7 with width 3 and above has problem.
A: 

Thank you Mark for good idea. The problem resolved by replacing LineTo function call with PolyLine call. I used plain CreatePen API which does not take as parameter end cap setting like ExtCreatePen does. Intertstingly that this code worked fine on Vista and XP. Only Windows 7 with width 3 and above has problem. –