473,847 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using The NativeWindow Class To Draw A GDI Type Circle On Top Of A DataGrid Possibly In The Override OnPaint

I have a requirement to put a GDI style circle or rectangle border around
the selected row of a datagrid/ It will overlap into the row above and below
the selected row. Doing this in a the OnPaint of a subclassed
DataGridTextBox Colum dos not seem like a practical way to do it.

I have subclassed a DataGrid and overridden the OnPaint as such:

protected override void OnPaint(PaintEv entArgs pe)

{

base.OnPaint(pe );

// Create pen.

Pen blackPen = new Pen(Color.Green , 2);

// Create location and size of rectangle.

float x = 15.0F;

float y = 53.0F;

float width = 500.0F;

float height = 20.0F;

// Draw rectangle to screen.

pe.Graphics.Dra wRectangle(blac kPen, x, y, width, height);

}

It works but has refresh problems.

Last week Bob Powell [MVP] suggested that I use a NativeWindow class to
solve this problem. I've just had the time to get back to this and not being
a former C++ person, etc., etc., etc. maybe someone can help with this.

The MSDN help screen for the NativeWindow class is this link:

http://msdn.microsoft.com/library/de...classtopic.asp

It has an example and in the subclassed NativeWindow constructor it creates
a button:

public MyNativeWindow( Form parent)

{

...see help example

}

Can someone advise me on where my code above that paints the Rectangle
should go in this NativeWindow example?


Nov 17 '05 #1
3 4270
Hi,

You should create a datagrid column style to do that. Here is an
example of an image column that shows how to tell when the column is
selected. You will have to draw your circle in the paint event. Keep in
mind that you have to draw part of the circle in each column shown.

Imports System.Drawing

Imports System.Drawing. Drawing2D

Imports System.Windows. Forms

Imports System.Reflecti on

Imports System.IO

Public Class DataGridImageCo lumn

Inherits DataGridTextBox Column

Dim WithEvents dg As DataGrid

Private arHeights As ArrayList

Private mintOffset As Integer

Private cm As CurrencyManager

Public Sub New()

Me.NullText = "Image not available."

End Sub

Private Sub GetHeightList()

Dim mi As MethodInfo = dg.GetType().Ge tMethod("get_Da taGridRows",
BindingFlags.Fl attenHierarchy Or BindingFlags.Ig noreCase Or
BindingFlags.In stance Or BindingFlags.No nPublic Or BindingFlags.Pu blic Or
BindingFlags.St atic)

Dim dgra As Array = CType(mi.Invoke (Me.dg, Nothing), Array)

arHeights = New ArrayList

Dim dgRowHeight As Object

For Each dgRowHeight In dgra

If dgRowHeight.ToS tring().EndsWit h("DataGridRela tionshipRow") = True Then

arHeights.Add(d gRowHeight)

End If

Next

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing. Graphics,
ByVal bounds As System.Drawing. Rectangle, ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing. Brush, ByVal foreBrush As System.Drawing. Brush,
ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

Static bMove As Boolean = True

If Not bPainted Then

dg = Me.DataGridTabl eStyle.DataGrid

cm = source

GetHeightList()

End If

If source.Count - 1 = rowNum And bMove Then

'

' Force resize so scroll bars show

'

dg.Size = New Size(dg.Size.Wi dth, dg.Height - 1)

bMove = False

dg.Size = New Size(dg.Size.Wi dth, dg.Height + 1)

End If

'clear the cell

g.FillRectangle (backBrush, bounds)

Dim objData As Object = Me.GetColumnVal ueAtRow([source], rowNum)

If TypeOf objData Is System.DBNull Then

Dim sf As New StringFormat

With sf

..Alignment = StringAlignment .Center

..LineAlignment = StringAlignment .Center

End With

g.DrawString(Me .NullText, Me.TextBox.Font , Brushes.Black, _

RectangleF.op_I mplicit(bounds) , sf)

Else

Try

Dim bm As Bitmap

If TypeOf objData Is String Then

Dim fs As New FileStream(objD ata.ToString, FileMode.Open)

bm = CType(Bitmap.Fr omStream(fs), Bitmap)

fs.Close()

Else

Try

bm = ConvertToBitmap (DirectCast(obj Data, Byte()), 78)

Catch

bm = ConvertToBitmap (DirectCast(obj Data, Byte()), 0)

End Try

End If

Dim h As Integer

Dim s As Double

s = (Me.Width / bm.Width)

h = CInt(bm.Height * s)

If s >= 1 Then

h = bm.Height

End If

AdjustHeight(h)

Dim r As Rectangle

If Me.Width > bm.Width Then

r = New Rectangle(bound s.Left, bounds.Top, bm.Width, bm.Height)

Else

r = New Rectangle(bound s.Left, bounds.Top, Me.Width, h)

End If

g.DrawImage(bm, r)

If dg.IsSelected(r owNum) Then

Dim br As New SolidBrush(Colo r.FromArgb(64, _

Me.DataGridTabl eStyle.Selectio nBackColor))

g.FillRectangle (br, bounds)

br.Dispose()

End If

Catch ex As Exception

Dim sf As New StringFormat

With sf

..Alignment = StringAlignment .Center

..LineAlignment = StringAlignment .Center

End With

g.DrawString("I nvalid image format", Me.TextBox.Font , Brushes.Black, _

RectangleF.op_I mplicit(bounds) , sf)

End Try

End If

bPainted = True

End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)

End Sub

Private Sub AdjustHeight(By Val h As Integer)

Dim curHeight As Integer = Me.DataGridTabl eStyle.Preferre dRowHeight

