コード例 #1
0
ファイル: MainForm.cs プロジェクト: zuloloxi/Win-forms-ch10
        private void lstPhotos_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
        {
            Photograph p          = _album[e.Index];
            Rectangle  scaledRect = p.ScaleToFit(_drawRect);

            e.ItemHeight = Math.Max(scaledRect.Height, lstPhotos.Font.Height) + 2;
            e.ItemWidth  = scaledRect.Width + 2 + (int)e.Graphics.MeasureString(p.Caption, lstPhotos.Font).Width;
        }
コード例 #2
0
        private void lstPhotos_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            Graphics   g = e.Graphics;
            Photograph p = _album[e.Index];

            Rectangle scaledRect = p.ScaleToFit(_drawRect);
            Rectangle imageRect  = e.Bounds;

            imageRect.Y     += 1;
            imageRect.Height = scaledRect.Height;
            imageRect.X     += 2;
            imageRect.Width  = scaledRect.Width;

            g.DrawImage(p.Thumbnail, imageRect);
            g.DrawRectangle(Pens.Black, imageRect);

            Rectangle textRect = new Rectangle(
                imageRect.Right + 2,
                imageRect.Y + ((imageRect.Height - e.Font.Height) / 2),
                e.Bounds.Width - imageRect.Width - 4,
                e.Font.Height);

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                _textBrush.Color = SystemColors.Highlight;
                g.FillRectangle(_textBrush, textRect);
                _textBrush.Color = SystemColors.HighlightText;
            }
            else
            {
                _textBrush.Color = SystemColors.Window;
                g.FillRectangle(_textBrush, textRect);
                _textBrush.Color = SystemColors.WindowText;
            }

            // Note that DisplayMember can be null here, causing pi to be null
            PropertyInfo pi = typeof(Photograph).GetProperty(lstPhotos.DisplayMember);

            if (pi != null)
            {
                object propValue = pi.GetValue(p, null);
                g.DrawString(propValue.ToString(), e.Font, _textBrush, textRect);
            }
            else
            {
                g.DrawString(p.ToString(), e.Font, _textBrush, textRect);
            }
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: VytautasP/MyAlbumEditor
        private void lstPhotos_DrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics   g = e.Graphics;
            Photograph p = _album[e.Index];

            Rectangle scaledRect = p.ScaleToFit(_drawRect);
            Rectangle imageRect  = e.Bounds;

            imageRect.Y     += 1;
            imageRect.Height = scaledRect.Height;
            imageRect.X     += 2;
            imageRect.Width  = scaledRect.Width;

            g.DrawImage(p.ThumbNail, imageRect);
            g.DrawRectangle(Pens.Black, imageRect);

            Rectangle textRect = new Rectangle(
                imageRect.Right + 2,
                imageRect.Y + ((imageRect.Height - e.Font.Height) / 2),
                e.Bounds.Width - imageRect.Width - 4,
                e.Font.Height);

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                _textBrush.Color = SystemColors.Highlight;
                g.FillRectangle(_textBrush, textRect);
                _textBrush.Color = SystemColors.HighlightText;
            }
            else
            {
                _textBrush.Color = SystemColors.Window;
                g.FillRectangle(_textBrush, textRect);
                _textBrush.Color = SystemColors.WindowText;
            }

            string displayMember = String.IsNullOrWhiteSpace(lstPhotos.DisplayMember)
                ? "FileName"
                : lstPhotos.DisplayMember;

            PropertyInfo pi        = typeof(Photograph).GetProperty(displayMember);
            object       propValue = pi.GetValue(p, null);

            g.DrawString(propValue.ToString(), e.Font, _textBrush, textRect);
        }
