Author Topic: SUB for printing colored strings easy  (Read 4034 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
SUB for printing colored strings easy
« on: August 05, 2021, 08:38:21 am »
Hi. I think, this code say you all...

Code: QB64: [Select]
  1. 'Program show new utility for direct text coloring. Use in 32 bit mode.
  2.  
  3. SCREEN _NEWIMAGE(1024, 768, 32)
  4.  
  5. 'old QB64 use for colored printing to screen:
  6. COLOR _RGB32(0, 255, 0): PRINT "This ";: COLOR _RGB32(255, 0, 0): PRINT "is ";: COLOR _RGB32(0, 0, 255): PRINT "colored ";: COLOR _RGB32(255, 255, 0): PRINT "text."
  7. 'next possibility using $COLOR:32
  8. COLOR LightGreen: PRINT "This ";: COLOR Red: PRINT "is ";: COLOR Blue: PRINT "colored ";: COLOR Yellow: PRINT "text."
  9.  
  10. 'new possibility:
  11.  
  12. COLTEXT "*LightGreen*This *Red*Is *Blue*colored *Yellow*text."
  13.  
  14. 'OR
  15.  
  16. COLTEXT "*0,255,0*This *255,0,0*is *0,0,255*colored *255,255,0*text."
  17.  
  18. 'OR combinated (if is $Color:32 used):
  19.  
  20. COLTEXT "*LightGreen*This *255,0,0*is *0,0,255*colored *YELLOW*text."
  21.  
  22.  
  23. 'Also one - number color is supported. So for full white:
  24. COLTEXT "*255*This is writed in _RGB32 (255, 255, 255)"
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. SUB COLTEXT (S$)
  39.     DC~& = _DEFAULTCOLOR
  40.     TextLenght = LEN(S$)
  41.     FOR all = 1 TO TextLenght
  42.         command = ASC(S$, all)
  43.  
  44.         IF command = ASC("*") THEN
  45.             IF LEN(Text$) THEN PRINT Text$;
  46.  
  47.             Text$ = ""
  48.             value$ = ""
  49.             cmd$ = ""
  50.             c~& = 0
  51.  
  52.             DO UNTIL cmd$ = "*"
  53.                 all = all + 1
  54.                 cmd$ = MID$(S$, all, 1)
  55.                 IF cmd$ <> "*" THEN value$ = value$ + cmd$
  56.                 IF all >= TextLenght THEN EXIT DO
  57.             LOOP
  58.  
  59.  
  60.             IF INSTR(value$, ",") = 0 THEN
  61.  
  62.  
  63.                 IF LEN(value$) AND VAL(value$) = 0 THEN '$COLOR32 SUPPORT
  64.                     SELECT CASE LCASE$(_TRIM$(value$))
  65.                         CASE "aliceblue": c~& = 4293982463
  66.                         CASE "almond": c~& = 4293910221
  67.                         CASE "antiquebrass": c~& = 4291663221
  68.                         CASE "antiquewhite": c~& = 4294634455
  69.                         CASE "apricot": c~& = 4294826421
  70.                         CASE "aqua": c~& = 4278255615
  71.                         CASE "aquamarine": c~& = 4286578644
  72.                         CASE "asparagus": c~& = 4287080811
  73.                         CASE "atomictangerine": c~& = 4294943860
  74.                         CASE "azure": c~& = 4293984255
  75.                         CASE "bananamania": c~& = 4294633397
  76.                         CASE "beaver": c~& = 4288643440
  77.                         CASE "beige": c~& = 4294309340
  78.                         CASE "bisque": c~& = 4294960324
  79.                         CASE "bittersweet": c~& = 4294802542
  80.                         CASE "black": c~& = 4278190080
  81.                         CASE "blanchedalmond": c~& = 4294962125
  82.                         CASE "blizzardblue": c~& = 4289521134
  83.                         CASE "blue": c~& = 4278190335
  84.                         CASE "bluebell": c~& = 4288848592
  85.                         CASE "bluegray": c~& = 4284914124
  86.                         CASE "bluegreen": c~& = 4279081146
  87.                         CASE "blueviolet": c~& = 4287245282
  88.                         CASE "blush": c~& = 4292763011
  89.                         CASE "brickred": c~& = 4291510612
  90.                         CASE "brown": c~& = 4289014314
  91.                         CASE "burlywood": c~& = 4292786311
  92.                         CASE "burntorange": c~& = 4294934345
  93.                         CASE "burntsienna": c~& = 4293557853
  94.                         CASE "cadetblue": c~& = 4284456608
  95.                         CASE "canary": c~& = 4294967193
  96.                         CASE "caribbeangreen": c~& = 4280079266
  97.                         CASE "carnationpink": c~& = 4294945484
  98.                         CASE "cerise": c~& = 4292691090
  99.                         CASE "cerulean": c~& = 4280134870
  100.                         CASE "chartreuse": c~& = 4286578432
  101.                         CASE "chestnut": c~& = 4290534744
  102.                         CASE "chocolate": c~& = 4291979550
  103.                         CASE "copper": c~& = 4292711541
  104.                         CASE "coral": c~& = 4294934352
  105.                         CASE "cornflower": c~& = 4288335595
  106.                         CASE "cornflowerblue": c~& = 4284782061
  107.                         CASE "cornsilk": c~& = 4294965468
  108.                         CASE "cottoncandy": c~& = 4294950105
  109.                         CASE "crayolaaquamarine": c~& = 4286110690
  110.                         CASE "crayolablue": c~& = 4280251902
  111.                         CASE "crayolablueviolet": c~& = 4285753021
  112.                         CASE "crayolabrown": c~& = 4290013005
  113.                         CASE "crayolacadetblue": c~& = 4289771462
  114.                         CASE "crayolaforestgreen": c~& = 4285378177
  115.                         CASE "crayolagold": c~& = 4293379735
  116.                         CASE "crayolagoldenrod": c~& = 4294760821
  117.                         CASE "crayolagray": c~& = 4287992204
  118.                         CASE "crayolagreen": c~& = 4280069240
  119.                         CASE "crayolagreenyellow": c~& = 4293978257
  120.                         CASE "crayolaindigo": c~& = 4284315339
  121.                         CASE "crayolalavender": c~& = 4294751445
  122.                         CASE "crayolamagenta": c~& = 4294337711
  123.                         CASE "crayolamaroon": c~& = 4291311706
  124.                         CASE "crayolamidnightblue": c~& = 4279912566
  125.                         CASE "crayolaorange": c~& = 4294931768
  126.                         CASE "crayolaorangered": c~& = 4294912811
  127.                         CASE "crayolaorchid": c~& = 4293306583
  128.                         CASE "crayolaplum": c~& = 4287513989
  129.                         CASE "crayolared": c~& = 4293795917
  130.                         CASE "crayolasalmon": c~& = 4294941610
  131.                         CASE "crayolaseagreen": c~& = 4288668351
  132.                         CASE "crayolasilver": c~& = 4291675586
  133.                         CASE "crayolaskyblue": c~& = 4286634731
  134.                         CASE "crayolaspringgreen": c~& = 4293716670
  135.                         CASE "crayolatann": c~& = 4294616940
  136.                         CASE "crayolathistle": c~& = 4293642207
  137.                         CASE "crayolaviolet": c~& = 4287786670
  138.                         CASE "crayolayellow": c~& = 4294764675
  139.                         CASE "crayolayellowgreen": c~& = 4291158916
  140.                         CASE "crimson": c~& = 4292613180
  141.                         CASE "cyan": c~& = 4278255615
  142.                         CASE "dandelion": c~& = 4294826861
  143.                         CASE "darkblue": c~& = 4278190219
  144.                         CASE "darkcyan": c~& = 4278225803
  145.                         CASE "darkgoldenrod": c~& = 4290283019
  146.                         CASE "darkgray": c~& = 4289309097
  147.                         CASE "darkgreen": c~& = 4278215680
  148.                         CASE "darkkhaki": c~& = 4290623339
  149.                         CASE "darkmagenta": c~& = 4287299723
  150.                         CASE "darkolivegreen": c~& = 4283788079
  151.                         CASE "darkorange": c~& = 4294937600
  152.                         CASE "darkorchid": c~& = 4288230092
  153.                         CASE "darkred": c~& = 4287299584
  154.                         CASE "darksalmon": c~& = 4293498490
  155.                         CASE "darkseagreen": c~& = 4287609999
  156.                         CASE "darkslateblue": c~& = 4282924427
  157.                         CASE "darkslategray": c~& = 4281290575
  158.                         CASE "darkturquoise": c~& = 4278243025
  159.                         CASE "darkviolet": c~& = 4287889619
  160.                         CASE "deeppink": c~& = 4294907027
  161.                         CASE "deepskyblue": c~& = 4278239231
  162.                         CASE "denim": c~& = 4281035972
  163.                         CASE "desertsand": c~& = 4293905848
  164.                         CASE "dimgray": c~& = 4285098345
  165.                         CASE "dodgerblue": c~& = 4280193279
  166.                         CASE "eggplant": c~& = 4285419872
  167.                         CASE "electriclime": c~& = 4291755805
  168.                         CASE "fern": c~& = 4285643896
  169.                         CASE "firebrick": c~& = 4289864226
  170.                         CASE "floralwhite": c~& = 4294966000
  171.                         CASE "forestgreen": c~& = 4280453922
  172.                         CASE "fuchsia": c~& = 4290995397
  173.                         CASE "fuzzywuzzy": c~& = 4291585638
  174.                         CASE "gainsboro": c~& = 4292664540
  175.                         CASE "ghostwhite": c~& = 4294506751
  176.                         CASE "gold": c~& = 4294956800
  177.                         CASE "goldenrod": c~& = 4292519200
  178.                         CASE "grannysmithapple": c~& = 4289258656
  179.                         CASE "gray": c~& = 4286611584
  180.                         CASE "green": c~& = 4278222848
  181.                         CASE "greenblue": c~& = 4279329972
  182.                         CASE "greenyellow": c~& = 4289593135
  183.                         CASE "honeydew": c~& = 4293984240
  184.                         CASE "hotmagenta": c~& = 4294909390
  185.                         CASE "hotpink": c~& = 4294928820
  186.                         CASE "inchworm": c~& = 4289915997
  187.                         CASE "indianred": c~& = 4291648604
  188.                         CASE "indigo": c~& = 4283105410
  189.                         CASE "ivory": c~& = 4294967280
  190.                         CASE "jazzberryjam": c~& = 4291442535
  191.                         CASE "junglegreen": c~& = 4282101903
  192.                         CASE "khaki": c~& = 4293977740
  193.                         CASE "laserlemon": c~& = 4294901282
  194.                         CASE "lavender": c~& = 4293322490
  195.                         CASE "lavenderblush": c~& = 4294963445
  196.                         CASE "lawngreen": c~& = 4286381056
  197.                         CASE "lemonchiffon": c~& = 4294965965
  198.                         CASE "lemonyellow": c~& = 4294964303
  199.                         CASE "lightblue": c~& = 4289583334
  200.                         CASE "lightcoral": c~& = 4293951616
  201.                         CASE "lightcyan": c~& = 4292935679
  202.                         CASE "lightgoldenrodyellow": c~& = 4294638290
  203.                         CASE "lightgray": c~& = 4292072403
  204.                         CASE "lightgreen": c~& = 4287688336
  205.                         CASE "lightpink": c~& = 4294948545
  206.                         CASE "lightsalmon": c~& = 4294942842
  207.                         CASE "lightseagreen": c~& = 4280332970
  208.                         CASE "lightskyblue": c~& = 4287090426
  209.                         CASE "lightslategray": c~& = 4286023833
  210.                         CASE "lightsteelblue": c~& = 4289774814
  211.                         CASE "lightyellow": c~& = 4294967264
  212.                         CASE "lime": c~& = 4278255360
  213.                         CASE "limegreen": c~& = 4281519410
  214.                         CASE "linen": c~& = 4294635750
  215.                         CASE "macaroniandcheese": c~& = 4294950280
  216.                         CASE "magenta": c~& = 4294902015
  217.                         CASE "magicmint": c~& = 4289392849
  218.                         CASE "mahogany": c~& = 4291643980
  219.                         CASE "maize": c~& = 4293775772
  220.                         CASE "manatee": c~& = 4288125610
  221.                         CASE "mangotango": c~& = 4294935107
  222.                         CASE "maroon": c~& = 4286578688
  223.                         CASE "mauvelous": c~& = 4293892266
  224.                         CASE "mediumaquamarine": c~& = 4284927402
  225.                         CASE "mediumblue": c~& = 4278190285
  226.                         CASE "mediumorchid": c~& = 4290401747
  227.                         CASE "mediumpurple": c~& = 4287852763
  228.                         CASE "mediumseagreen": c~& = 4282168177
  229.                         CASE "mediumslateblue": c~& = 4286277870
  230.                         CASE "mediumspringgreen": c~& = 4278254234
  231.                         CASE "mediumturquoise": c~& = 4282962380
  232.                         CASE "mediumvioletred": c~& = 4291237253
  233.                         CASE "melon": c~& = 4294818996
  234.                         CASE "midnightblue": c~& = 4279834992
  235.                         CASE "mintcream": c~& = 4294311930
  236.                         CASE "mistyrose": c~& = 4294960353
  237.                         CASE "moccasin": c~& = 4294960309
  238.                         CASE "mountainmeadow": c~& = 4281383567
  239.                         CASE "mulberry": c~& = 4291120012
  240.                         CASE "navajowhite": c~& = 4294958765
  241.                         CASE "navy": c~& = 4278190208
  242.                         CASE "navyblue": c~& = 4279858386
  243.                         CASE "neoncarrot": c~& = 4294943555
  244.                         CASE "oldlace": c~& = 4294833638
  245.                         CASE "olive": c~& = 4286611456
  246.                         CASE "olivedrab": c~& = 4285238819
  247.                         CASE "olivegreen": c~& = 4290426988
  248.                         CASE "orange": c~& = 4294944000
  249.                         CASE "orangered": c~& = 4294919424
  250.                         CASE "orangeyellow": c~& = 4294497640
  251.                         CASE "orchid": c~& = 4292505814
  252.                         CASE "outerspace": c~& = 4282468940
  253.                         CASE "outrageousorange": c~& = 4294929994
  254.                         CASE "pacificblue": c~& = 4280068553
  255.                         CASE "palegoldenrod": c~& = 4293847210
  256.                         CASE "palegreen": c~& = 4288215960
  257.                         CASE "paleturquoise": c~& = 4289720046
  258.                         CASE "palevioletred": c~& = 4292571283
  259.                         CASE "papayawhip": c~& = 4294963157
  260.                         CASE "peach": c~& = 4294954923
  261.                         CASE "peachpuff": c~& = 4294957753
  262.                         CASE " periwinkle": c~& = 4291154150
  263.                         CASE "peru": c~& = 4291659071
  264.                         CASE "piggypink": c~& = 4294827494
  265.                         CASE "pinegreen": c~& = 4279599224
  266.                         CASE "pink": c~& = 4294951115
  267.                         CASE "pinkflamingo": c~& = 4294735101
  268.                         CASE "pinksherbet": c~& = 4294414247
  269.                         CASE "plum": c~& = 4292714717
  270.                         CASE "powderblue": c~& = 4289781990
  271.                         CASE "purple": c~& = 4286578816
  272.                         CASE "purpleheart": c~& = 4285809352
  273.                         CASE "purplemountainsmajesty": c~& = 4288512442
  274.                         CASE "purplepizzazz": c~& = 4294856410
  275.                         CASE "radicalred": c~& = 4294920556
  276.                         CASE "rawsienna": c~& = 4292250201
  277.                         CASE "rawumber": c~& = 4285614883
  278.                         CASE "razzledazzlerose": c~& = 4294920400
  279.                         CASE "razzmatazz": c~& = 4293076331
  280.                         CASE "red": c~& = 4294901760
  281.                         CASE "redorange": c~& = 4294923081
  282.                         CASE "redviolet": c~& = 4290790543
  283.                         CASE "robinseggblue": c~& = 4280274635
  284.                         CASE "rosybrown": c~& = 4290547599
  285.                         CASE "royalblue": c~& = 4282477025
  286.                         CASE "royalpurple": c~& = 4286075305
  287.                         CASE "saddlebrown": c~& = 4287317267
  288.                         CASE "salmon": c~& = 4294606962
  289.                         CASE "sandybrown": c~& = 4294222944
  290.                         CASE "scarlet": c~& = 4294715463
  291.                         CASE "screamingreen": c~& = 4285988730
  292.                         CASE "seagreen": c~& = 4281240407
  293.                         CASE "seashell": c~& = 4294964718
  294.                         CASE "sepia": c~& = 4289030479
  295.                         CASE "shadow": c~& = 4287265117
  296.                         CASE "shamrock": c~& = 4282764962
  297.                         CASE "shockingpink": c~& = 4294672125
  298.                         CASE "sienna": c~& = 4288696877
  299.                         CASE "silver": c~& = 4290822336
  300.                         CASE "skyblue": c~& = 4287090411
  301.                         CASE "slateblue": c~& = 4285160141
  302.                         CASE "slategray": c~& = 4285563024
  303.                         CASE "snow": c~& = 4294966010
  304.                         CASE "springgreen": c~& = 4278255487
  305.                         CASE "steelblue": c~& = 4282811060
  306.                         CASE "sunglow": c~& = 4294954824
  307.                         CASE "sunsetorange": c~& = 4294794835
  308.                         CASE "tann": c~& = 4291998860
  309.                         CASE "teal": c~& = 4278222976
  310.                         CASE "tealblue": c~& = 4279805877
  311.                         CASE "thistle": c~& = 4292394968
  312.                         CASE "ticklemepink": c~& = 4294740396
  313.                         CASE "timberwolf": c~& = 4292597714
  314.                         CASE "tomato": c~& = 4294927175
  315.                         CASE "tropicalrainforest": c~& = 4279730285
  316.                         CASE "tumbleweed": c~& = 4292782728
  317.                         CASE "turquoise": c~& = 4282441936
  318.                         CASE "turquoiseblue": c~& = 4286045671
  319.                         CASE "unmellowyellow": c~& = 4294967142
  320.                         CASE "violet": c~& = 4293821166
  321.                         CASE "violetblue": c~& = 4281486002
  322.                         CASE "violetred": c~& = 4294398868
  323.                         CASE "vividtangerine": c~& = 4294942857
  324.                         CASE "vividviolet": c~& = 4287582365
  325.                         CASE "wheat": c~& = 4294303411
  326.                         CASE "white": c~& = 4294967295
  327.                         CASE "whitesmoke": c~& = 4294309365
  328.                         CASE "wildblueyonder": c~& = 4288851408
  329.                         CASE "wildstrawberry": c~& = 4294919076
  330.                         CASE "wildwatermelon": c~& = 4294732933
  331.                         CASE "wisteria": c~& = 4291667166
  332.                         CASE "yellow": c~& = 4294967040
  333.                         CASE "yellowgreen": c~& = 4288335154
  334.                         CASE "yelloworange": c~& = 4294946370
  335.                     END SELECT
  336.                 END IF
  337.             ELSE
  338.  
  339.  
  340.                 ValueLen = LEN(value$)
  341.  
  342.                 REDIM CLRS(2) AS _UNSIGNED _BYTE
  343.                 i = 0
  344.  
  345.                 DO UNTIL ValueLen = 0
  346.                     f = INSTR(value$, ",")
  347.                     IF f <= 0 THEN f = ValueLen
  348.                     R$ = LEFT$(value$, f)
  349.                     CLRS(i) = VAL(R$)
  350.                     i = i + 1
  351.                     ValueLen = ValueLen - LEN(R$)
  352.                     value$ = MID$(value$, f + 1)
  353.                 LOOP
  354.  
  355.                 c~& = _RGB32(CLRS(0), CLRS(1), CLRS(2))
  356.  
  357.             END IF
  358.  
  359.             IF c~& THEN COLOR c~& ELSE COLOR _RGB32(VAL(value$))
  360.  
  361.         ELSE
  362.             Text$ = Text$ + CHR$(command)
  363.         END IF
  364.     NEXT
  365.     IF LEN(Text$) THEN PRINT Text$
  366.  

« Last Edit: August 05, 2021, 08:39:29 am by Petr »

FellippeHeitor

  • Guest
Re: SUB for printing colored strings easy
« Reply #1 on: August 05, 2021, 08:45:12 am »
Thanks for sharing, Petr! Is there a way to escape the star character in case I wanna print colored stars?

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: SUB for printing colored strings easy
« Reply #2 on: August 05, 2021, 08:59:16 am »
Hi Fellippe, yes it is possible:

Code: QB64: [Select]
  1. 'Program show new utility for direct text coloring. Use in 32 bit mode.
  2.  
  3. SCREEN _NEWIMAGE(1024, 768, 32)
  4.  
  5. 'old QB64 use for colored printing to screen:
  6. COLOR _RGB32(0, 255, 0): PRINT "This ";: COLOR _RGB32(255, 0, 0): PRINT "is ";: COLOR _RGB32(0, 0, 255): PRINT "colored ";: COLOR _RGB32(255, 255, 0): PRINT "text."
  7. 'next possibility using $COLOR:32
  8. COLOR LightGreen: PRINT "This ";: COLOR Red: PRINT "is ";: COLOR Blue: PRINT "colored ";: COLOR Yellow: PRINT "text."
  9.  
  10. 'new possibility:
  11.  
  12. COLTEXT "/LightGreen/This /Red/Is /Blue/colored /Yellow/text."
  13.  
  14. 'OR
  15.  
  16. COLTEXT "/0,255,0/This /255,0,0/is /0,0,255/colored /255,255,0/text."
  17.  
  18. 'OR combinated (if is $Color:32 used):
  19.  
  20. COLTEXT "/LightGreen/This /255,0,0/is /0,0,255/colored /YELLOW/text."
  21.  
  22.  
  23. 'Also one - number color is supported. So for full white:
  24. COLTEXT "/255/This is writed in _RGB32 (255, 255, 255)"
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. SUB COLTEXT (S$)
  39.     DC~& = _DEFAULTCOLOR
  40.     TextLenght = LEN(S$)
  41.     FOR all = 1 TO TextLenght
  42.         command = ASC(S$, all)
  43.  
  44.         IF command = ASC("/") THEN '                                         changed * to /
  45.             IF LEN(Text$) THEN PRINT Text$;
  46.  
  47.             Text$ = ""
  48.             value$ = ""
  49.             cmd$ = ""
  50.             c~& = 0
  51.  
  52.             DO UNTIL cmd$ = "/" '                                            changed * to /
  53.                 all = all + 1
  54.                 cmd$ = MID$(S$, all, 1)
  55.                 IF cmd$ <> "/" THEN value$ = value$ + cmd$ '                 changed * to /
  56.                 IF all >= TextLenght THEN EXIT DO
  57.             LOOP
  58.  
  59.  
  60.             IF INSTR(value$, ",") = 0 THEN
  61.  
  62.  
  63.                 IF LEN(value$) AND VAL(value$) = 0 THEN '$COLOR32 SUPPORT
  64.                     SELECT CASE LCASE$(_TRIM$(value$))
  65.                         CASE "aliceblue": c~& = 4293982463
  66.                         CASE "almond": c~& = 4293910221
  67.                         CASE "antiquebrass": c~& = 4291663221
  68.                         CASE "antiquewhite": c~& = 4294634455
  69.                         CASE "apricot": c~& = 4294826421
  70.                         CASE "aqua": c~& = 4278255615
  71.                         CASE "aquamarine": c~& = 4286578644
  72.                         CASE "asparagus": c~& = 4287080811
  73.                         CASE "atomictangerine": c~& = 4294943860
  74.                         CASE "azure": c~& = 4293984255
  75.                         CASE "bananamania": c~& = 4294633397
  76.                         CASE "beaver": c~& = 4288643440
  77.                         CASE "beige": c~& = 4294309340
  78.                         CASE "bisque": c~& = 4294960324
  79.                         CASE "bittersweet": c~& = 4294802542
  80.                         CASE "black": c~& = 4278190080
  81.                         CASE "blanchedalmond": c~& = 4294962125
  82.                         CASE "blizzardblue": c~& = 4289521134
  83.                         CASE "blue": c~& = 4278190335
  84.                         CASE "bluebell": c~& = 4288848592
  85.                         CASE "bluegray": c~& = 4284914124
  86.                         CASE "bluegreen": c~& = 4279081146
  87.                         CASE "blueviolet": c~& = 4287245282
  88.                         CASE "blush": c~& = 4292763011
  89.                         CASE "brickred": c~& = 4291510612
  90.                         CASE "brown": c~& = 4289014314
  91.                         CASE "burlywood": c~& = 4292786311
  92.                         CASE "burntorange": c~& = 4294934345
  93.                         CASE "burntsienna": c~& = 4293557853
  94.                         CASE "cadetblue": c~& = 4284456608
  95.                         CASE "canary": c~& = 4294967193
  96.                         CASE "caribbeangreen": c~& = 4280079266
  97.                         CASE "carnationpink": c~& = 4294945484
  98.                         CASE "cerise": c~& = 4292691090
  99.                         CASE "cerulean": c~& = 4280134870
  100.                         CASE "chartreuse": c~& = 4286578432
  101.                         CASE "chestnut": c~& = 4290534744
  102.                         CASE "chocolate": c~& = 4291979550
  103.                         CASE "copper": c~& = 4292711541
  104.                         CASE "coral": c~& = 4294934352
  105.                         CASE "cornflower": c~& = 4288335595
  106.                         CASE "cornflowerblue": c~& = 4284782061
  107.                         CASE "cornsilk": c~& = 4294965468
  108.                         CASE "cottoncandy": c~& = 4294950105
  109.                         CASE "crayolaaquamarine": c~& = 4286110690
  110.                         CASE "crayolablue": c~& = 4280251902
  111.                         CASE "crayolablueviolet": c~& = 4285753021
  112.                         CASE "crayolabrown": c~& = 4290013005
  113.                         CASE "crayolacadetblue": c~& = 4289771462
  114.                         CASE "crayolaforestgreen": c~& = 4285378177
  115.                         CASE "crayolagold": c~& = 4293379735
  116.                         CASE "crayolagoldenrod": c~& = 4294760821
  117.                         CASE "crayolagray": c~& = 4287992204
  118.                         CASE "crayolagreen": c~& = 4280069240
  119.                         CASE "crayolagreenyellow": c~& = 4293978257
  120.                         CASE "crayolaindigo": c~& = 4284315339
  121.                         CASE "crayolalavender": c~& = 4294751445
  122.                         CASE "crayolamagenta": c~& = 4294337711
  123.                         CASE "crayolamaroon": c~& = 4291311706
  124.                         CASE "crayolamidnightblue": c~& = 4279912566
  125.                         CASE "crayolaorange": c~& = 4294931768
  126.                         CASE "crayolaorangered": c~& = 4294912811
  127.                         CASE "crayolaorchid": c~& = 4293306583
  128.                         CASE "crayolaplum": c~& = 4287513989
  129.                         CASE "crayolared": c~& = 4293795917
  130.                         CASE "crayolasalmon": c~& = 4294941610
  131.                         CASE "crayolaseagreen": c~& = 4288668351
  132.                         CASE "crayolasilver": c~& = 4291675586
  133.                         CASE "crayolaskyblue": c~& = 4286634731
  134.                         CASE "crayolaspringgreen": c~& = 4293716670
  135.                         CASE "crayolatann": c~& = 4294616940
  136.                         CASE "crayolathistle": c~& = 4293642207
  137.                         CASE "crayolaviolet": c~& = 4287786670
  138.                         CASE "crayolayellow": c~& = 4294764675
  139.                         CASE "crayolayellowgreen": c~& = 4291158916
  140.                         CASE "crimson": c~& = 4292613180
  141.                         CASE "cyan": c~& = 4278255615
  142.                         CASE "dandelion": c~& = 4294826861
  143.                         CASE "darkblue": c~& = 4278190219
  144.                         CASE "darkcyan": c~& = 4278225803
  145.                         CASE "darkgoldenrod": c~& = 4290283019
  146.                         CASE "darkgray": c~& = 4289309097
  147.                         CASE "darkgreen": c~& = 4278215680
  148.                         CASE "darkkhaki": c~& = 4290623339
  149.                         CASE "darkmagenta": c~& = 4287299723
  150.                         CASE "darkolivegreen": c~& = 4283788079
  151.                         CASE "darkorange": c~& = 4294937600
  152.                         CASE "darkorchid": c~& = 4288230092
  153.                         CASE "darkred": c~& = 4287299584
  154.                         CASE "darksalmon": c~& = 4293498490
  155.                         CASE "darkseagreen": c~& = 4287609999
  156.                         CASE "darkslateblue": c~& = 4282924427
  157.                         CASE "darkslategray": c~& = 4281290575
  158.                         CASE "darkturquoise": c~& = 4278243025
  159.                         CASE "darkviolet": c~& = 4287889619
  160.                         CASE "deeppink": c~& = 4294907027
  161.                         CASE "deepskyblue": c~& = 4278239231
  162.                         CASE "denim": c~& = 4281035972
  163.                         CASE "desertsand": c~& = 4293905848
  164.                         CASE "dimgray": c~& = 4285098345
  165.                         CASE "dodgerblue": c~& = 4280193279
  166.                         CASE "eggplant": c~& = 4285419872
  167.                         CASE "electriclime": c~& = 4291755805
  168.                         CASE "fern": c~& = 4285643896
  169.                         CASE "firebrick": c~& = 4289864226
  170.                         CASE "floralwhite": c~& = 4294966000
  171.                         CASE "forestgreen": c~& = 4280453922
  172.                         CASE "fuchsia": c~& = 4290995397
  173.                         CASE "fuzzywuzzy": c~& = 4291585638
  174.                         CASE "gainsboro": c~& = 4292664540
  175.                         CASE "ghostwhite": c~& = 4294506751
  176.                         CASE "gold": c~& = 4294956800
  177.                         CASE "goldenrod": c~& = 4292519200
  178.                         CASE "grannysmithapple": c~& = 4289258656
  179.                         CASE "gray": c~& = 4286611584
  180.                         CASE "green": c~& = 4278222848
  181.                         CASE "greenblue": c~& = 4279329972
  182.                         CASE "greenyellow": c~& = 4289593135
  183.                         CASE "honeydew": c~& = 4293984240
  184.                         CASE "hotmagenta": c~& = 4294909390
  185.                         CASE "hotpink": c~& = 4294928820
  186.                         CASE "inchworm": c~& = 4289915997
  187.                         CASE "indianred": c~& = 4291648604
  188.                         CASE "indigo": c~& = 4283105410
  189.                         CASE "ivory": c~& = 4294967280
  190.                         CASE "jazzberryjam": c~& = 4291442535
  191.                         CASE "junglegreen": c~& = 4282101903
  192.                         CASE "khaki": c~& = 4293977740
  193.                         CASE "laserlemon": c~& = 4294901282
  194.                         CASE "lavender": c~& = 4293322490
  195.                         CASE "lavenderblush": c~& = 4294963445
  196.                         CASE "lawngreen": c~& = 4286381056
  197.                         CASE "lemonchiffon": c~& = 4294965965
  198.                         CASE "lemonyellow": c~& = 4294964303
  199.                         CASE "lightblue": c~& = 4289583334
  200.                         CASE "lightcoral": c~& = 4293951616
  201.                         CASE "lightcyan": c~& = 4292935679
  202.                         CASE "lightgoldenrodyellow": c~& = 4294638290
  203.                         CASE "lightgray": c~& = 4292072403
  204.                         CASE "lightgreen": c~& = 4287688336
  205.                         CASE "lightpink": c~& = 4294948545
  206.                         CASE "lightsalmon": c~& = 4294942842
  207.                         CASE "lightseagreen": c~& = 4280332970
  208.                         CASE "lightskyblue": c~& = 4287090426
  209.                         CASE "lightslategray": c~& = 4286023833
  210.                         CASE "lightsteelblue": c~& = 4289774814
  211.                         CASE "lightyellow": c~& = 4294967264
  212.                         CASE "lime": c~& = 4278255360
  213.                         CASE "limegreen": c~& = 4281519410
  214.                         CASE "linen": c~& = 4294635750
  215.                         CASE "macaroniandcheese": c~& = 4294950280
  216.                         CASE "magenta": c~& = 4294902015
  217.                         CASE "magicmint": c~& = 4289392849
  218.                         CASE "mahogany": c~& = 4291643980
  219.                         CASE "maize": c~& = 4293775772
  220.                         CASE "manatee": c~& = 4288125610
  221.                         CASE "mangotango": c~& = 4294935107
  222.                         CASE "maroon": c~& = 4286578688
  223.                         CASE "mauvelous": c~& = 4293892266
  224.                         CASE "mediumaquamarine": c~& = 4284927402
  225.                         CASE "mediumblue": c~& = 4278190285
  226.                         CASE "mediumorchid": c~& = 4290401747
  227.                         CASE "mediumpurple": c~& = 4287852763
  228.                         CASE "mediumseagreen": c~& = 4282168177
  229.                         CASE "mediumslateblue": c~& = 4286277870
  230.                         CASE "mediumspringgreen": c~& = 4278254234
  231.                         CASE "mediumturquoise": c~& = 4282962380
  232.                         CASE "mediumvioletred": c~& = 4291237253
  233.                         CASE "melon": c~& = 4294818996
  234.                         CASE "midnightblue": c~& = 4279834992
  235.                         CASE "mintcream": c~& = 4294311930
  236.                         CASE "mistyrose": c~& = 4294960353
  237.                         CASE "moccasin": c~& = 4294960309
  238.                         CASE "mountainmeadow": c~& = 4281383567
  239.                         CASE "mulberry": c~& = 4291120012
  240.                         CASE "navajowhite": c~& = 4294958765
  241.                         CASE "navy": c~& = 4278190208
  242.                         CASE "navyblue": c~& = 4279858386
  243.                         CASE "neoncarrot": c~& = 4294943555
  244.                         CASE "oldlace": c~& = 4294833638
  245.                         CASE "olive": c~& = 4286611456
  246.                         CASE "olivedrab": c~& = 4285238819
  247.                         CASE "olivegreen": c~& = 4290426988
  248.                         CASE "orange": c~& = 4294944000
  249.                         CASE "orangered": c~& = 4294919424
  250.                         CASE "orangeyellow": c~& = 4294497640
  251.                         CASE "orchid": c~& = 4292505814
  252.                         CASE "outerspace": c~& = 4282468940
  253.                         CASE "outrageousorange": c~& = 4294929994
  254.                         CASE "pacificblue": c~& = 4280068553
  255.                         CASE "palegoldenrod": c~& = 4293847210
  256.                         CASE "palegreen": c~& = 4288215960
  257.                         CASE "paleturquoise": c~& = 4289720046
  258.                         CASE "palevioletred": c~& = 4292571283
  259.                         CASE "papayawhip": c~& = 4294963157
  260.                         CASE "peach": c~& = 4294954923
  261.                         CASE "peachpuff": c~& = 4294957753
  262.                         CASE " periwinkle": c~& = 4291154150
  263.                         CASE "peru": c~& = 4291659071
  264.                         CASE "piggypink": c~& = 4294827494
  265.                         CASE "pinegreen": c~& = 4279599224
  266.                         CASE "pink": c~& = 4294951115
  267.                         CASE "pinkflamingo": c~& = 4294735101
  268.                         CASE "pinksherbet": c~& = 4294414247
  269.                         CASE "plum": c~& = 4292714717
  270.                         CASE "powderblue": c~& = 4289781990
  271.                         CASE "purple": c~& = 4286578816
  272.                         CASE "purpleheart": c~& = 4285809352
  273.                         CASE "purplemountainsmajesty": c~& = 4288512442
  274.                         CASE "purplepizzazz": c~& = 4294856410
  275.                         CASE "radicalred": c~& = 4294920556
  276.                         CASE "rawsienna": c~& = 4292250201
  277.                         CASE "rawumber": c~& = 4285614883
  278.                         CASE "razzledazzlerose": c~& = 4294920400
  279.                         CASE "razzmatazz": c~& = 4293076331
  280.                         CASE "red": c~& = 4294901760
  281.                         CASE "redorange": c~& = 4294923081
  282.                         CASE "redviolet": c~& = 4290790543
  283.                         CASE "robinseggblue": c~& = 4280274635
  284.                         CASE "rosybrown": c~& = 4290547599
  285.                         CASE "royalblue": c~& = 4282477025
  286.                         CASE "royalpurple": c~& = 4286075305
  287.                         CASE "saddlebrown": c~& = 4287317267
  288.                         CASE "salmon": c~& = 4294606962
  289.                         CASE "sandybrown": c~& = 4294222944
  290.                         CASE "scarlet": c~& = 4294715463
  291.                         CASE "screamingreen": c~& = 4285988730
  292.                         CASE "seagreen": c~& = 4281240407
  293.                         CASE "seashell": c~& = 4294964718
  294.                         CASE "sepia": c~& = 4289030479
  295.                         CASE "shadow": c~& = 4287265117
  296.                         CASE "shamrock": c~& = 4282764962
  297.                         CASE "shockingpink": c~& = 4294672125
  298.                         CASE "sienna": c~& = 4288696877
  299.                         CASE "silver": c~& = 4290822336
  300.                         CASE "skyblue": c~& = 4287090411
  301.                         CASE "slateblue": c~& = 4285160141
  302.                         CASE "slategray": c~& = 4285563024
  303.                         CASE "snow": c~& = 4294966010
  304.                         CASE "springgreen": c~& = 4278255487
  305.                         CASE "steelblue": c~& = 4282811060
  306.                         CASE "sunglow": c~& = 4294954824
  307.                         CASE "sunsetorange": c~& = 4294794835
  308.                         CASE "tann": c~& = 4291998860
  309.                         CASE "teal": c~& = 4278222976
  310.                         CASE "tealblue": c~& = 4279805877
  311.                         CASE "thistle": c~& = 4292394968
  312.                         CASE "ticklemepink": c~& = 4294740396
  313.                         CASE "timberwolf": c~& = 4292597714
  314.                         CASE "tomato": c~& = 4294927175
  315.                         CASE "tropicalrainforest": c~& = 4279730285
  316.                         CASE "tumbleweed": c~& = 4292782728
  317.                         CASE "turquoise": c~& = 4282441936
  318.                         CASE "turquoiseblue": c~& = 4286045671
  319.                         CASE "unmellowyellow": c~& = 4294967142
  320.                         CASE "violet": c~& = 4293821166
  321.                         CASE "violetblue": c~& = 4281486002
  322.                         CASE "violetred": c~& = 4294398868
  323.                         CASE "vividtangerine": c~& = 4294942857
  324.                         CASE "vividviolet": c~& = 4287582365
  325.                         CASE "wheat": c~& = 4294303411
  326.                         CASE "white": c~& = 4294967295
  327.                         CASE "whitesmoke": c~& = 4294309365
  328.                         CASE "wildblueyonder": c~& = 4288851408
  329.                         CASE "wildstrawberry": c~& = 4294919076
  330.                         CASE "wildwatermelon": c~& = 4294732933
  331.                         CASE "wisteria": c~& = 4291667166
  332.                         CASE "yellow": c~& = 4294967040
  333.                         CASE "yellowgreen": c~& = 4288335154
  334.                         CASE "yelloworange": c~& = 4294946370
  335.                     END SELECT
  336.                 END IF
  337.             ELSE
  338.  
  339.  
  340.                 ValueLen = LEN(value$)
  341.  
  342.                 REDIM CLRS(2) AS _UNSIGNED _BYTE
  343.                 i = 0
  344.  
  345.                 DO UNTIL ValueLen = 0
  346.                     f = INSTR(value$, ",")
  347.                     IF f <= 0 THEN f = ValueLen
  348.                     R$ = LEFT$(value$, f)
  349.                     CLRS(i) = VAL(R$)
  350.                     i = i + 1
  351.                     ValueLen = ValueLen - LEN(R$)
  352.                     value$ = MID$(value$, f + 1)
  353.                 LOOP
  354.  
  355.                 c~& = _RGB32(CLRS(0), CLRS(1), CLRS(2))
  356.  
  357.             END IF
  358.  
  359.             IF c~& THEN COLOR c~& ELSE COLOR _RGB32(VAL(value$))
  360.  
  361.         ELSE
  362.             Text$ = Text$ + CHR$(command)
  363.         END IF
  364.     NEXT
  365.     IF LEN(Text$) THEN PRINT Text$
  366.  
  367.  
  368.  

In this version is used "/" and not "*"

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: SUB for printing colored strings easier
« Reply #3 on: August 05, 2021, 10:30:55 am »
Very neat, Petr! Speaking of "printing" and "colored text"....

Code: QB64: [Select]
  1.  
  2. $ExeIcon:'.\internal\source\qb64.ico'
  3.  
  4. $VersionInfo:CompanyName=SpriggsySpriggs
  5. $VersionInfo:ProductName=WinGDI Test
  6. $VersionInfo:FileDescription=A test of the WinGDI printing system
  7.  
  8. Type SIZE
  9.     As Long cx, cy
  10.  
  11.     As Long x, y
  12.  
  13. Type MSG
  14.     As _Offset hwnd
  15.     As _Unsigned Long message
  16.     As _Unsigned _Offset wParam
  17.     As _Offset lParam
  18.     As Long time
  19.     As POINT pt
  20.     As Long lPrivate
  21.  
  22. Type DOCINFO
  23.     As Long cbSize
  24.     As _Offset lpszDocName, lpszOutput, lpszDatatype
  25.     As _Unsigned Long fwType
  26.  
  27. Type PRINTDLG
  28.     As _Unsigned Long lStructSize
  29.     $If 64BIT Then
  30.         As String * 4 padding
  31.     $End If
  32.     As _Offset hwndOwner, hDevMode, hDevNames, hDC
  33.     As _Unsigned Long Flags
  34.     As Integer nFromPage, nToPage, nMinPage, nMaxPage, nCopies
  35.     $If 64BIT Then
  36.         As String * 2 padding2
  37.     $End If
  38.     As _Offset hInstance, lCustData, lpfnPrintHook, lpfnSetupHook, lpPrintTemplateName, lpSetupTemplateName, hPrintTemplate, hSetupTemplate
  39.  
  40.     Sub PrintDlg Alias "PrintDlgA" (ByVal lppd As _Offset)
  41.  
  42.     Function GetDeviceCaps& (ByVal hdc As _Offset, Byval index As Long)
  43.     Sub SelectObject (ByVal hdc As _Offset, Byval h As _Offset)
  44.     Function CreatePen%& (ByVal iStyle As Long, Byval cWidth As Long, Byval color As _Unsigned Long)
  45.     Sub Rectangle (ByVal hdc As _Offset, Byval left As Long, Byval top As Long, Byval right As Long, Byval bottom As Long)
  46.     Sub SetBkMode (ByVal hdc As _Offset, Byval mode As Long)
  47.     Sub TextOut Alias "TextOutA" (ByVal hdc As _Offset, Byval x As Long, Byval y As Long, Byval lpString As _Offset, Byval c As Long)
  48.     Function SetAbortProc& (ByVal hdc As _Offset, Byval proc As _Offset)
  49.     Sub StartDoc Alias "StartDocA" (ByVal hdc As _Offset, Byval lpdi As _Offset)
  50.     Sub StartPage (ByVal hdc As _Offset)
  51.     Sub EndPage (ByVal hdc As _Offset)
  52.     Sub EndDoc (ByVal hdc As _Offset)
  53.     Sub DeleteDC (ByVal hdc As _Offset)
  54.     Function CreateFont%& Alias "CreateFontA" (ByVal cHeight As Long, Byval cWidth As Long, Byval cEscapement As Long, Byval cOrientation As Long, Byval cWeight As Long, Byval bItalic As _Unsigned Long, Byval bUnderline As _Unsigned Long, Byval bStrikeout As _Unsigned Long, Byval iCharSet As _Unsigned Long, Byval iOutPrecision As _Unsigned Long, Byval iClipPrecision As _Unsigned Long, Byval iQuality As _Unsigned Long, Byval iPitchAndFamily As _Unsigned Long, pszFaceName As String)
  55.     Sub SetTextColor (ByVal hdc As _Offset, Byval color As _Unsigned Long)
  56.     Sub GetTextExtentPoint32 Alias "GetTextExtentPoint32A" (ByVal hdc As _Offset, lpString As String, Byval c As Long, Byval psizl As _Offset)
  57.  
  58.     Function PeekMessage%% (ByVal lpMsg As _Offset, Byval hWnd As _Offset, Byval wMsgFilterMin As _Unsigned Long, Byval wMsgFilterMax As _Unsigned Long, Byval wRemoveMsg As _Unsigned Long)
  59.     Sub TranslateMessage (ByVal lpMsg As _Offset)
  60.     Sub DispatchMessage (ByVal lpMsg As _Offset)
  61.     Sub MessageBox (ByVal hWnd As _Offset, lpText As String, lpCaption As String, Byval uType As _Unsigned Long)
  62.     Function GetLastError~& ()
  63.  
  64.     Function pAbortProc%& ()
  65.  
  66. $If 64BIT Then
  67.     Declare Library ".\internal\c\c_compiler\x86_64-w64-mingw32\include\wingdi"
  68.     End Declare
  69.     Declare Library ".\internal\c\c_compiler\i686-w64-mingw32\include\wingdi"
  70.     End Declare
  71. Dim Shared As PRINTDLG pdlg
  72. PrintJob
  73.  
  74. Sub InitPrintJobDoc (di As DOCINFO, docname As String)
  75.     docname = docname + Chr$(0)
  76.     di.cbSize = Len(di)
  77.     di.lpszDocName = _Offset(docname)
  78.  
  79. Sub DrawPage (hdc As _Offset, Page As _Unsigned Long)
  80.     Const HORZRES = 8
  81.     Const VERTRES = 10
  82.     Const PS_SOLID = 0
  83.     Const TRANSPARENT = 1
  84.     Dim As String * 50 gdiline
  85.     Dim As Long nWidth, nHeight
  86.     nWidth = GetDeviceCaps(hdc, HORZRES)
  87.     nHeight = GetDeviceCaps(hdc, VERTRES)
  88.     'SelectObject hdc, CreatePen(PS_SOLID, 2, RGB(255, 0, 0))
  89.     'Rectangle hdc, 0, 0, nWidth - 4, nHeight - 2
  90.     SetBkMode hdc, TRANSPARENT
  91.     Dim As SIZE size
  92.     Dim As String t1, t2, t3
  93.     t1 = "Title!"
  94.     t2 = "This is another print test!"
  95.     t3 = "Page" + Str$(Page)
  96.     HDCPrint hdc, t1, "Castellar", 72, "UNDERLINE", "BLACK", 425, 550, RGB(188, 33, 116)
  97.     HDCPrint hdc, t2, "Freestyle Script", 48, "UNDERLINE", "BLACK", 425, 750, RGB(33, 127, 127)
  98.     HDCPrint hdc, t3, "Goudy Stout", 24, "", "BLACK", 425, 1100, RGB(127, 55, 127)
  99.  
  100. Function AbortProc%% (hDC As _Offset, Errr As Long)
  101.     Const PM_REMOVE = &H0001
  102.     Dim As MSG msg
  103.     While PeekMessage(_Offset(msg), 0, 0, 0, PM_REMOVE)
  104.         TranslateMessage _Offset(msg)
  105.         DispatchMessage _Offset(msg)
  106.     Wend
  107.     AbortProc = -1
  108.  
  109. Function GetPrinterDC%& (Pages As _Unsigned Long)
  110.     Const PD_RETURNDC = &H100
  111.     pdlg.lStructSize = Len(pdlg)
  112.     pdlg.Flags = PD_RETURNDC
  113.     pdlg.nMinPage = 1
  114.     pdlg.nMaxPage = Pages
  115.     pdlg.nToPage = Pages
  116.     PrintDlg _Offset(pdlg)
  117.     GetPrinterDC = pdlg.hDC
  118.  
  119. Sub PrintJob ()
  120.     Const MB_OK = &H00000000
  121.     Const MB_APPLMODAL = &H00000000
  122.     Const SP_ERROR = -1
  123.     Dim As _Offset hDC
  124.     Dim As DOCINFO di
  125.     hDC = GetPrinterDC(2)
  126.     If hDC = 0 Then
  127.         MessageBox 0, "Error creating DC" + Chr$(0), "Error" + Chr$(0), MB_APPLMODAL Or MB_OK
  128.         Exit Sub
  129.     End If
  130.     If SetAbortProc(hDC, pAbortProc) = SP_ERROR Then
  131.         MessageBox 0, "Error setting up AbortProc" + Chr$(0), "Error" + Chr$(0), MB_APPLMODAL Or MB_OK
  132.         Exit Sub
  133.     End If
  134.     InitPrintJobDoc di, "MyDoc"
  135.     StartDoc hDC, _Offset(di)
  136.     Dim As Long i, l
  137.     For l = 1 To pdlg.nCopies
  138.         For i = 1 To pdlg.nToPage
  139.             StartPage hDC
  140.             DrawPage hDC, i
  141.             EndPage hDC
  142.         Next
  143.     Next
  144.     EndDoc hDC
  145.     DeleteDC hDC
  146.  
  147. Sub HDCPrint (hdc As _Offset, text As String, fontName As String * 32, height As Long, style As String, weightStyle As String, x As Long, y As Long, colorref As _Unsigned Long)
  148.     Dim As _Offset font
  149.     Dim As _Byte bold, underline, strikeout, italic
  150.     Dim As Long weight
  151.     style = UCase$(style)
  152.     If InStr(style, "UNDERLINE") Then underline = -1
  153.     If InStr(style, "STRIKEOUT") Then strikeout = -1
  154.     If InStr(style, "ITALIC") Then italic = -1
  155.     Select Case UCase$(weightStyle)
  156.         Case "THIN"
  157.             weight = 100
  158.         Case "EXTRALIGHT", "ULTRALIGHT"
  159.             weight = 200
  160.         Case "LIGHT"
  161.             weight = 300
  162.         Case "NORMAL", "REGULAR"
  163.             weight = 400
  164.         Case "MEDIUM"
  165.             weight = 500
  166.         Case "SEMIBOLD", "DEMIBOLD"
  167.             weight = 600
  168.         Case "BOLD"
  169.             weight = 700
  170.         Case "EXTRABOLD", "ULTRABOLD"
  171.             weight = 800
  172.         Case "HEAVY", "BLACK"
  173.             weight = 900
  174.         Case Else
  175.             weight = 0
  176.     End Select
  177.     Dim As Long FF_DECORATIVE: FF_DECORATIVE = _SHL(5, 4)
  178.     Dim As Long FF_MODERN: FF_MODERN = _SHL(3, 4)
  179.     Dim As Long FF_ROMAN: FF_ROMAN = _SHL(1, 4)
  180.     Dim As Long FF_SCRIPT: FF_SCRIPT = _SHL(4, 4)
  181.     Dim As Long FF_SWISS: FF_SWISS = _SHL(2, 4)
  182.     Const LOGPIXELSY = 90
  183.     Const LOGPIXELSX = 88
  184.     Const DT_CALCRECT = &H00000400
  185.     Dim As Long DPIScaleY: DPIScaleY = GetDeviceCaps(hdc, LOGPIXELSY) / 96
  186.     Dim As Long DPIScaleX: DPIScaleX = GetDeviceCaps(hdc, LOGPIXELSX) / 96
  187.     font = CreateFont(height * DPIScaleY, 0, 0, 0, weight, italic, underline, strikeout, 0, 0, 0, 5, FF_DECORATIVE Or FF_MODERN Or FF_ROMAN Or FF_SCRIPT Or FF_SWISS, fontName + Chr$(0))
  188.     If font Then
  189.         SelectObject hdc, font
  190.         SetTextColor hdc, colorref
  191.         Dim As SIZE size
  192.         GetTextExtentPoint32 hdc, text, Len(text), _Offset(size)
  193.         Dim As Long nx, ny
  194.         If x > 0 Then nx = (x * DPIScaleX) - size.cx / 2 Else nx = x * DPIScaleX
  195.         If y > 0 Then ny = (y * DPIScaleY) - (size.cy) Else ny = y * DPIScaleY
  196.         TextOut hdc, nx, ny, _Offset(text), Len(text)
  197.     End If
* abort.h (Filesize: 0.24 KB, Downloads: 148)
Shuwatch!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: SUB for printing colored strings easy
« Reply #4 on: August 05, 2021, 11:30:19 am »
Hi @SpriggsySpriggs, what's that huge block of libraries? My printer is small scrap by HP (it cuts, I don't know yet if it's a weak wifi signal or why it does) even when using common programs in windows, as I understand it, it should create some color print, right? Well, it will be difficult for me, but if you really manage to squeeze a color printout from my black and white wireless printer, then my whole family will worship you as a god :)

Program behaves by popping up a windows dialog asking you to print and select a printer. I press OK. And nothing happens. But as I say. My printer is fooling around and I haven't figured out why yet. The truth is that she worked reliably in the original place, but now she is further away from the wifi router and has been working according to her mood ever since. Thank you for sharing, in truth, I don't know much about the huge number of called libraries.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: SUB for printing colored strings easy
« Reply #5 on: August 05, 2021, 11:31:18 am »
@Petr You can select "Microsoft Print to PDF" instead and see it work its magic
Shuwatch!