If h > curHeight Then

'

' Manually set all the row heights to the new height

'

Me.DataGridTabl eStyle.Preferre dRowHeight = h

Trace.WriteLine (h)

If arHeights.Count < cm.Count Then

GetHeightList()

End If

For rownum As Integer = 0 To cm.Count - 1

Try

Dim pi As PropertyInfo

pi = arHeights(rownu m).GetType().Ge tProperty("Heig ht")

' adjust height

If h > curHeight Then

pi.SetValue(arH eights(rownum), h, Nothing)

End If

Catch

' something wrong leave default height

Debug.WriteLine ("Error")

End Try

Next

'

' Resize datagrid to get scrollbars to work right

'

Dim sz As Size = dg.Size

dg.Size = New Size(New Point(sz.Width - 1, sz.Height - 1))

dg.Size = sz

End If

End Sub

Private Function ConvertToBitmap (ByVal data() As Byte, ByVal offset As
Integer) As Bitmap

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

ms = New MemoryStream

ms.Write(data, offset, data.Length - offset)

bm = New Bitmap(ms)

Return bm

End Function

<DataSysDescrip tion("Suspends painting of column until EndUpdate is
called")> _

Public Shadows Sub BeginUpdate()

MyBase.BeginUpd ate()

End Sub

<DataSysDescrip tion("Resumes painting of column")> _

Public Shadows Sub EndUpdate()

MyBase.EndUpdat e()

End Sub

End Class

Ken

---------------------

"Richard" <a@b.com> wrote in message
news:vg******** ************@tw ister.nyc.rr.co m...
I have a requirement to put a GDI style circle or rectangle border around
the selected row of a datagrid/ It will overlap into the row above and below
the selected row. Doing this in a the OnPaint of a subclassed
DataGridTextBox Colum dos not seem like a practical way to do it.

I have subclassed a DataGrid and overridden the OnPaint as such:

protected override void OnPaint(PaintEv entArgs pe)

{

base.OnPaint(pe );

// Create pen.

Pen blackPen = new Pen(Color.Green , 2);

// Create location and size of rectangle.

float x = 15.0F;

float y = 53.0F;

float width = 500.0F;

float height = 20.0F;

// Draw rectangle to screen.

pe.Graphics.Dra wRectangle(blac kPen, x, y, width, height);

}

It works but has refresh problems.

Last week Bob Powell [MVP] suggested that I use a NativeWindow class to
solve this problem. I've just had the time to get back to this and not being
a former C++ person, etc., etc., etc. maybe someone can help with this.

The MSDN help screen for the NativeWindow class is this link:

http://msdn.microsoft.com/library/de...classtopic.asp

It has an example and in the subclassed NativeWindow constructor it creates
a button:

public MyNativeWindow( Form parent)

{

...see help example

}

Can someone advise me on where my code above that paints the Rectangle
should go in this NativeWindow example?



Nov 17 '05 #2
> Last week Bob Powell [MVP] suggested that I use a NativeWindow class to
solve this problem. I've just had the time to get back to this and not
being
a former C++ person, etc., etc., etc. maybe someone can help with this.
Far be it from me to leave a hint like that hanging in the air ;-)

See after my signature. (lots of lovely interop)

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

Imports System

Imports System.Drawing

Imports System.Drawing. Drawing2D

Imports System.Collecti ons

Imports System.Componen tModel

Imports System.Windows. Forms

Imports System.Data

Imports System.Runtime. InteropServices

'

' * The example below explains how to use a NativeWindow With the
WS_EX_LAYERED style set to create a window that sits on top of everything.

' * This uses only one window which is mostly transparent. The transparency
is obtained by using some garish color like magenta on the background and
using the SetLayeredWindo wAttributes API to create the transparency key.

' *

' * The window draws on top of everything including the desktop but is a
true window so better managed than raw desktop drawing.

' *

'

Namespace LayeredWindowTe st

'/ <summary>

'/ Summary description for Form1.

'/ </summary>
Public Class Form1

Inherits System.Windows. Forms.Form

Private WithEvents timer1 As System.Windows. Forms.Timer

Private button1 As System.Windows. Forms.Button

Private textBox1 As System.Windows. Forms.TextBox

Private listBox1 As System.Windows. Forms.ListBox

Private components As System.Componen tModel.IContain er

Public Sub New()

'

' Required for Windows Form Designer suppoTest

'

InitializeCompo nent()

End Sub 'New
'

' TODO: Add any constructor code after InitializeCompo nent call

'
'/ <summary>

'/ Clean up any resources being used.

'/ </summary>

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Disp ose()

End If

End If

MyBase.Dispose( disposing)

End Sub 'Dispose

#Region "Windows Form Designer generated code"

'/ <summary>

'/ Required method for Designer suppoTest - do not modify

'/ the contents of this method with the code editor.

'/ </summary>

Private Sub InitializeCompo nent()

Me.components = New System.Componen tModel.Containe r

Me.timer1 = New System.Windows. Forms.Timer(Me. components)

Me.button1 = New System.Windows. Forms.Button

Me.textBox1 = New System.Windows. Forms.TextBox

Me.listBox1 = New System.Windows. Forms.ListBox

Me.SuspendLayou t()

'

' timer1

'

Me.timer1.Enabl ed = True

Me.timer1.Inter val = 50

'

' button1

'

Me.button1.Loca tion = New System.Drawing. Point(56, 48)

Me.button1.Name = "button1"

Me.button1.TabI ndex = 0

Me.button1.Text = "button1"

'

