Reputation: 11652
Ok so I'm trying to understand the very basics of how "Controls" are built in Windows. I don't know how the individual bits and pieces inside a ListView control (for example) are drawn.
Let's take a ListView, and add 3 items to it.
How do those items get there to begin with?
How do they select an item when the user selects an item? - Do they: get the x & y coordinates of the mouse in the on mouse down event, then look for any items that were drawn in that location in an internal List and then redraw that particular item in that location with a different colored background to convey to the user that the item was selected?
How do they know which item (and how do they) to select when the user already selected an item and then pressed the up or down arrow key? - do they get the cords of the last selected item, then adjust those coords so that they fall in line with the next item to be selected?
And finally, if text is "drawn" in the manner I speak of, then basically it's just pixels, right? So, how am I supposed to "know" what the text says when the user clicks in the same location. Am I supposed to store all pieses of text that are drawn as a string in a List along with the coordinates, too?
Upvotes: 0
Views: 190
Reputation: 9089
Your questions seem more of how it works than how to do it so hopefully this will give you an idea.
Let's take a ListView, and add 3 items to it. How do those items get there to begin with?
Do they draw the list item text at a specified location within the listview control in the OnPaint event of the listview? (for example: listView1_Paint(...) { graphics.DrawString(...); }
All items in the list view are stored in a collection (ListView.Items). Each row is a ListViewItem
which contains the necessary pieces of information for rendering and interacting with that row.
They either draw it in the ListView's OnPaint or some other method on the ListViewItem. Normally painting is factored out to most logical place. I can't speak specifically for ListView, but when I was building my Grid, I had each Cell do their own drawing.
How do they select an item when the user selects an item? - Do they: get the x & y coordinates of the mouse in the on mouse down event...
Yes, yes they do. This is known as "Hit Testing". Notice the HitTest(...)
method on ListView. Depending on the specific implementation of the control, each item is measured to properly calculate the scrollbar values and when a mouse event occurs, a hit test occurs against the mouse information in conjunction with the scrollbar(s) to determine exactly which part of the control is being interacted with.
How do they know which item (and how do they) to select when the user already selected an item and then pressed the up or down arrow key? - do they get the cords of the last selected item, then adjust those coords so that they fall in line with the next item to be selected?
It's all just an array of items. They know is currently selected. There's a bunch of different ways to do it, but one way is to keep a IsSelected
boolean on an item and then in the OnPaint()
call, render it differently if it is selected. Moving up and down is as simple as just increasing or decreasing the index of the selected item. The real fun is when you need to start moving up and down through sub-items and handling multi-selection with the keyboard.
And finally, if text is "drawn" in the manner I speak of, then basically it's just pixels, right? So, how am I supposed to "know" what the text says when the user clicks in the same location. Am I supposed to store all pieses of text that are drawn as a string in a List along with the coordinates, too?
It is just pixels. That text was specifically drawn. You may be able to answer this simply by asking yourself, "Well, how did I know what text to draw in the first place?".
Remember, there will be a collection of items that have all the information required for drawing. When a HitTest
happens, you should be getting back either the object that contains the information for drawing, or some other information to get it.
The main thing to keep in mind is that you should have objects that store all the information you need. Look at the ListViewItem class to get an idea.
Upvotes: 1