Code Examples & Download C++Builder Using Images's Canvas to Draw Graphics in CBuilder--With Full Codes Example
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149
Tags:

This is an full codes example on how to draw graphics with an image's canvas. The following picture shows the effect of the running program. If you want to run this code example, just save the form's text to "FormMain.dfm", save the .h file codes to "graphwin.h" and save the .cpp file code to "graphwin.cpp", then create a new project and include these files to your project.

 DrawGraphics.png

//FormMain's text--FormMain

object FormMain: TFormMain
  Left = 250
  Top = 148
  Width = 463
  Height = 453
  Caption = 'Graphic Example'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Default'
  Font.Style = []
  Menu = MainMenu1
  OldCreateOrder = False
  Position = poScreenCenter
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 447
    Height = 41
    Align = alTop
    TabOrder = 0
    object LineButton: TSpeedButton
      Left = 8
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Down = True
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003333333333333333300300003333333333333333003300003333
        3333333333300333000033333333333333003333000033333333333330033333
        0000333333333333003333330000333333333330033333330000333333333300
        3333333300003333333330033333333300003333333300333333333300003333
        3330033333333333000033333300333333333333000033333003333333333333
        0000333300333333333333330000333003333333333333330000330033333333
        3333333300003003333333333333333300003033333333333333333300003333
        33333333333333330000}
      OnClick = LineButtonClick
    end
    object RectangleButton: TSpeedButton
      Left = 40
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003033333333333333330300003033
        3333333333333303000030333333333333333303000030333333333333333303
        0000303333333333333333030000303333333333333333030000303333333333
        3333330300003033333333333333330300003033333333333333330300003033
        3333333333333303000030333333333333333303000030333333333333333303
        0000303333333333333333030000303333333333333333030000303333333333
        3333330300003033333333333333330300003000000000000000000300003333
        33333333333333330000}
      OnClick = RectangleButtonClick
    end
    object EllipseButton: TSpeedButton
      Left = 72
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        4E010000424D4E01000000000000760000002800000012000000120000000100
        040000000000D800000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333000000
        3333330000003333003333330033330000003330333333333303330000003303
        3333333333303300000030333333333333330300000030333333333333330300
        0000033333333333333330000000033333333333333330000000033333333333
        3333300000000333333333333333300000000333333333333333300000000333
        3333333333333000000030333333333333330300000030333333333333330300
        0000330333333333333033000000333033333333330333000000333300333333
        003333000000333333000000333333000000}
      OnClick = EllipseButtonClick
    end
    object RoundRectButton: TSpeedButton
      Left = 104
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        4E010000424D4E01000000000000760000002800000012000000120000000100
        040000000000D800000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333300000000
        0033330000003300333333333300330000003033333333333333030000003033
        3333333333330300000003333333333333333000000003333333333333333000
        0000033333333333333330000000033333333333333330000000033333333333
        3333300000000333333333333333300000000333333333333333300000000333
        3333333333333000000003333333333333333000000003333333333333333000
        0000303333333333333303000000303333333333333303000000330033333333
        330033000000333300000000003333000000}
      OnClick = RoundRectButtonClick
    end
    object PenButton: TSpeedButton
      Left = 176
      Top = 8
      Width = 25
      Height = 25
      AllowAllUp = True
      GroupIndex = 2
      Glyph.Data = {
        66010000424D6601000000000000760000002800000015000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333339003333333333333333003334003333333333333330F00334003333
        33333333330F0003300033333333333330F00033300033333333333300000333
        3C00333333333330F0003333380033333333330F0003333330003333333330F0
        003333333700333333330F0003333333370033333330F000333333333F003333
        330F0003333333333700333330F00033333333333F0033330F00033333333333
        37003330F0003333333333333700333000033333333333333000330F00333333
        3333333337003300333333333333333337003033333333333333333337003333
        33333333333333333700}
      OnClick = PenButtonClick
    end
    object BrushButton: TSpeedButton
      Left = 208
      Top = 8
      Width = 25
      Height = 25
      AllowAllUp = True
      GroupIndex = 3
      Glyph.Data = {
        66010000424D6601000000000000760000002800000013000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333F000030030000003333333337000033333330F0003333333B00003333
        33330F000333333000003333333330F00033333700003333333330F000333330
        00003333333300F00003333A0000333333300FF0000333370000333333300F00
        00033337000033333300FF000000333E00003333330FF0000000333700003333
        330FF00000003337000033333300F0000000333F000033333330000000033337
        0000333333330FFF003333370000333333330FFF0033333F0000333333330FFF
        0033333E0000333333330FFF0033333700003333333330000333333F00003333
        33333333333333300000}
      OnClick = BrushButtonClick
    end
  end
  object PenBar: TPanel
    Left = 0
    Top = 41
    Width = 447
    Height = 41
    Align = alTop
    TabOrder = 1
    Visible = False
    object SolidPen: TSpeedButton
      Left = 8
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 4
      Down = True
      Glyph.Data = {
        B2000000424DB200000000000000760000002800000015000000050000000100
        0400000000003C00000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        333333333700333333333333333333333000300000000000000000003F003333
        33333333333333333000333333333333333333333000}
      OnClick = SetPenStyle
    end
    object DashPen: TSpeedButton
      Left = 33
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 4
      Glyph.Data = {
        B2000000424DB200000000000000760000002800000015000000050000000100
        0400000000003C00000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        333333333700333333333333333333333000300000300000030000033F003333
        33333333333333333000333333333333333333333000}
      OnClick = SetPenStyle
    end
    object DotPen: TSpeedButton
      Left = 58
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 4
      Glyph.Data = {
        B2000000424DB200000000000000760000002800000015000000050000000100
        0400000000003C00000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333333003333333333333333333333003030303030303030303033003333
        33333333333333333300333333333333333333333300}
      OnClick = SetPenStyle
    end
    object DashDotPen: TSpeedButton
      Left = 83
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 4
      Glyph.Data = {
        B2000000424DB200000000000000760000002800000015000000050000000100
        0400000000003C00000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333330003333333333333333333330003000003030000030300030003333
        33333333333333333F00333333333333333333333700}
      OnClick = SetPenStyle
    end
    object DashDotDotPen: TSpeedButton
      Left = 108
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 4
      Glyph.Data = {
        B2000000424DB200000000000000760000002800000015000000050000000100
        0400000000003C00000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333333003333333333333333333333003000003030300000303033003333
        33333333333333333300333333333333333333333300}
      OnClick = SetPenStyle
    end
    object ClearPen: TSpeedButton
      Left = 133
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 4
      Glyph.Data = {
        B2000000424DB200000000000000760000002800000015000000050000000100
        0400000000003C00000000000000000000001000000000000000000000000000
        80000080000000808000800000008000800080800000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333333003333333333333333333333003FFFFFFFFFFFFFFFFFFF33003333
        33333333333333333300333333333333333333333300}
      OnClick = SetPenStyle
    end
    object PenColor: TSpeedButton
      Left = 174
      Top = 8
      Width = 25
      Height = 25
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000010000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00999BBBBFAAAA
        4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999B
        BBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999BBBBFAAAACCCCFFFF
        0000999BBBBFAAAA4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA
        4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999B
        BBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999BBBBFAAAACCCCFFFF
        0000999BBBBFAAAA4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA
        4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999B
        BBFFAAAFFCCCFFFF0000}
      OnClick = PenColorClick
    end
    object PenSize: TEdit
      Left = 280
      Top = 12
      Width = 29
      Height = 21
      TabOrder = 1
      Text = '1'
      OnChange = PenSizeChange
    end
    object PenWidth: TUpDown
      Left = 309
      Top = 12
      Width = 12
      Height = 21
      Associate = PenSize
      ArrowKeys = False
      Min = 0
      Position = 1
      TabOrder = 0
      Wrap = False
    end
  end
  object BrushBar: TPanel
    Left = 0
    Top = 82
    Width = 447
    Height = 41
    Align = alTop
    TabOrder = 2
    Visible = False
    object SolidBrush: TSpeedButton
      Left = 8
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Down = True
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003000000000000000000300003000
        0000000000000003000030000000000000000003000030000000000000000003
        0000300000000000000000030000300000000000000000030000300000000000
        0000000300003000000000000000000300003000000000000000000300003000
        0000000000000003000030000000000000000003000030000000000000000003
        0000300000000000000000030000300000000000000000030000300000000000
        0000000300003000000000000000000300003000000000000000000300003333
        33333333333333330000}
      OnClick = SetBrushStyle
    end
    object ClearBrush: TSpeedButton
      Left = 40
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003033333333333333330300003033
        3333333333333303000030333333333333333303000030333333333333333303
        0000303333333333333333030000303333333333333333030000303333333333
        3333330300003033333333333333330300003033333333333333330300003033
        3333333333333303000030333333333333333303000030333333333333333303
        0000303333333333333333030000303333333333333333030000303333333333
        3333330300003033333333333333330300003000000000000000000300003333
        33333333333333330000}
      OnClick = SetBrushStyle
    end
    object HorizontalBrush: TSpeedButton
      Left = 72
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003033033033033033030300003033
        0330330330330303000030330330330330330303000030330330330330330303
        0000303303303303303303030000303303303303303303030000303303303303
        3033030300003033033033033033030300003033033033033033030300003033
        0330330330330303000030330330330330330303000030330330330330330303
        0000303303303303303303030000303303303303303303030000303303303303
        3033030300003033033033033033030300003000000000000000000300003333
        33333333333333330000}
      OnClick = SetBrushStyle
    end
    object VerticalBrush: TSpeedButton
      Left = 104
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003033333333333333330300003000
        0000000000000003000030333333333333333303000030333333333333333303
        0000300000000000000000030000303333333333333333030000303333333333
        3333330300003000000000000000000300003033333333333333330300003033
        3333333333333303000030000000000000000003000030333333333333333303
        0000303333333333333333030000300000000000000000030000303333333333
        3333330300003033333333333333330300003000000000000000000300003333
        33333333333333330000}
      OnClick = SetBrushStyle
    end
    object FDiagonalBrush: TSpeedButton
      Left = 136
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003033303330333033300300003033
        0333033303330303000030303330333033303303000030033303330333033303
        0000303330333033303330030000303303330333033303030000303033303330
        3330330300003003330333033303330300003033303330333033300300003033
        0333033303330303000030303330333033303303000030033303330333033303
        0000303330333033303330030000303303330333033303030000303033303330
        3330330300003003330333033303330300003000000000000000000300003333
        33333333333333330000}
      OnClick = SetBrushStyle
    end
    object BDiagonalBrush: TSpeedButton
      Left = 168
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003003330333033303330300003030
        3330333033303303000030330333033303330303000030333033303330333003
        0000300333033303330333030000303033303330333033030000303303330333
        0333030300003033303330333033300300003003330333033303330300003030
        3330333033303303000030330333033303330303000030333033303330333003
        0000300333033303330333030000303033303330333033030000303303330333
        0333030300003033303330333033300300003000000000000000000300003333
        33333333333333330000}
      OnClick = SetBrushStyle
    end
    object CrossBrush: TSpeedButton
      Left = 200
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003033033033033033030300003000
        0000000000000003000030330330330330330303000030330330330330330303
        0000300000000000000000030000303303303303303303030000303303303303
        3033030300003000000000000000000300003033033033033033030300003033
        0330330330330303000030000000000000000003000030330330330330330303
        0000303303303303303303030000300000000000000000030000303303303303
        3033030300003033033033033033030300003000000000000000000300003333
        33333333333333330000}
      OnClick = SetBrushStyle
    end
    object DiagCrossBrush: TSpeedButton
      Left = 232
      Top = 8
      Width = 25
      Height = 25
      GroupIndex = 1
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000000000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00333333333333
        3333333300003000000000000000000300003033303330333033300300003003
        0303030303030303000030303330333033303303000030030303030303030303
        0000303330333033303330030000300303030303030303030000303033303330
        3330330300003003030303030303030300003033303330333033300300003003
        0303030303030303000030303330333033303303000030030303030303030303
        0000303330333033303330030000300303030303030303030000303033303330
        3330330300003003030303030303030300003000000000000000000300003333
        33333333333333330000}
      OnClick = SetBrushStyle
    end
    object BrushColor: TSpeedButton
      Left = 279
      Top = 8
      Width = 25
      Height = 25
      Glyph.Data = {
        66010000424D6601000000000000760000002800000014000000140000000100
        040000000000F000000000000000000000001000000010000000000000000000
        BF0000BF000000BFBF00BF000000BF00BF00BFBF0000C0C0C000808080000000
        FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00999BBBBFAAAA
        4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999B
        BBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999BBBBFAAAACCCCFFFF
        0000999BBBBFAAAA4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA
        4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999B
        BBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999BBBBFAAAACCCCFFFF
        0000999BBBBFAAAA4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA
        4CCCFFFF0000999BBBBFAAAACCCCFFFF0000999BBBBFAAAA4CCCFFFF0000999B
        BBFFAAAFFCCCFFFF0000}
      OnClick = BrushColorClick
    end
  end
  object ScrollBox1: TScrollBox
    Left = 0
    Top = 123
    Width = 447
    Height = 254
    Align = alClient
    TabOrder = 4
    object Image: TImage
      Left = 0
      Top = 0
      Width = 443
      Height = 250
      Align = alClient
      AutoSize = True
      OnMouseDown = FormMouseDown
      OnMouseMove = FormMouseMove
      OnMouseUp = FormMouseUp
    end
  end
  object StatusBar1: TStatusBar
    Left = 0
    Top = 377
    Width = 447
    Height = 18
    Panels = <
      item
        Width = 150
      end
      item
        Width = 50
      end>
    SimplePanel = False
  end
  object MainMenu1: TMainMenu
    Left = 297
    Top = 148
    object File1: TMenuItem
      Caption = '&File'
      object New1: TMenuItem
        Caption = '&New'
        OnClick = New1Click
      end
      object Open1: TMenuItem
        Caption = '&Open...'
        OnClick = Open1Click
      end
      object Save1: TMenuItem
        Caption = '&Save'
        OnClick = Save1Click
      end
      object Saveas1: TMenuItem
        Caption = 'Save &as...'
        OnClick = SaveAs1Click
      end
      object Print1: TMenuItem
        Caption = '&Print'
        OnClick = Print1Click
      end
      object N1: TMenuItem
        Caption = '-'
      end
      object Exit1: TMenuItem
        Caption = 'E&xit'
        OnClick = Exit1Click
      end
    end
    object Edit1: TMenuItem
      Caption = '&Edit'
      object Cut1: TMenuItem
        Caption = 'Cu&t'
        ShortCut = 16472
        OnClick = Cut1Click
      end
      object Copy1: TMenuItem
        Caption = '&Copy'
        ShortCut = 16451
        OnClick = Copy1Click
      end
      object Paste1: TMenuItem
        Caption = '&Paste'
        ShortCut = 16470
        OnClick = Paste1Click
      end
    end
  end
  object OpenDialog1: TOpenDialog
    DefaultExt = 'bmp'
    Filter = 'Bitmap files (*.bmp)|*.bmp|All files|*.*'
    Left = 297
    Top = 188
  end
  object ColorDialog1: TColorDialog
    Ctl3D = True
    Left = 345
    Top = 148
  end
  object SaveDialog1: TSaveDialog
    DefaultExt = 'bmp'
    Filter = 'Bitmap files (*.bmp)|*.bmp|All files|*.*'
    Left = 345
    Top = 188
  end