' textBox1

'

Me.textBox1.Loc ation = New System.Drawing. Point(168, 144)

Me.textBox1.Nam e = "textBox1"

Me.textBox1.Tab Index = 1

Me.textBox1.Tex t = "textBox1"

'

' listBox1

'

Me.listBox1.Loc ation = New System.Drawing. Point(24, 88)

Me.listBox1.Nam e = "listBox1"

Me.listBox1.Siz e = New System.Drawing. Size(120, 95)

Me.listBox1.Tab Index = 2

'

' Form1

'

Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)

Me.ClientSize = New System.Drawing. Size(292, 273)

Me.Controls.Add (listBox1)

Me.Controls.Add (textBox1)

Me.Controls.Add (button1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout (False)

End Sub 'InitializeComp onent

#End Region

'/ <summary>

'/ The main entry point for the application.

'/ </summary>

<STAThread()> _

Shared Sub Main()

Application.Run (New Form1)

End Sub 'Main

Private x As Integer = 30

Private y As Integer = 30

Private dir As Integer = 1

Private Sub timer1_Tick(ByV al sender As Object, ByVal e As System.EventArg s)
Handles timer1.Tick

x += dir

y += dir

If x > Me.Width - 30 OrElse x < 30 Then

dir = -dir

End If

lwt.Location = New Point(10, 10)

lwt.Size = New Size(x, y)

End Sub 'timer1_Tick

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles MyBase.Load

lwt.Create(Me)

lwt.Show()

End Sub 'Form1_Load

Private lwt As New LayeredWindowTe st

End Class 'Form1


Class LayeredWindowTe st

Inherits NativeWindow

<DllImport("Use r32.dll")> _

Public Shared Function MoveWindow(ByVa l hWnd As IntPtr, ByVal X As Integer,
ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal
bRepaint As Integer) As Integer

End Function

<DllImport("Use r32.dll")> _

Public Shared Function ShowWindow(ByVa l hWnd As System.IntPtr, ByVal cmdShow
As Short) As Integer

End Function

<DllImport("Use r32.dll")> _

Public Shared Function BeginPaint(ByVa l p As IntPtr, ByRef ps As
PAINTSTRUCT) As IntPtr

End Function

<DllImport("Use r32.dll")> _

Public Shared Function EndPaint(ByVal p As IntPtr, ByRef ps As PAINTSTRUCT)
As Integer

End Function

<DllImport("Use r32.dll")> _

Public Shared Function SetLayeredWindo wAttributes(ByV al hWnd As IntPtr,
ByVal clrKey As COLORREF, ByVal bAlpha As Byte, ByVal dwFlags As Integer) As
Integer

End Function

Private _internalLocati on As POINT

Private _location As POINT

Public Property Location() As POINT

Get

Return _location

End Get

Set(ByVal Value As POINT)

_location = Value

Dim tp As New Point(Value.X, Value.Y)

tp = _parent.PointTo Screen(tp)

_internalLocati on.X = tp.X

_internalLocati on.Y = tp.Y

If Not Me.Handle.Equal s(IntPtr.Zero) Then

MoveWindow(Me.H andle, _internalLocati on.X, _internalLocati on.Y, _size.Width,
_size.Height, 1)

End If

End Set

End Property

Private _size As Size

Public Property Size() As Size

Get

Return _size

End Get

Set(ByVal Value As Size)

_size = Value

If Not Me.Handle.Equal s(IntPtr.Zero) Then

MoveWindow(Me.H andle, _internalLocati on.X, _internalLocati on.Y, _size.Width,
_size.Height, 1)

End If

End Set

End Property

Private _parent As Control

Public Sub Create(ByVal parent As Control)

Me._parent = parent

Dim cp As New CreateParams

cp.Style = CInt(WSDefs.WS_ POPUP)

cp.ClassStyle = CInt(CSDefs.CS_ OWNDC)

cp.ExStyle = cp.ExStyle Or (CInt(WSDefs.WS _EX_LAYERED) Or
CInt(WSDefs.WS_ EX_TOPMOST))

cp.ClassName = Nothing

cp.Caption = Nothing

cp.X = -2

cp.Y = -2

cp.Width = 1

cp.Height = 1

cp.Parent = parent.Handle

CreateHandle(cp )

End Sub 'Create

Protected Overrides Sub WndProc(ByRef m As Message)

Select Case m.Msg

Case CInt(WMDefs.WM_ PAINT)

Dim g As Graphics

Dim ps As New PAINTSTRUCT

g = Graphics.FromHd c(BeginPaint(m. HWnd, ps))

g.SmoothingMode = SmoothingMode.N one

g.Clear(Color.M agenta)

Dim clipRect As Rectangle = Me._parent.Clie ntRectangle

clipRect.Offset (-Me.Location.X, -Me.Location.Y)

Dim p As New Pen(Color.Red, 3)

g.SmoothingMode = SmoothingMode.A ntiAlias

g.DrawEllipse(p , 2, 2, Me.Size.Width - 4, Me.Size.Height - 4)

p.Dispose()

g.Dispose()

EndPaint(m.HWnd , ps)

m.Result = New IntPtr(0)

Case Else

MyBase.WndProc( m)

End Select

End Sub 'WndProc

Public Sub Show()

If Not Me.Handle.Equal s(IntPtr.Zero) Then

ShowWindow(Me.H andle, CShort(SWDefs.S W_SHOWNOACTIVAT E))

Dim cr As New COLORREF

cr.Color = Color.Magenta

SetLayeredWindo wAttributes(Me. Handle, cr, 0, CInt(LWADefs.LW A_COLORKEY))

End If

End Sub 'Show

End Class 'LayeredWindowT est

Public Enum WMDefs

WM_NULL = &H0

WM_CREATE = &H1

WM_DESTROY = &H2

WM_MOVE = &H3

WM_SIZE = &H5

WM_ACTIVATE = &H6

WA_INACTIVE = 0

WA_ACTIVE = 1

WA_CLICKACTIVE = 2

WM_SETFOCUS = &H7

WM_KILLFOCUS = &H8

WM_ENABLE = &HA

WM_SETREDRAW = &HB

WM_SETTEXT = &HC

WM_GETTEXT = &HD

WM_GETTEXTLENGT H = &HE

WM_PAINT = &HF

WM_CLOSE = &H10

WM_QUERYENDSESS ION = &H11

WM_QUERYOPEN = &H13

WM_ENDSESSION = &H16

WM_QUIT = &H12

WM_ERASEBKGND = &H14

WM_SYSCOLORCHAN GE = &H15

WM_SHOWWINDOW = &H18

WM_WININICHANGE = &H1A

WM_SETTINGCHANG E = WM_WININICHANGE

WM_DEVMODECHANG E = &H1B

WM_ACTIVATEAPP = &H1C

WM_FONTCHANGE = &H1D

WM_TIMECHANGE = &H1E

WM_CANCELMODE = &H1F

WM_SETCURSOR = &H20

WM_MOUSEACTIVAT E = &H21

WM_CHILDACTIVAT E = &H22

WM_QUEUESYNC = &H23

WM_GETMINMAXINF O = &H24

WM_PAINTICON = &H26

WM_ICONERASEBKG ND = &H27

WM_NEXTDLGCTL = &H28

WM_SPOOLERSTATU S = &H2A

WM_DRAWITEM = &H2B

WM_MEASUREITEM = &H2C

WM_DELETEITEM = &H2D

WM_VKEYTOITEM = &H2E

WM_CHARTOITEM = &H2F

WM_SETFONT = &H30

WM_GETFONT = &H31

WM_SETHOTKEY = &H32

WM_GETHOTKEY = &H33

WM_QUERYDRAGICO N = &H37

WM_COMPAREITEM = &H39

WM_GETOBJECT = &H3D

WM_COMPACTING = &H41

WM_COMMNOTIFY = &H44

WM_WINDOWPOSCHA NGING = &H46

WM_WINDOWPOSCHA NGED = &H47

WM_POWER = &H48

PWR_OK = 1

PWR_FAIL = &HFFFF

PWR_SUSPENDREQU EST = 1

PWR_SUSPENDRESU ME = 2

PWR_CRITICALRES UME = 3

WM_COPYDATA = &H4A

WM_CANCELJOURNA L = &H4B

WM_NOTIFY = &H4E

WM_INPUTLANGCHA NGEREQUEST = &H50

WM_INPUTLANGCHA NGE = &H51

WM_TCARD = &H52

WM_HELP = &H53

WM_USERCHANGED = &H54

WM_NOTIFYFORMAT = &H55

NFR_ANSI = 1

NFR_UNICODE = 2

NF_QUERY = 3

NF_REQUERY = 4

WM_CONTEXTMENU = &H7B

WM_STYLECHANGIN G = &H7C

WM_STYLECHANGED = &H7D

WM_DISPLAYCHANG E = &H7E

WM_GETICON = &H7F

WM_SETICON = &H80

WM_NCCREATE = &H81

WM_NCDESTROY = &H82

WM_NCCALCSIZE = &H83

WM_NCHITTEST = &H84

WM_NCPAINT = &H85

WM_NCACTIVATE = &H86

WM_GETDLGCODE = &H87

WM_SYNCPAINT = &H88

WM_NCMOUSEMOVE = &HA0

WM_NCLBUTTONDOW N = &HA1

WM_NCLBUTTONUP = &HA2

WM_NCLBUTTONDBL CLK = &HA3

WM_NCRBUTTONDOW N = &HA4

WM_NCRBUTTONUP = &HA5

WM_NCRBUTTONDBL CLK = &HA6

WM_NCMBUTTONDOW N = &HA7

WM_NCMBUTTONUP = &HA8

WM_NCMBUTTONDBL CLK = &HA9

WM_NCXBUTTONDOW N = &HAB

WM_NCXBUTTONUP = &HAC

WM_NCXBUTTONDBL CLK = &HAD

WM_INPUT = &HFF

WM_KEYFIRST = &H100

WM_KEYDOWN = &H100

WM_KEYUP = &H101

WM_CHAR = &H102

WM_DEADCHAR = &H103

WM_SYSKEYDOWN = &H104

WM_SYSKEYUP = &H105

WM_SYSCHAR = &H106

WM_SYSDEADCHAR = &H107

WM_UNICHAR = &H109

WM_KEYLAST = &H108

UNICODE_NOCHAR = &HFFFF

WM_IME_STARTCOM POSITION = &H10D

WM_IME_ENDCOMPO SITION = &H10E

WM_IME_COMPOSIT ION = &H10F

WM_IME_KEYLAST = &H10F

WM_INITDIALOG = &H110

WM_COMMAND = &H111

WM_SYSCOMMAND = &H112

WM_TIMER = &H113

WM_HSCROLL = &H114

WM_VSCROLL = &H115

WM_INITMENU = &H116

WM_INITMENUPOPU P = &H117

WM_MENUSELECT = &H11F

WM_MENUCHAR = &H120

WM_ENTERIDLE = &H121

WM_MENURBUTTONU P = &H122

WM_MENUDRAG = &H123

WM_MENUGETOBJEC T = &H124

WM_UNINITMENUPO PUP = &H125

WM_MENUCOMMAND = &H126

WM_CHANGEUISTAT E = &H127

WM_UPDATEUISTAT E = &H128

WM_QUERYUISTATE = &H129

UIS_SET = 1

UIS_CLEAR = 2

UIS_INITIALIZE = 3

UISF_HIDEFOCUS = &H1

UISF_HIDEACCEL = &H2

UISF_ACTIVE = &H4

WM_CTLCOLOR = &H19

WM_CTLCOLORMSGB OX = &H132

WM_CTLCOLOREDIT = &H133

WM_CTLCOLORLIST BOX = &H134

WM_CTLCOLORBTN = &H135

WM_CTLCOLORDLG = &H136

WM_CTLCOLORSCRO LLBAR = &H137

WM_CTLCOLORSTAT IC = &H138

MN_GETHMENU = &H1E1

WM_MOUSEFIRST = &H200

WM_MOUSEMOVE = &H200

WM_LBUTTONDOWN = &H201

WM_LBUTTONUP = &H202

WM_LBUTTONDBLCL K = &H203

WM_RBUTTONDOWN = &H204

WM_RBUTTONUP = &H205

WM_RBUTTONDBLCL K = &H206

WM_MBUTTONDOWN = &H207

WM_MBUTTONUP = &H208

WM_MBUTTONDBLCL K = &H209

WM_MOUSEWHEEL = &H20A

WM_XBUTTONDOWN = &H20B

WM_XBUTTONUP = &H20C

WM_XBUTTONDBLCL K = &H20D

WM_MOUSELAST = &H20D

WHEEL_DELTA = 120

XBUTTON1 = &H1

XBUTTON2 = &H2

WM_PARENTNOTIFY = &H210

WM_ENTERMENULOO P = &H211

WM_EXITMENULOOP = &H212

WM_NEXTMENU = &H213

WM_SIZING = &H214

WM_CAPTURECHANG ED = &H215

WM_MOVING = &H216

WM_POWERBROADCA ST = &H218

PBT_APMQUERYSUS PEND = &H0

PBT_APMQUERYSTA NDBY = &H1

PBT_APMQUERYSUS PENDFAILED = &H2

PBT_APMQUERYSTA NDBYFAILED = &H3

PBT_APMSUSPEND = &H4

PBT_APMSTANDBY = &H5

PBT_APMRESUMECR ITICAL = &H6

PBT_APMRESUMESU SPEND = &H7

PBT_APMRESUMEST ANDBY = &H8

PBTF_APMRESUMEF ROMFAILURE = &H1

PBT_APMBATTERYL OW = &H9

PBT_APMPOWERSTA TUSCHANGE = &HA

PBT_APMOEMEVENT = &HB

PBT_APMRESUMEAU TOMATIC = &H12

WM_DEVICECHANGE = &H219

WM_MDICREATE = &H220

WM_MDIDESTROY = &H221

WM_MDIACTIVATE = &H222

WM_MDIRESTORE = &H223

WM_MDINEXT = &H224

WM_MDIMAXIMIZE = &H225

WM_MDITILE = &H226

WM_MDICASCADE = &H227

WM_MDIICONARRAN GE = &H228

WM_MDIGETACTIVE = &H229

WM_MDISETMENU = &H230

WM_ENTERSIZEMOV E = &H231

WM_EXITSIZEMOVE = &H232

WM_DROPFILES = &H233

WM_MDIREFRESHME NU = &H234

WM_IME_SETCONTE XT = &H281

WM_IME_NOTIFY = &H282

WM_IME_CONTROL = &H283

WM_IME_COMPOSIT IONFULL = &H284

WM_IME_SELECT = &H285

WM_IME_CHAR = &H286

WM_IME_REQUEST = &H288

WM_IME_KEYDOWN = &H290

WM_IME_KEYUP = &H291

WM_MOUSEHOVER = &H2A1

WM_MOUSELEAVE = &H2A3

WM_NCMOUSELEAVE = &H2A2

WM_WTSSESSION_C HANGE = &H2B1

WM_TABLET_FIRST = &H2C0

WM_TABLET_LAST = &H2DF

WM_CUT = &H300

WM_COPY = &H301

WM_PASTE = &H302

WM_CLEAR = &H303

WM_UNDO = &H304

WM_RENDERFORMAT = &H305

WM_RENDERALLFOR MATS = &H306

WM_DESTROYCLIPB OARD = &H307

WM_DRAWCLIPBOAR D = &H308

WM_PAINTCLIPBOA RD = &H309

WM_VSCROLLCLIPB OARD = &H30A

WM_SIZECLIPBOAR D = &H30B

WM_ASKCBFORMATN AME = &H30C

WM_CHANGECBCHAI N = &H30D

WM_HSCROLLCLIPB OARD = &H30E

WM_QUERYNEWPALE TTE = &H30F

WM_PALETTEISCHA NGING = &H310

WM_PALETTECHANG ED = &H311

WM_HOTKEY = &H312

WM_PRINT = &H317

WM_PRINTCLIENT = &H318

WM_APPCOMMAND = &H319

WM_THEMECHANGED = &H31A

WM_HANDHELDFIRS T = &H358

WM_HANDHELDLAST = &H35F

WM_AFXFIRST = &H360

WM_AFXLAST = &H37F

WM_PENWINFIRST = &H380

WM_PENWINLAST = &H38F

WM_USER = &H400

WM_REFLECT = &H2000

WM_APP = &H8000

End Enum 'WMDefs

Public Enum CSDefs

CS_VREDRAW = &H1

CS_HREDRAW = &H2

CS_DBLCLKS = &H8

CS_OWNDC = &H20

CS_CLASSDC = &H40

CS_PARENTDC = &H80

CS_NOCLOSE = &H200

CS_SAVEBITS = &H800

CS_BYTEALIGNCLI ENT = &H1000

CS_BYTEALIGNWIN DOW = &H2000

CS_GLOBALCLASS = &H4000

CS_IME = &H10000

CS_DROPSHADOW = &H20000

End Enum 'CSDefs

Public Enum LWADefs

LWA_COLORKEY = &H1

LWA_ALPHA = &H2

End Enum 'LWADefs

<Flags()> _

Public Enum SWDefs

SW_HIDE = 0

SW_SHOWNORMAL = 1

SW_NORMAL = 1

SW_SHOWMINIMIZE D = 2

SW_SHOWMAXIMIZE D = 3

SW_MAXIMIZE = 3

SW_SHOWNOACTIVA TE = 4

SW_SHOW = 5

SW_MINIMIZE = 6

SW_SHOWMINNOACT IVE = 7

SW_SHOWNA = 8

SW_RESTORE = 9

SW_SHOWDEFAULT = 10

SW_FORCEMINIMIZ E = 11

SW_MAX = 11

End Enum 'SWDefs

Public Enum WSDefs

WS_OVERLAPPED = &H0

WS_POPUP = &H80000000

WS_CHILD = &H40000000

WS_MINIMIZE = &H20000000

WS_VISIBLE = &H10000000

WS_DISABLED = &H8000000

WS_CLIPSIBLINGS = &H4000000

WS_CLIPCHILDREN = &H2000000

WS_MAXIMIZE = &H1000000

WS_CAPTION = &HC00000 ' WS_BORDER or WS_DLGFRAME

WS_BORDER = &H800000

WS_DLGFRAME = &H400000

WS_VSCROLL = &H200000

WS_HSCROLL = &H100000

WS_SYSMENU = &H80000

WS_THICKFRAME = &H40000

WS_GROUP = &H20000

WS_TABSTOP = &H10000

WS_MINIMIZEBOX = &H20000

WS_MAXIMIZEBOX = &H10000

WS_TILED = WS_OVERLAPPED

WS_ICONIC = WS_MINIMIZE

WS_SIZEBOX = WS_THICKFRAME

WS_TILEDWINDOW = WS_OVERLAPPEDWI NDOW

'

' * Common Window Styles

'

WS_OVERLAPPEDWI NDOW = (WS_OVERLAPPED Or _

WS_CAPTION Or _

WS_SYSMENU Or _

WS_THICKFRAME Or _

WS_MINIMIZEBOX Or _

WS_MAXIMIZEBOX)

WS_POPUPWINDOW = (WS_POPUP Or _

WS_BORDER Or _

WS_SYSMENU)

WS_CHILDWINDOW = WS_CHILD

'

' * Extended Window Styles

'

WS_EX_DLGMODALF RAME = &H1

WS_EX_NOPARENTN OTIFY = &H4

WS_EX_TOPMOST = &H8

WS_EX_ACCEPTFIL ES = &H10

WS_EX_TRANSPARE NT = &H20

WS_EX_MDICHILD = &H40

WS_EX_TOOLWINDO W = &H80

WS_EX_WINDOWEDG E = &H100

WS_EX_CLIENTEDG E = &H200

WS_EX_CONTEXTHE LP = &H400

WS_EX_RIGHT = &H1000

WS_EX_LEFT = &H0

WS_EX_RTLREADIN G = &H2000

WS_EX_LTRREADIN G = &H0

WS_EX_LEFTSCROL LBAR = &H4000

WS_EX_RIGHTSCRO LLBAR = &H0

WS_EX_CONTROLPA RENT = &H10000

WS_EX_STATICEDG E = &H20000

WS_EX_APPWINDOW = &H40000

WS_EX_OVERLAPPE DWINDOW = (WS_EX_WINDOWED GE Or WS_EX_CLIENTEDG E)

WS_EX_PALETTEWI NDOW = (WS_EX_WINDOWED GE Or WS_EX_TOOLWINDO W Or
WS_EX_TOPMOST)

WS_EX_LAYERED = &H80000

WS_EX_NOINHERIT LAYOUT = &H100000 ' Disable inheritence of mirroring by
children

WS_EX_LAYOUTRTL = &H400000 ' Right to left mirroring

WS_EX_COMPOSITE D = &H2000000

WS_EX_NOACTIVAT E = &H8000000

End Enum 'WSDefs

Public Structure COLORREF

Public _color As Integer

Public Property Color() As Color

Get

Return ColorTranslator .FromWin32(_col or)

End Get

Set(ByVal Value As Color)

_color = ColorTranslator .ToWin32(Value)

End Set

End Property

End Structure 'COLORREF

Public Structure PAINTSTRUCT

Public hdc As IntPtr

Public fErase As Integer

Public rcPaint As RECT

Public fRestore As Integer

Public fIncUpdate As Integer

Public rgbReserved1 As Long

Public rgbReserved2 As Long

Public rgbReserved3 As Long

Public rgbReserved4 As Long

End Structure 'PAINTSTRUCT

<StructLayout(L ayoutKind.Seque ntial)> _

Public Structure RECT

Public left As Integer

Public top As Integer

Public right As Integer

Public bottom As Integer

Public ReadOnly Property Width() As Integer

Get

Return right - left

End Get

End Property

Public ReadOnly Property Height() As Integer

Get

Return bottom - top

End Get

End Property

End Structure 'RECT

End Namespace 'LayeredWindowT est


"Richard" <a@b.com> wrote in message
news:vg******** ************@tw ister.nyc.rr.co m...I have a requirement to put a GDI style circle or rectangle border around
the selected row of a datagrid/ It will overlap into the row above and
below
the selected row. Doing this in a the OnPaint of a subclassed
DataGridTextBox Colum dos not seem like a practical way to do it.

I have subclassed a DataGrid and overridden the OnPaint as such:

protected override void OnPaint(PaintEv entArgs pe)

{

base.OnPaint(pe );

// Create pen.

Pen blackPen = new Pen(Color.Green , 2);

// Create location and size of rectangle.

float x = 15.0F;

float y = 53.0F;

float width = 500.0F;

float height = 20.0F;

// Draw rectangle to screen.

pe.Graphics.Dra wRectangle(blac kPen, x, y, width, height);

}

It works but has refresh problems.

Last week Bob Powell [MVP] suggested that I use a NativeWindow class to
solve this problem. I've just had the time to get back to this and not
being
a former C++ person, etc., etc., etc. maybe someone can help with this.

The MSDN help screen for the NativeWindow class is this link:

http://msdn.microsoft.com/library/de...classtopic.asp

It has an example and in the subclassed NativeWindow constructor it
creates
a button:

public MyNativeWindow( Form parent)

{

..see help example

}

Can someone advise me on where my code above that paints the Rectangle
should go in this NativeWindow example?


Nov 17 '05 #3
Hello

Yes, this is the approach I was trying to avoid. How would your method/code
work if it was the top row of the DataGrid. How would you suggest making the
part of the circle that will have to go over the column headings or in fact
even the title bar?

Thank you,

Richard
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:O0******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

You should create a datagrid column style to do that. Here is an
example of an image column that shows how to tell when the column is
selected. You will have to draw your circle in the paint event. Keep in
mind that you have to draw part of the circle in each column shown.

Imports System.Drawing

Imports System.Drawing. Drawing2D

Imports System.Windows. Forms

Imports System.Reflecti on

Imports System.IO

Public Class DataGridImageCo lumn

Inherits DataGridTextBox Column

Dim WithEvents dg As DataGrid

Private arHeights As ArrayList

Private mintOffset As Integer

Private cm As CurrencyManager

Public Sub New()

Me.NullText = "Image not available."

End Sub

Private Sub GetHeightList()

Dim mi As MethodInfo = dg.GetType().Ge tMethod("get_Da taGridRows",
BindingFlags.Fl attenHierarchy Or BindingFlags.Ig noreCase Or
BindingFlags.In stance Or BindingFlags.No nPublic Or BindingFlags.Pu blic Or
BindingFlags.St atic)

Dim dgra As Array = CType(mi.Invoke (Me.dg, Nothing), Array)

arHeights = New ArrayList

Dim dgRowHeight As Object

For Each dgRowHeight In dgra

If dgRowHeight.ToS tring().EndsWit h("DataGridRela tionshipRow") = True Then

arHeights.Add(d gRowHeight)

End If

Next

End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing. Graphics, ByVal bounds As System.Drawing. Rectangle, ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal
backBrush As System.Drawing. Brush, ByVal foreBrush As System.Drawing. Brush, ByVal alignToRight As Boolean)

