CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: How to Draw Rectangles on Screen

    Strange... One of 3 things is probably happening: either your location/size input parameters aren't right, or there is some interference with the regular windows drawing routine, where your rectangles are overwritten when the screen redraws itself (or portions of itself), or your draw method never get's called.

    I attached a small sample application that draws semitransparent rectangles on random screen locations. Note that when you hover your mouse over some parts of the window (like minimize/maximize buttons), a part of the screen get's redrawn. You can even induce the invalidation of the entire screen area.

    The gist of the sample is simple, the code below:
    Code:
    private void m_btnDraw_Click(object sender, EventArgs e)
            {
                var g = Graphics.FromHwnd(IntPtr.Zero);
    
                int screenWidth = Screen.GetWorkingArea(this).Width;
                int screenHeight = Screen.GetWorkingArea(this).Height;
                int boxSize = 100;
    
                Brush brsh = new SolidBrush(Color.FromArgb(128, m_rnd.Next(256), m_rnd.Next(256), m_rnd.Next(256)));
    
                g.FillRectangle(brsh,
                    m_rnd.Next(screenWidth - boxSize),
                    m_rnd.Next(screenHeight - boxSize),
                    boxSize,
                    boxSize
                    );
    
                brsh.Dispose();
                g.Dispose();
            }
    Attached Files Attached Files

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

Featured