Solved

how to draw a string to a picturebox in XOR mode

Posted on 1998-05-21
11
492 Views
Last Modified: 2008-02-01
as the question title says, I want to know how I can draw a string to a picturebox in XOR mode so that I can easily erase it later by drawing it in the same position again.
thanks for ur attention.

wyldsj
0
Comment
Question by:wyldsj
  • 5
  • 2
  • 2
  • +2
11 Comments
 

Expert Comment

by:rosborn051498
Comment Utility
Instead of "drawing" the string, put a label control in the picture box with the label's Backstyle property = 0 (transparent).  You can put a bitmap in the picture box, and just update the caption of the label.  The picture box will take care of XORing.
0
 

Author Comment

by:wyldsj
Comment Utility
sorry rosborn, but my label has to be drawn upon a PictureBox and I didn't seem to be able to bring the label to front by ZOrder() method(the label is hidden behind the picturebox)

wyldsj
0
 
LVL 5

Expert Comment

by:bin_huwairib
Comment Utility
wyldsj,

It seems that you place the label control outside the picture control boundaries that is why it is hidden behind the picture box.
Label control must be in the picture box in order to solve your problem, and to do select the label control and use Edit/Cut command from VB main menu then select the picture box and use Edit/Paste command. Now the label control becomes a child in the picture box.

Best regards
Bin Huwairib
0
 

Author Comment

by:wyldsj
Comment Utility
thanks, bin_huwairib. I am able to put a label control on the picture box now. The problem now is that because I have to update the label's position very frequently(it traces the mousemove message), my windows's client area has strong flickering. I think I still have to use the XOR method...Is there any windows api can do this?
0
 
LVL 2

Expert Comment

by:swilt
Comment Utility
I have not used xor mode, instead I set the ForeColor to the BackColor.  Take out the me.Show if the form is already loaded  


    Dim nFore As Long
   
    Me.Show 'use this line if in Form_Load
   
    Picture1.CurrentX = 100
    Picture1.CurrentY = 300
    Picture1.Print "Hello"
   
    nFore = Picture1.ForeColor
    Picture1.ForeColor = Picture1.BackColor
   
    MsgBox ""
   
    Picture1.CurrentX = 100
    Picture1.CurrentY = 300
    Picture1.Print "Hello"
   
    Picture1.ForeColor = nFore
   
    Picture1.CurrentX = 100
    Picture1.CurrentY = 600
    Picture1.Print "Goodbye"

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:wyldsj
Comment Utility
thanks swilt but:

  I have actually used my picturebox as a canvas to do 2-D function plot. So I have used the Line function heaviliy. Although my picturebox's background color is gray, my line color is not(red instread). When I use ur method, my drawings are corrupted by the output string...

wyldsj
0
 

Expert Comment

by:rosborn051498
Comment Utility
Does the text actually move around?  Or does it remain in the same place and just update the mouse position?  If it remains still, you can reduce flicker by only updating the part that changes.  For example, one label says "X position: " , and another label has the current X position.  Only the second label needs to be updated.

If the text moves around, all you have to do is change the Left and Top properties of the label control (I'm sure you already knew this) to get it to move around.  

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Label1.Left = X
    Label1.Top = Y

End Sub

This does create some flicker, but MouseMoves happen very rapidly.  I'm not sure that using an API would be that much better.


0
 

Author Comment

by:wyldsj
Comment Utility
hi rosborn,

  Thanks for your help all the way. Unfortunately my label needs to move around and the flickering is just too much...
0
 
LVL 4

Accepted Solution

by:
zsi earned 30 total points
Comment Utility
wyldsj,

You do not want to use a label at all.  What you need to do is create a copy of the picturebox without the label and store it in memory.  Draw your text on the picture box wherever you need it.  When it is time to erase the text, simply copy the saved copy of the picture back into the screen.

You can do this one of two ways.  

The easiest is to create a picture object and store the picture there.  Then, when you need to refresh the screen, just set the picture property of the PictureBox control to the PIcture object.

The other way of doing this is through the API.  However, for your needs, the Picture object should suffice.

When you need to update your picture (such as plotting new data), do your drawing onto  the picture object, and then copy the object into the PictureBox as described above.

Essentially, the picture object is being used as a buffer, which is the proper way to render graphics anyway.  It is always faster to do it offscreen.

Hope this helps!
zsi
0
 

Author Comment

by:wyldsj
Comment Utility
Hello zsi,

  I did as you said but it still gives me flickering(strong flickering). However, I suspect whether there is something wrong with my own settings. Last time when I tried to do some simple animation by changing the picture of a picturebox in sequence, it also gave me flickering. I would like to do whether you have encountered this problem, and whether there is any idea of what is going wrong...
  Thanks a lot for your help.

Sun Jian
0
 
LVL 4

Expert Comment

by:zsi
Comment Utility
You have to use a technizue known as "double buffering", which I described above.  If you do it correctly, drawing offscreen, you should not have any flickering.  If you would like me to look at your code, you can send it to expert@zs.com
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now