Static bPainted As Boolean = False

Static bMove As Boolean = True

If Not bPainted Then

dg = Me.DataGridTabl eStyle.DataGrid

cm = source

GetHeightList()

End If

If source.Count - 1 = rowNum And bMove Then

'

' Force resize so scroll bars show

'

dg.Size = New Size(dg.Size.Wi dth, dg.Height - 1)

bMove = False

dg.Size = New Size(dg.Size.Wi dth, dg.Height + 1)

End If

'clear the cell

g.FillRectangle (backBrush, bounds)

Dim objData As Object = Me.GetColumnVal ueAtRow([source], rowNum)

If TypeOf objData Is System.DBNull Then

Dim sf As New StringFormat

With sf

.Alignment = StringAlignment .Center

.LineAlignment = StringAlignment .Center

End With

g.DrawString(Me .NullText, Me.TextBox.Font , Brushes.Black, _

RectangleF.op_I mplicit(bounds) , sf)

Else

Try

Dim bm As Bitmap

If TypeOf objData Is String Then

Dim fs As New FileStream(objD ata.ToString, FileMode.Open)

bm = CType(Bitmap.Fr omStream(fs), Bitmap)

fs.Close()

Else

Try

bm = ConvertToBitmap (DirectCast(obj Data, Byte()), 78)