end

//.h files codes--graphwin.h

#ifndef graphwinH
#define graphwinH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------

enum TDrawingTool {dtLine, dtRectangle, dtEllipse, dtRoundRect};

class TFormMain : public TForm
{
__published:
    TPanel *Panel1;
    TPanel *PenBar;
    TPanel *BrushBar;
    TScrollBox *ScrollBox1;
    TImage *Image;
    TMainMenu *MainMenu1;
    TMenuItem *File1;
    TMenuItem *New1;
    TMenuItem *Open1;
    TMenuItem *Save1;
    TMenuItem *Saveas1;
    TMenuItem *Print1;
    TMenuItem *N1;
    TMenuItem *Exit1;
    TMenuItem *Edit1;
    TMenuItem *Cut1;
    TMenuItem *Copy1;
    TMenuItem *Paste1;
    TOpenDialog *OpenDialog1;
    TColorDialog *ColorDialog1;
    TSaveDialog *SaveDialog1;
    TStatusBar *StatusBar1;
    TSpeedButton *LineButton;
    TSpeedButton *RectangleButton;
    TSpeedButton *EllipseButton;
    TSpeedButton *RoundRectButton;
    TSpeedButton *PenButton;
    TSpeedButton *BrushButton;
    TSpeedButton *SolidPen;
    TSpeedButton *DashPen;
    TSpeedButton *DotPen;
    TSpeedButton *DashDotPen;
    TSpeedButton *DashDotDotPen;
    TSpeedButton *ClearPen;
    TSpeedButton *PenColor;
    TEdit *PenSize;
    TUpDown *PenWidth;
    TSpeedButton *SolidBrush;
    TSpeedButton *ClearBrush;
    TSpeedButton *HorizontalBrush;
    TSpeedButton *VerticalBrush;
    TSpeedButton *FDiagonalBrush;
    TSpeedButton *BDiagonalBrush;
    TSpeedButton *CrossBrush;
    TSpeedButton *DiagCrossBrush;
    TSpeedButton *BrushColor;
    void __fastcall RectangleButtonClick(TObject *Sender);
    void __fastcall EllipseButtonClick(TObject *Sender);
    void __fastcall RoundRectButtonClick(TObject *Sender);
    void __fastcall PenButtonClick(TObject *Sender);
    void __fastcall BrushButtonClick(TObject *Sender);
    void __fastcall SetPenStyle(TObject *Sender);
    void __fastcall PenColorClick(TObject *Sender);
    void __fastcall PenSizeChange(TObject *Sender);
    void __fastcall SetBrushStyle(TObject *Sender);
    void __fastcall BrushColorClick(TObject *Sender);
    void __fastcall FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y);
    void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y);
    void __fastcall FormMouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y);
    void __fastcall New1Click(TObject *Sender);
    void __fastcall Open1Click(TObject *Sender);
    void __fastcall Save1Click(TObject *Sender);
    void __fastcall SaveAs1Click(TObject *Sender);
    void __fastcall Exit1Click(TObject *Sender);
    void __fastcall Cut1Click(TObject *Sender);
    void __fastcall Copy1Click(TObject *Sender);
    void __fastcall Paste1Click(TObject *Sender);
   
    void __fastcall LineButtonClick(TObject *Sender);
    void __fastcall Print1Click(TObject *Sender);
