Results 1 to 10 of 10

Thread: [RESOLVED] Drawing on Windowless Usercontrols

Hybrid View

  1. #1
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Drawing on Windowless Usercontrols

    Welcome to the forums.

    1. This is an excellent reference for usercontrols. Note that I'm having trouble accessing the site at the moment, but it's been up for years. Directly addresses VB5 but VB6 is not that different in this case.

    2. Windowless controls can have their own DC or not (HasDC property).
    a. Own DC: You draw on to the DC (same size as control) and VB transfers it to the form automatically
    b. No DC: During the control's Paint event, VB passes you a clipped DC that you draw on directly. That DC is the control's parent's DC. Before the DC is sent to you, anything lower in the zOrder of your control that occupies the same space as your control is already drawn, including the background color as applicable.

    3. Drawing text or anything on the control is done by drawing to the DC.
    a. Has DC: use UserControl.hDC value. The Paint event is not called if AutoRedraw is True. If True, .Refresh control after drawing
    b. No DC: use UserControl.hDC only in the control's Paint event. Accessing it outside of the event can/will produce an error since the control has no DC allocated. AutoRedraw cannot be set on DC-less controls.

    Edited: The "No DC" option is how you can ensure transparency. Other methods include using the MaskPicture and MaskColor properties but are somewhat limited.

    Regarding subclassing. Each usercontrol has a containerHwnd property.

    Hope this clears up some issues.
    Last edited by LaVolpe; Aug 18th, 2010 at 01:30 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    5

    Resolved Re: Drawing on Windowless Usercontrols

    Quote Originally Posted by LaVolpe View Post
    Welcome to the forums.
    b. No DC: use UserControl.hDC only in the control's Paint event. Accessing it outside of the event can/will produce an error since the control has no DC allocated. AutoRedraw cannot be set on DC-less controls.

    Edited: The "No DC" option is how you can ensure transparency. Other methods include using the MaskPicture and MaskColor properties but are somewhat limited.
    This solved the issue! It seems VB holds a buffer DC which is available in Paint event thus allowing you double-buffered rendering along with transparency! Really nice. I just call native Refresh method in my properties Set/Let handlers to request repainting.

    That's what I've been missing, you're the man LaVolpe.

    @petersen
    Edit control would be an overkill, this is just a label . I was going for no permanent GDI handles and/or window handles.

    @some1uk03
    I've put LaVolpe's explanation to use and it worked great. All you have to do is draw inside the Paint event with BackgroundStyle set to Transparent! That way your DC will be laid out automatically by VB to the container surface. This eliminates the need for double buffering as well so you can just draw directly to the control's DC.

    Thanks for help all!

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Drawing on Windowless Usercontrols

    Quote Originally Posted by SassyCat View Post
    This solved the issue! It seems VB holds a buffer DC which is available in Paint event thus allowing you double-buffered rendering along with transparency! Really nice. I just call native Refresh method in my properties Set/Let handlers to request repainting.

    That's what I've been missing, you're the man LaVolpe.
    I've designed many windowless controls using dc-less options and it works quite well.

    One word of advice. Depending on complexity of your drawing (i.e., time consuming), you may want to use an offscreen buffer, copy the passed DC to your offscreen, do the complex drawing and then copy the offscreen back to the passed DC. Also, should you be selecting anything into the passed DC, cache what you pulled out and put it back in else unexpected results can occur.
    Last edited by LaVolpe; Aug 19th, 2010 at 11:37 AM. Reason: typos
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    New Member
    Join Date
    Jan 2013
    Posts
    2

    Re: Drawing on Windowless Usercontrols

    Quote Originally Posted by LaVolpe View Post
    I've designed many windowless controls using dc-less options and it works quite well.

    One word of advice. Depending on complexity of your drawing (i.e., time consuming), you may want to use an offscreen buffer, copy the passed DC to your offscreen, do the complex drawing and then copy the offscreen back to the passed DC. Also, should you be selecting anything into the passed DC, cache what you pulled out and put it back in else unexpected results can occur.
    Thank you very much guys and especially LaVolpe, you just saved my day as I was writting a Image Button component to freshen an old application UI... this forum is just so great!

    Have a nice day

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width