Catch

bm = ConvertToBitmap (DirectCast(obj Data, Byte()), 0)

End Try

End If

Dim h As Integer

Dim s As Double

s = (Me.Width / bm.Width)

h = CInt(bm.Height * s)

If s >= 1 Then

h = bm.Height

End If

AdjustHeight(h)

Dim r As Rectangle

If Me.Width > bm.Width Then

r = New Rectangle(bound s.Left, bounds.Top, bm.Width, bm.Height)

Else

r = New Rectangle(bound s.Left, bounds.Top, Me.Width, h)

End If

g.DrawImage(bm, r)

If dg.IsSelected(r owNum) Then

Dim br As New SolidBrush(Colo r.FromArgb(64, _

Me.DataGridTabl eStyle.Selectio nBackColor))

g.FillRectangle (br, bounds)

br.Dispose()

End If

Catch ex As Exception

Dim sf As New StringFormat

With sf

.Alignment = StringAlignment .Center

.LineAlignment = StringAlignment .Center

End With

g.DrawString("I nvalid image format", Me.TextBox.Font , Brushes.Black, _

RectangleF.op_I mplicit(bounds) , sf)

End Try

End If

bPainted = True

End Sub

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows. Forms.CurrencyM anager, ByVal rowNum As Integer, ByVal bounds As System.Drawing. Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)