コード例 #4
0
        private void pnlPhoto_Paint(object sender, PaintEventArgs e)
        {
            if (_dlgPixel != null && _pixelDlgIndex != _album.CurrentPosition)
            {
                _pixelDlgIndex = _album.CurrentPosition;
                Point p = pnlPhoto.PointToClient(Form.MousePosition);
                UpdatePixelData(p.X, p.Y);
            }

            if (_album.Count > 0)
            {
                Photograph photo = _album.CurrentPhotograph;
                Graphics   g     = e.Graphics;

                switch (_selectedMode)
                {
                default:
                case DisplayMode.ScaleToFit:
                    g.DrawImage(photo.Image, photo.ScaleToFit(pnlPhoto.DisplayRectangle));
                    break;

                case DisplayMode.StretchImage:
                    g.DrawImage(photo.Image, pnlPhoto.DisplayRectangle);
                    break;

                case DisplayMode.Normal:
                    g.DrawImage(photo.Image,
                                pnlPhoto.AutoScrollPosition.X,
                                pnlPhoto.AutoScrollPosition.Y,
                                photo.Image.Width,
                                photo.Image.Height);
                    pnlPhoto.AutoScrollMinSize = photo.Image.Size;
                    break;
                }
            }
            else
            {
                e.Graphics.Clear(SystemColors.Control);
            }
        }
コード例 #5
0
ファイル: MainForm.cs プロジェクト: zuloloxi/Win-forms-ch10
        private void lstPhotos_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            Graphics   g = e.Graphics;
            Photograph p = _album[e.Index];

            Rectangle scaledRect = p.ScaleToFit(_drawRect);
            Rectangle imageRect  = e.Bounds;

            imageRect.Y     += 1;
            imageRect.Height = scaledRect.Height;
            imageRect.X     += 2;
            imageRect.Width  = scaledRect.Width;

            g.DrawImage(p.Thumbnail, imageRect);
            g.DrawRectangle(Pens.Black, imageRect);

            Rectangle textRect = new Rectangle(
                imageRect.Right + 2,
                imageRect.Y + ((imageRect.Height - e.Font.Height) / 2),
                e.Bounds.Width - imageRect.Width - 4,
                e.Font.Height);

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                _textBrush.Color = SystemColors.Highlight;
                g.FillRectangle(_textBrush, textRect);
                _textBrush.Color = SystemColors.HighlightText;
            }
            else
            {
                _textBrush.Color = SystemColors.Window;
                g.FillRectangle(_textBrush, textRect);
                _textBrush.Color = SystemColors.WindowText;
            }

            g.DrawString(p.Caption, e.Font, _textBrush, textRect);
        }
コード例 #6
0
        protected void UpdatePixelData(int xPos, int yPos)
        {
            if (_dlgPixel == null || !_dlgPixel.Visible)
            {
                return;
            }

            Photograph photo = _album.CurrentPhotograph;

            Rectangle r = pnlPhoto.ClientRectangle;

            if (photo == null || !r.Contains(xPos, yPos))
            {
                _dlgPixel.Text     = photo == null ? " " : photo.Caption;
                _dlgPixel.XVal     = 0;
                _dlgPixel.YVal     = 0;
                _dlgPixel.RedVal   = 0;
                _dlgPixel.BlueVal  = 0;
                _dlgPixel.GreenVal = 0;
                _dlgPixel.Update();

                return;
            }

            int    x = 0, y = 0;
            Bitmap bmp = photo.Image;

            switch (_selectedMode)
            {
            case DisplayMode.Normal:
                x = xPos;
                y = yPos;
                break;

            case DisplayMode.StretchImage:
                x = xPos * bmp.Width / r.Width;
                y = yPos * bmp.Height / r.Height;
                break;

            case DisplayMode.ScaleToFit:
                Rectangle r2 = photo.ScaleToFit(r);

                if (!r2.Contains(xPos, yPos))
                {
                    return;
                }

                x = (xPos - r2.Left) * bmp.Width / r2.Width;
                y = (yPos - r2.Top) * bmp.Height / r2.Height;

                break;
            }

            Color c = bmp.GetPixel(x, y);

            _dlgPixel.XVal     = x;
            _dlgPixel.YVal     = y;
            _dlgPixel.RedVal   = c.R;
            _dlgPixel.BlueVal  = c.B;
            _dlgPixel.GreenVal = c.G;
            _dlgPixel.Update();
        }