private:        // private user declarations
public:         // public user declarations
        bool Drawing;
        TPoint Origin, MovePt;
        TDrawingTool DrawingTool;
        AnsiString CurrentFile;

        void __fastcall DrawShape(TPoint TopLeft, TPoint BottomRight, TPenMode AMode);
        virtual __fastcall TFormMain(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern TFormMain *FormMain;
//---------------------------------------------------------------------------
#endif

//.cpp files codes--graphwin.cpp

#include
#pragma hdrstop
#include

#include "graphwin.h"
#include "bmpdlg.h"
#include
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TFormMain* FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
  : TForm(Owner)
{
 Image->Canvas->MoveTo(0,0);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::LineButtonClick(TObject *Sender)
{
  DrawingTool = dtLine;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::RectangleButtonClick(TObject *Sender)
{
  DrawingTool = dtRectangle;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::EllipseButtonClick(TObject *Sender)
{
  DrawingTool = dtEllipse;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::RoundRectButtonClick(TObject *Sender)
{
  DrawingTool = dtRoundRect;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::PenButtonClick(TObject *Sender)
{
  PenBar->Visible = PenButton->Down;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::BrushButtonClick(TObject *Sender)
{
  BrushBar->Visible = BrushButton->Down;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::SetPenStyle(TObject *Sender)
{
    if (Sender == SolidPen){
       Image->Canvas->Pen->Style = psSolid;
    }
    else if (Sender == DashPen){
            Image->Canvas->Pen->Style = psDash;
    }
    else if (Sender == DotPen){
         Image->Canvas->Pen->Style = psDot;
    }
    else if (Sender == DashDotPen){
         Image->Canvas->Pen->Style = psDashDot;
    }
    else if (Sender == DashDotDotPen){
         Image->Canvas->Pen->Style = psDashDotDot;
    }
    else if (Sender == ClearPen){
         Image->Canvas->Pen->Style = psClear;
    }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::PenColorClick(TObject *Sender)
{
  ColorDialog1->Color = Image->Canvas->Pen->Color;
  if (ColorDialog1->Execute()){
    Image->Canvas->Pen->Color = ColorDialog1->Color;
  }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::PenSizeChange(TObject *Sender)
{
  Image->Canvas->Pen->Width = PenWidth->Position;
}
//---------------------------------------------------------------------
void __fastcall TFormMain::SetBrushStyle(TObject *Sender)
{
    if (Sender == SolidBrush){
       Image->Canvas->Brush->Style = bsSolid;
    }
    else if (Sender == ClearBrush){
         Image->Canvas->Brush->Style = bsClear;
    }
    else if (Sender == HorizontalBrush){
         Image->Canvas->Brush->Style = bsHorizontal;
    }
    else if (Sender == VerticalBrush){
         Image->Canvas->Brush->Style = bsVertical;
    }
    else if (Sender == FDiagonalBrush){
         Image->Canvas->Brush->Style = bsFDiagonal;
    }
    else if (Sender == BDiagonalBrush){
         Image->Canvas->Brush->Style = bsBDiagonal;
    }
    else if (Sender == CrossBrush){
         Image->Canvas->Brush->Style = bsCross;
    }
    else if (Sender == DiagCrossBrush){
         Image->Canvas->Brush->Style = bsDiagCross;
    }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::BrushColorClick(TObject *Sender)
{
  ColorDialog1->Color = Image->Canvas->Brush->Color;
  if (ColorDialog1->Execute()){
    Image->Canvas->Brush->Color = ColorDialog1->Color;
  }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  Drawing = True;
  Image->Canvas->MoveTo(X, Y);
  Origin = Point(X, Y);
  MovePt = Origin;

  TVarRec tempvar[2] = {X, Y};
  StatusBar1->Panels->Items[0]->Text = Format("Origin: (%d, %d)", tempvar, 2);
}
//---------------------------------------------------------------------
void __fastcall TFormMain::FormMouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
  if (Drawing){
    DrawShape(Origin, MovePt, pmNotXor);
    MovePt = Point(X, Y);
    DrawShape(Origin, MovePt, pmNotXor);
  }

  TVarRec tempvar[2] = {X, Y};
  StatusBar1->Panels->Items[1]->Text = Format("Current: (%d, %d)", tempvar, 2);
}
//---------------------------------------------------------------------
void __fastcall TFormMain::FormMouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  if (Drawing){
    DrawShape(Origin, Point(X, Y), pmCopy);
    Drawing = False;
  }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::New1Click(TObject *Sender)
{
     Graphics::TBitmap *Bitmap;

     NewBMPForm->ActiveControl = NewBMPForm->WidthEdit;
     NewBMPForm->WidthEdit->Text = IntToStr(Image->Picture->Graphic->Width);
     NewBMPForm->HeightEdit->Text = IntToStr(Image->Picture->Graphic->Height);
//     if (ShowModal() != idCancel){
     if (NewBMPForm->ShowModal() != IDCANCEL){
        Bitmap = new Graphics::TBitmap();
//        Bitmap->Create();
        Bitmap->Width = StrToInt(NewBMPForm->WidthEdit->Text);
        Bitmap->Height = StrToInt(NewBMPForm->HeightEdit->Text);
        Image->Picture->Graphic = Bitmap;
        CurrentFile = EmptyStr;
     }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::Open1Click(TObject *Sender)
{
  if (OpenDialog1->Execute()){
    CurrentFile = OpenDialog1->FileName;
    Image->Picture->LoadFromFile(CurrentFile);
  }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::Save1Click(TObject *Sender)
{
  if (CurrentFile != EmptyStr){
    Image->Picture->SaveToFile(CurrentFile);
  }
  else{
   SaveAs1Click(Sender);
  }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::SaveAs1Click(TObject *Sender)
{
  if (SaveDialog1->Execute()){
    CurrentFile = SaveDialog1->FileName;
    Save1Click(Sender);
  }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::Exit1Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------
void __fastcall TFormMain::Cut1Click(TObject *Sender)
{
 TRect ARect;

 Copy1Click(Sender);

 Image->Canvas->CopyMode = cmWhiteness;
 ARect = Rect(0, 0, Image->Width, Image->Height);
 Image->Canvas->CopyRect(ARect, Image->Canvas, ARect);
 Image->Canvas->CopyMode = cmSrcCopy;
}

//---------------------------------------------------------------------
void __fastcall TFormMain::Copy1Click(TObject *Sender)
{
  Clipboard()->Assign(Image->Picture);
}

//---------------------------------------------------------------------
void __fastcall TFormMain::Paste1Click(TObject *Sender)
{

  Graphics::TBitmap *Bitmap;

  if (Clipboard()->HasFormat(CF_BITMAP)){
    Bitmap = new Graphics::TBitmap();
    try{
      Bitmap->Assign(Clipboard());
      Image->Canvas->Draw(0, 0, Bitmap);
      delete Bitmap;
    }
    catch(...){
      delete Bitmap;
    }
  }
}

//---------------------------------------------------------------------
void __fastcall TFormMain::DrawShape(TPoint TopLeft, TPoint BottomRight, TPenMode AMode){
    Image->Canvas->Pen->Mode = AMode;

    switch (DrawingTool){
           case dtLine : {
                Image->Canvas->MoveTo(TopLeft.x, TopLeft.y);
                Image->Canvas->LineTo(BottomRight.x, BottomRight.y);
                break;
           }

           case dtRectangle : {
                Image->Canvas->Rectangle(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
                break;
           }

           case dtEllipse : {
                Image->Canvas->Ellipse(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
                break;
           }

           case dtRoundRect : {
                Image->Canvas->RoundRect(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y, (TopLeft.x - BottomRight.x)/2, (TopLeft.y - BottomRight.y)/2);
                break;
           }
    }
}
//---------------------------------------------------------------------
void __fastcall TFormMain::Print1Click(TObject *Sender)
{
    unsigned int BitmapInfoSize, BitmapImageSize;
    long DIBWidth, DIBHeight;
    PChar BitmapImage;
    Windows::PBitmapInfo BitmapInfo;
    Graphics::TBitmap *Bitmap;

    Printer()->BeginDoc();
    Bitmap = new Graphics::TBitmap();
    Bitmap->Assign(Image->Picture);
    GetDIBSizes(Bitmap->Handle, BitmapInfoSize, BitmapImageSize);
    BitmapInfo  = (PBitmapInfo) new char[BitmapInfoSize];
    BitmapImage = (PChar) new char [BitmapImageSize];
    GetDIB(Bitmap->Handle, 0, BitmapInfo, BitmapImage);
    DIBWidth  = BitmapInfo->bmiHeader.biWidth;
    DIBHeight = BitmapInfo->bmiHeader.biHeight;
    StretchDIBits(Printer()->Canvas->Handle,
                0, 0, DIBWidth, DIBHeight,
                0, 0, DIBWidth, DIBHeight,
                BitmapImage, BitmapInfo,
                DIB_RGB_COLORS, SRCCOPY);
    delete [] BitmapImage;
    delete [] BitmapInfo;
    delete Bitmap;

    Printer()->EndDoc();
}
//---------------------------------------------------------------------


Code Examples & Download - C++Builder