End Sub

Private Sub AdjustHeight(By Val h As Integer)

Dim curHeight As Integer = Me.DataGridTabl eStyle.Preferre dRowHeight

If h > curHeight Then

'

' Manually set all the row heights to the new height

'

Me.DataGridTabl eStyle.Preferre dRowHeight = h

Trace.WriteLine (h)

If arHeights.Count < cm.Count Then

GetHeightList()

End If

For rownum As Integer = 0 To cm.Count - 1

Try

Dim pi As PropertyInfo

pi = arHeights(rownu m).GetType().Ge tProperty("Heig ht")

' adjust height

If h > curHeight Then

pi.SetValue(arH eights(rownum), h, Nothing)

End If

Catch

' something wrong leave default height

Debug.WriteLine ("Error")

End Try

Next

'

' Resize datagrid to get scrollbars to work right

'

Dim sz As Size = dg.Size

dg.Size = New Size(New Point(sz.Width - 1, sz.Height - 1))

dg.Size = sz

End If

End Sub

Private Function ConvertToBitmap (ByVal data() As Byte, ByVal offset As
Integer) As Bitmap

Dim ms As New System.IO.Memor yStream

Dim bm As Bitmap

ms = New MemoryStream

ms.Write(data, offset, data.Length - offset)

bm = New Bitmap(ms)

