Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Applications and operating systems have a ton of windows and controls, sub windows and sub controls. I've noticed its common to have sub controls have their coordinate systems reset to start at 0 making drawing much easier. However I'm not sure how that is done and how I'd be able to do that.

I'm going to be creating an application that does a lot of 2d drawing and includes things like my own implemented scrollbars and lots of controls that have sub controls. I want the coordinate system of sub controls to reset to 0 and I want the controls to have minimal knowledge of their location within parent controls, and parent parent controls, ect.

The only thing I can think of is to pass in transform coordinates and have each sub control when drawing add the x and y transform coordinates to their objects being drawn so it gets transformed to the correct location. That quickly gets very tiring when I know there are better ways.

I'm not sure what I'll be using, weather it will be GDI+, OpenGL, or Direct2D. I'm just looking for general information on how this done so sub controls can draw at x and y 0 and it pops up at whatever location the actual control is located at.

share|improve this question
    
most gui framework graphics objects allow you to make shifts and clip regions to do exactly that similar to java's Graphics.create(int,int,int,int) –  ratchet freak Jul 11 '13 at 0:51
add comment

1 Answer

up vote 1 down vote accepted

I did this once before, at the time I even implemented the drawing algorithms. About the way it works in Windows at least, it requires a handle for a device context (DC), that handle identifies an unit in the screen, a window, a control, etc.

With that handle you have an extra layer that converts relative positions to an actual position in the screen, and that also has the clipping area, so you don't draw outside of it, and other stuff. The transform matrix you refer to kinda exists, it's called viewport (I can not say if it's implemented like that). Each time you are going to paint a control you request the DC and call drawing functions to work with that DC.

If you want to implement all of that by yourself you may still use standard windows and use the device context provided by them. Or you may simulate each unit in the window, and create that layer that will handle the drawing operations. In this case you also need to distribute events to the target control, but you will require the coordinates of the control.

share|improve this answer
    
Ahh I'm starting to get this now and am able to Google further effectively, thank you! –  Dimension Jul 11 '13 at 1:05
    
You're welcome! –  lgarcia Jul 11 '13 at 1:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.