Jump to content

  • Log In with Google      Sign In   
  • Create Account


Drawing elements on a GDI control

  • You cannot reply to this topic
7 replies to this topic

#1 knos991   Members   -  Reputation: 111

Like
0Likes
Like

Posted 05 September 2014 - 06:37 AM

I'd like to draw simple shapes (e.g. a triangle filled with a color) in one of my GDI+ controls. How should I go for that?

If I'm not mistaken one might store the bitmap for the triangle in the resources and then call LoadBitmap, but I don't really like this method. Is there any other way of doing this?

Is it possible to extract a bitmap of the scene and somehow draw to it? I definitely need some suggestions here please



Sponsor:

#2 Buckeye   Crossbones+   -  Reputation: 4340

Like
1Likes
Like

Posted 05 September 2014 - 08:45 AM


in one of my GDI+ controls

 

Are you subclassing a Win32 control, or do you have a custom control class that you implement with GDI+?

 

If the latter, you should look at the Graphics.FillPath function. Creating a path by adding lines to form the desired triangle and filling that path with a SolidBrush of the appropriate color should serve your needs.


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#3 knos991   Members   -  Reputation: 111

Like
0Likes
Like

Posted 05 September 2014 - 09:24 AM

Thanks! Yes I'm creating custom GDI+ controls and drawing them from scratch.
 
One more question if I may: I'm trying to create an entire custom UI and right now I'm doing something like this in the window's WM_PAINT event

case WM_PAINT:
    for_each( visible_control )
        paint_that_control(window_hdc);

I somehow feel that this approach is painfully slow and I'd like some advice.. I'd like to avoid creating multiple win32 elements, is there another better approach?



#4 Buckeye   Crossbones+   -  Reputation: 4340

Like
0Likes
Like

Posted 05 September 2014 - 10:03 AM


I'm trying to create an entire custom UI

 

Considering the existing Win32 API has been in development for decades, you've got a long row to hoe. As it appears you've already started to use the Win32 API, why not continue?

 

EDIT: Do you, perhaps, mean you want a custom look to the Win32 interface? happy.png  Or do you really intend to change the behavior of the controls? If the latter, that's going to confuse your users as they will expect a button to behave in the same way, whatever it looks like.

 

 


I'm doing something like this in the window's WM_PAINT event <snippet>

 

An easier alternative may be to do it similarly to Win32. Create each control window as a child to the main window, store a pointer to your custom class or a data structure in the control window's userdata area (or subclass it by changing the HWND's window procedure to your custom control WNDPROC), and let the main window itself dispatch the paint calls to its children.


Edited by Buckeye, 05 September 2014 - 10:14 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#5 knos991   Members   -  Reputation: 111

Like
0Likes
Like

Posted 05 September 2014 - 01:18 PM

Since I'm a newbie I've probably written something terribly wrong, sorry biggrin.png

 

What I'm trying to do is to render my controls with GDI+ into a window client area (every single one I use... the app is a simple one and doesn't use many :) )

 

Since I'm rendering everything in that area (and I wanted those controls to "blend a bit" together) I kind of disliked the idea of having multiple different win32 control windows. Is there an alternative?



#6 Buckeye   Crossbones+   -  Reputation: 4340

Like
0Likes
Like

Posted 05 September 2014 - 01:48 PM


I kind of disliked the idea of having multiple different win32 control windows.

 

Any particular reason? That's the common method for controls.

 

 

 


Is there an alternative?

 

Render them as you posted above.


Edited by Buckeye, 05 September 2014 - 01:51 PM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#7 knos991   Members   -  Reputation: 111

Like
0Likes
Like

Posted 05 September 2014 - 02:39 PM

The reason not to have multiple win32 controls is that I want to "blend those controls" together a bit... and having different stuff won't probably work (or will become messy with signals).

 

What I'm really interested in is if there's an advantage on using win32 window controls instead of rendering my own ones: how are signals for redrawing parts of the client area handled if I render my controls?

 

Do I have to redraw everything each time even a small corner of the client win32 area gets covered and then uncovered?



#8 Buckeye   Crossbones+   -  Reputation: 4340

Like
0Likes
Like

Posted 05 September 2014 - 06:46 PM


I want to "blend those controls" together a bit... and having different stuff won't probably work (or will become messy with signals)

 

Not sure what you mean by "blend," I'm afraid. Graphics? Functionality? Also, difficult to respond to a question when you use technical terms like "stuff" and "messy." wink.png

 

As mentioned above, Win32 controls are child windows of another window. They don't get "signals" - Win32 windows use a window messaging system. A control, being a window, gets a message to repaint only when all or a portion of the window needs to be redrawn.

 

With regard to handling child window messages, considering the questions you're asking, I would strongly suggest you get familiar with how parent-child communication is handled in the Win32 API. Reading up on the messaging system, and creating a small project to try the things you want to do will really be a benefit - whether you draw your own objects or use controls. There are a couple ways to approach drawing your own controls - google for things like "BS_OWNERDRAW" and "windows controls subclassing."

 

You may also benefit from purchasing a copy of Charles Petzold's book (Microsoft Press) "Programming Windows." Though the 5th edition is copyright 1998, I haven't found anything that doesn't work as advertised as late as Windows 7. It comes with a lot of example code that is very straightforward, compiles and runs out-of-the-box.

 


What I'm really interested in is if there's an advantage on using win32 window controls

 

I mentioned some of the drawing aspects above. The primary advantage of using the API controls (whether you do the drawing or not), is you won't have to code response behavior to button-clicks, dropdowns, mouseovers, getting/losing focus, etc., etc. Petzold devotes a chapter to Child Window Controls.


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.






PARTNERS