Return bm

End Function

<DataSysDescrip tion("Suspends painting of column until EndUpdate is
called")> _

Public Shadows Sub BeginUpdate()

MyBase.BeginUpd ate()

End Sub

<DataSysDescrip tion("Resumes painting of column")> _

Public Shadows Sub EndUpdate()

MyBase.EndUpdat e()

End Sub

End Class

Ken

---------------------

"Richard" <a@b.com> wrote in message
news:vg******** ************@tw ister.nyc.rr.co m...
I have a requirement to put a GDI style circle or rectangle border around
the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint of a subclassed
DataGridTextBox Colum dos not seem like a practical way to do it.

I have subclassed a DataGrid and overridden the OnPaint as such:

protected override void OnPaint(PaintEv entArgs pe)

{

base.OnPaint(pe );

// Create pen.

Pen blackPen = new Pen(Color.Green , 2);

// Create location and size of rectangle.

float x = 15.0F;

float y = 53.0F;

float width = 500.0F;

float height = 20.0F;

// Draw rectangle to screen.

pe.Graphics.Dra wRectangle(blac kPen, x, y, width, height);

}

It works but has refresh problems.

Last week Bob Powell [MVP] suggested that I use a NativeWindow class to
solve this problem. I've just had the time to get back to this and not being a former C++ person, etc., etc., etc. maybe someone can help with this.

The MSDN help screen for the NativeWindow class is this link:

http://msdn.microsoft.com/library/de...classtopic.asp
It has an example and in the subclassed NativeWindow constructor it creates a button:

public MyNativeWindow( Form parent)

{

..see help example

}

Can someone advise me on where my code above that paints the Rectangle
should go in this NativeWindow example?


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3340
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
19
676
by: Kamilche | last post by:
I have looked at many object-oriented programming frameworks out there for C. Though the ideas presented are intriguing, and I've used some of them in my own work, they all suffered some drawback or another. I have created a new method of doing OOP with C, something that fits my needs particularly well. I haven't encountered anything else like it, so I'm tossing it out to the Internet for comment. ...
3
1846
by: Tobias Froehlich | last post by:
Hello, I'd like to create a drawing field on a form in which I can draw (using the Pen class I guess). I've found tutorials on how to use the GDI+ but I still haven't found out how to actually create a BOX (with a specified size) in which the user can draw (he shouldn't be able to draw on the whole form / window!). I will also need to be able to get information on the drawn pixels in that box (specifically: i need to draw a rectangular...
0
2191
by: Marco Segurini | last post by:
Hi, I am trying to dynamically install/deinstall a message handler to a System.Windows.Forms.Form using NativeWindow. I do not use IMessageFilter derived class because it intercept only the PostMessage messages. My problem is that when I install the message handler the second time a "Fatal stack overflow error" occurs.
5
20296
by: Vin | last post by:
Hi, I am using the following code to draw whatever the user draws using x,y. // draws lines directly on a winform. CreateGraphics().DrawLine(APen, x, y, OldX, OldY); Now how do I save the drawing on to a bmp file on my harddisk? C# code in this regard would be very helpful. I tried all forums but invain.
3
10335
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case shapeType Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10) Case 2 'draw a circle Case 3 'draw a triangle Case 4 'draw other shape Case 5 'draw other shape
3
5165
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
7
2585
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be stupid or missing the point. I used DrawString( ) as a simple test but I cannot get it to work at all unless I handle my custom controls Paint event.
14
2981
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main reason you would want to do this is for example to trigger something from an OnPaint event without resorting to boolean switches-- say if a user presses the "M" key while the program is Painting, the user gets the PaintHandler to do something else. ...
0
9892
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9734
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10718
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7888
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7061
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4129
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3168
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.