Author Topic: WPF Message Boxes in QB64!  (Read 2694 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
WPF Message Boxes in QB64!
« on: February 21, 2020, 05:56:57 pm »
Hello, Everyone!
I've been working on trying to find new ways to make message boxes in my programs because I didn't like being limited to the Windows API. I stumbled upon a wonderful PowerShell class here https://smsagent.blog/2017/08/24/a-customisable-wpf-messagebox-for-powershell/ and I've been working on a function that takes advantage of it to create WPF Message Boxes on the fly in your programs. The PowerShell script allows for CUSTOM BUTTONS and CUSTOM COLORS/STYLES! The below function is what builds the message box that gets called using PowerShell. I've edited the original PowerShell script to work with my function and it works quite well! Eventually I will make a version that takes advantage of the custom image feature of the WPF Message Box script as well as the media player. This is a work in progress and I will update this frequently with new things! (edit: Here is my GitHub page for the current status of my project: https://github.com/SpriggsySpriggs/WPFMessageBoxQB64)
ENJOY!
Here is the edit I made to the PowerShell code:
Quote
$Params = @{
Content = $args[0]
Title = $args[1]
ButtonType = $args[2]
ContentFontSize = $args[3]
TitleFontSize = $args[4]
BorderThickness = $args[5]
CornerRadius = $args[6]
ShadowDepth = $args[7]
BlurRadius = $args[8]
Timeout = $args[9]
ContentBackground = $args[10]
FontFamily = $args[11]
TitleFontWeight = $args[12]
ContentFontWeight = $args[13]
TitleTextForeground = $args[14]
ContentTextForeground = $args[15]
BorderBrush = $args[16]
TitleBackground = $args[17]
ButtonTextForeground = $args[18]
Sound = $args[19]
CustomButtons = $args[20],$args[21],$args[22]
}
 
New-WPFMessageBox @Params
switch ($WPFMessageBoxOutput){
"OK" {exit 1}
"Cancel" {exit 2}
"Abort" {exit 3}
"Retry" {exit 4}
"Ignore" {exit 5}
"Yes" {exit 6}
"No" {exit 7}
"TryAgain" {exit 8}
"Continue" {exit 9}
"None" {exit 10}
}
Code: QB64: [Select]
  1. TYPE Params
  2.     Content AS STRING 'mandatory, otherwise defaults to "null"
  3.     Title AS STRING 'not mandatory defaults to TITLE$
  4.     ButtonType AS STRING 'defaults to "OK"
  5.     'Valid ButtonType values are "OK", "OK-Cancel", "Abort-Retry-Ignore", "Yes-No-Cancel", "Yes-No", "Retry-Cancel", "Cancel-TryAgain-Continue", "None" ("None" is used when defining custom buttons. This QB64 program allows for 3)
  6.     ContentFontSize AS STRING 'defaults to "14"
  7.     TitleFontSize AS STRING 'defaults to "14"
  8.     BorderThickness AS STRING 'defaults to "0"
  9.     CornerRadius AS STRING 'defaults to "8"
  10.     ShadowDepth AS STRING 'defaults to "3"
  11.     BlurRadius AS STRING 'defaults to "20"
  12.     'WindowHost AS STRING
  13.     Timeout AS STRING 'not mandatory
  14.     'OnLoaded AS STRING 'not mandatory
  15.     'OnClosed AS STRING 'not mandatory
  16.     ContentBackground AS STRING 'not mandatory, defaults to "White"
  17.     FontFamily AS STRING 'not mandatory, defaults to "Segoe UI"
  18.     TitleFontWeight AS STRING 'not mandatory, defaults to "Normal"
  19.     ContentFontWeight AS STRING 'not mandatory, defaults to "Normal"
  20.     ContentTextForeground AS STRING 'not mandatory, defaults to "Black"
  21.     TitleTextForeground AS STRING 'not mandatory, defaults to "Black"
  22.     BorderBrush AS STRING 'not mandatory, defaults to "Black"
  23.     TitleBackground AS STRING 'not mandatory, defaults to "White"
  24.     ButtonTextForeground AS STRING 'not mandatory, defaults to "Black"
  25.     'Valid colors are listed at https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.colors?view=netframework-4.8
  26.     Sound AS STRING 'not mandatory, defaults to "Windows Notify System Generic"
  27.     'Valid sounds are "Windows Background","Windows Balloon","Windows Battery Critical","Windows Battery Low","Windows Critical Stop","Windows Default","Windows Ding","Windows Error","Windows Exclamation","Windows Feed Discovered","Windows Foreground","Windows Hardware Fail","Windows Hardware Insert","Windows Hardware Remove","Windows Information Bar","Windows Logoff Sound","Windows Logon","Windows Menu Command","Windows Message Nudge","Windows Minimize","Windows Navigation Start","Windows Notify Calendar","Windows Notify Email","Windows Notify Messaging","Windows Notify System Generic","Windows Notify","Windows Pop-up Blocked","Windows Print complete","Windows Proximity Connection","Windows Proximity Notification","Windows Recycle","Windows Restore","Windows Ringin","Windows Ringout","Windows Shutdown","Windows Startup","Windows Unlock","Windows User Account Control"
  28.     CustomButton1 AS STRING 'not mandatory, only used if ButtonType = "None"
  29.     CustomButton2 AS STRING 'not mandatory, only used if ButtonType = "None"
  30.     CustomButton3 AS STRING 'not mandatory, only used if ButtonType = "None"
  31. DIM SHARED Params AS Params
  32. FUNCTION WPFMessageBox (Params AS Params)
  33.     SHELL$ = "PowerShell -Command " + CHR$(34) + "&'" + _STARTDIR$ + "\New-WPFMessageBox.ps1'"
  34.     '==============================================================================
  35.     IF Params.Content <> "" AND INSTR(Params.Content, "\" + CHR$(34)) = 0 THEN
  36.         Params.Content = "\" + CHR$(34) + Params.Content + "\" + CHR$(34) '$args[0]
  37.         SHELL$ = SHELL$ + " " + Params.Content
  38.     ELSE
  39.         Params.Content = "null"
  40.         Params.Content = "\" + CHR$(34) + Params.Content + "\" + CHR$(34)
  41.         SHELL$ = SHELL$ + " " + Params.Content
  42.     END IF
  43.  
  44.     IF Params.Title <> "" AND INSTR(Params.Title, "\" + CHR$(34)) = 0 THEN
  45.         Params.Title = "\" + CHR$(34) + Params.Title + "\" + CHR$(34) '$args[1]
  46.         SHELL$ = SHELL$ + " " + Params.Title
  47.     ELSE
  48.         Params.Title = TITLE$
  49.         Params.Title = "\" + CHR$(34) + Params.Title + "\" + CHR$(34) '$args[1]
  50.         SHELL$ = SHELL$ + " " + Params.Title
  51.     END IF
  52.  
  53.     IF Params.ButtonType <> "" AND INSTR(Params.ButtonType, "\" + CHR$(34)) = 0 THEN
  54.         IF Params.ButtonType <> "OK" AND Params.ButtonType <> "OK-Cancel" AND Params.ButtonType <> "Abort-Retry-Ignore" AND Params.ButtonType <> "Yes-No-Cancel" AND Params.ButtonType <> "Yes-No" AND Params.ButtonType <> "Retry-Cancel" AND Params.ButtonType <> "Cancel-TryAgain-Continue" AND Params.ButtonType <> "None" THEN
  55.             Params.ButtonType = "OK"
  56.             Params.ButtonType = "\" + CHR$(34) + Params.ButtonType + "\" + CHR$(34)
  57.             SHELL$ = SHELL$ + " " + Params.ButtonType
  58.         ELSE
  59.             Params.ButtonType = "\" + CHR$(34) + Params.ButtonType + "\" + CHR$(34) '$args[2]
  60.             SHELL$ = SHELL$ + " " + Params.ButtonType
  61.         END IF
  62.     ELSE
  63.         Params.ButtonType = "OK"
  64.         Params.ButtonType = "\" + CHR$(34) + Params.ButtonType + "\" + CHR$(34)
  65.         SHELL$ = SHELL$ + " " + Params.ButtonType
  66.     END IF
  67.  
  68.     IF Params.ContentFontSize <> "" AND INSTR(Params.ContentFontSize, "\" + CHR$(34)) = 0 THEN
  69.         Params.ContentFontSize = "\" + CHR$(34) + Params.ContentFontSize + "\" + CHR$(34) '$args[3]
  70.         SHELL$ = SHELL$ + " " + Params.ContentFontSize
  71.     ELSE
  72.         Params.ContentFontSize = "14"
  73.         Params.ContentFontSize = "\" + CHR$(34) + Params.ContentFontSize + "\" + CHR$(34)
  74.         SHELL$ = SHELL$ + " " + Params.ContentFontSize
  75.     END IF
  76.  
  77.     IF Params.TitleFontSize <> "" AND INSTR(Params.TitleFontSize, "\" + CHR$(34)) = 0 THEN
  78.         Params.TitleFontSize = "\" + CHR$(34) + Params.TitleFontSize + "\" + CHR$(34) '$args[4]
  79.         SHELL$ = SHELL$ + " " + Params.TitleFontSize
  80.     ELSE
  81.         Params.TitleFontSize = "14"
  82.         Params.TitleFontSize = "\" + CHR$(34) + Params.TitleFontSize + "\" + CHR$(34)
  83.         SHELL$ = SHELL$ + " " + Params.TitleFontSize
  84.     END IF
  85.  
  86.     IF Params.BorderThickness <> "" AND INSTR(Params.BorderThickness, "\" + CHR$(34)) = 0 THEN
  87.         Params.BorderThickness = "\" + CHR$(34) + Params.BorderThickness + "\" + CHR$(34) '$args[5]
  88.         SHELL$ = SHELL$ + " " + Params.BorderThickness
  89.     ELSE
  90.         Params.BorderThickness = "0"
  91.         Params.BorderThickness = "\" + CHR$(34) + Params.BorderThickness + "\" + CHR$(34)
  92.         SHELL$ = SHELL$ + " " + Params.BorderThickness
  93.     END IF
  94.  
  95.     IF Params.CornerRadius <> "" AND INSTR(Params.CornerRadius, "\" + CHR$(34)) = 0 THEN
  96.         Params.CornerRadius = "\" + CHR$(34) + Params.CornerRadius + "\" + CHR$(34) '$args[6]
  97.         SHELL$ = SHELL$ + " " + Params.CornerRadius
  98.     ELSE
  99.         Params.CornerRadius = "5"
  100.         Params.CornerRadius = "\" + CHR$(34) + Params.CornerRadius + "\" + CHR$(34)
  101.         SHELL$ = SHELL$ + " " + Params.CornerRadius
  102.     END IF
  103.  
  104.     IF Params.ShadowDepth <> "" AND INSTR(Params.ShadowDepth, "\" + CHR$(34)) = 0 THEN
  105.         Params.ShadowDepth = "\" + CHR$(34) + Params.ShadowDepth + "\" + CHR$(34) '$args[7]
  106.         SHELL$ = SHELL$ + " " + Params.ShadowDepth
  107.     ELSE
  108.         Params.ShadowDepth = "3"
  109.         Params.ShadowDepth = "\" + CHR$(34) + Params.ShadowDepth + "\" + CHR$(34)
  110.         SHELL$ = SHELL$ + " " + Params.ShadowDepth
  111.     END IF
  112.  
  113.     IF Params.BlurRadius <> "" AND INSTR(Params.BlurRadius, "\" + CHR$(34)) = 0 THEN
  114.         Params.BlurRadius = "\" + CHR$(34) + Params.BlurRadius + "\" + CHR$(34) '$args[8]
  115.         SHELL$ = SHELL$ + " " + Params.BlurRadius
  116.     ELSE
  117.         Params.BlurRadius = "20"
  118.         Params.BlurRadius = "\" + CHR$(34) + Params.BlurRadius + "\" + CHR$(34)
  119.         SHELL$ = SHELL$ + " " + Params.BlurRadius
  120.     END IF
  121.  
  122.     IF Params.Timeout <> "" AND INSTR(Params.Timeout, "\" + CHR$(34)) = 0 THEN
  123.         Params.Timeout = "\" + CHR$(34) + Params.Timeout + "\" + CHR$(34) '$args[9]
  124.         SHELL$ = SHELL$ + " " + Params.Timeout
  125.     ELSE
  126.         Params.Timeout = "999"
  127.         Params.Timeout = "\" + CHR$(34) + Params.Timeout + "\" + CHR$(34)
  128.         SHELL$ = SHELL$ + " " + Params.Timeout
  129.     END IF
  130.  
  131.     IF Params.ContentBackground <> "" AND INSTR(Params.ContentBackground, "\" + CHR$(34)) = 0 THEN
  132.         Params.ContentBackground = "\" + CHR$(34) + Params.ContentBackground + "\" + CHR$(34) '$args[10]
  133.         SHELL$ = SHELL$ + " " + Params.ContentBackground
  134.     ELSE
  135.         Params.ContentBackground = "White"
  136.         Params.ContentBackground = "\" + CHR$(34) + Params.ContentBackground + "\" + CHR$(34)
  137.         SHELL$ = SHELL$ + " " + Params.ContentBackground
  138.     END IF
  139.  
  140.     IF Params.FontFamily <> "" AND INSTR(Params.FontFamily, "\" + CHR$(34)) = 0 THEN
  141.         Params.FontFamily = "\" + CHR$(34) + Params.FontFamily + "\" + CHR$(34) '$args[11]
  142.         SHELL$ = SHELL$ + " " + Params.FontFamily
  143.     ELSE
  144.         Params.FontFamily = "Segoe UI"
  145.         Params.FontFamily = "\" + CHR$(34) + Params.FontFamily + "\" + CHR$(34)
  146.         SHELL$ = SHELL$ + " " + Params.FontFamily
  147.     END IF
  148.  
  149.     IF Params.TitleFontWeight <> "" AND INSTR(Params.TitleFontWeight, "\" + CHR$(34)) = 0 THEN
  150.         Params.TitleFontWeight = "\" + CHR$(34) + Params.TitleFontWeight + "\" + CHR$(34) '$args[12]
  151.         SHELL$ = SHELL$ + " " + Params.TitleFontWeight
  152.     ELSE
  153.         Params.TitleFontWeight = "Normal"
  154.         Params.TitleFontWeight = "\" + CHR$(34) + Params.TitleFontWeight + "\" + CHR$(34)
  155.         SHELL$ = SHELL$ + " " + Params.TitleFontWeight
  156.     END IF
  157.  
  158.     IF Params.ContentFontWeight <> "" AND INSTR(Params.ContentFontWeight, "\" + CHR$(34)) = 0 THEN
  159.         Params.ContentFontWeight = "\" + CHR$(34) + Params.ContentFontWeight + "\" + CHR$(34) '$args[13]
  160.         SHELL$ = SHELL$ + " " + Params.ContentFontWeight
  161.     ELSE
  162.         Params.ContentFontWeight = "Normal"
  163.         Params.ContentFontWeight = "\" + CHR$(34) + Params.ContentFontWeight + "\" + CHR$(34)
  164.         SHELL$ = SHELL$ + " " + Params.ContentFontWeight
  165.     END IF
  166.  
  167.     IF Params.TitleTextForeground <> "" AND INSTR(Params.TitleTextForeground, "\" + CHR$(34)) = 0 THEN
  168.         Params.TitleTextForeground = "\" + CHR$(34) + Params.TitleTextForeground + "\" + CHR$(34) '$args[14]
  169.         SHELL$ = SHELL$ + " " + Params.TitleTextForeground
  170.     ELSE
  171.         Params.TitleTextForeground = "Black"
  172.         Params.TitleTextForeground = "\" + CHR$(34) + Params.TitleTextForeground + "\" + CHR$(34)
  173.         SHELL$ = SHELL$ + " " + Params.TitleTextForeground
  174.     END IF
  175.  
  176.     IF Params.ContentTextForeground <> "" AND INSTR(Params.ContentTextForeground, "\" + CHR$(34)) = 0 THEN
  177.         Params.ContentTextForeground = "\" + CHR$(34) + Params.ContentTextForeground + "\" + CHR$(34) '$args[15]
  178.         SHELL$ = SHELL$ + " " + Params.ContentTextForeground
  179.     ELSE
  180.         Params.ContentTextForeground = "Black"
  181.         Params.ContentTextForeground = "\" + CHR$(34) + Params.ContentTextForeground + "\" + CHR$(34)
  182.         SHELL$ = SHELL$ + " " + Params.ContentTextForeground
  183.     END IF
  184.  
  185.     IF Params.BorderBrush <> "" AND INSTR(Params.BorderBrush, "\" + CHR$(34)) = 0 THEN
  186.         Params.BorderBrush = "\" + CHR$(34) + Params.BorderBrush + "\" + CHR$(34) '$args[16]
  187.         SHELL$ = SHELL$ + " " + Params.BorderBrush
  188.     ELSE
  189.         Params.BorderBrush = "Black"
  190.         Params.BorderBrush = "\" + CHR$(34) + Params.BorderBrush + "\" + CHR$(34)
  191.         SHELL$ = SHELL$ + " " + Params.BorderBrush
  192.     END IF
  193.  
  194.     IF Params.TitleBackground <> "" AND INSTR(Params.TitleBackground, "\" + CHR$(34)) = 0 THEN
  195.         Params.TitleBackground = "\" + CHR$(34) + Params.TitleBackground + "\" + CHR$(34) '$args[17]
  196.         SHELL$ = SHELL$ + " " + Params.TitleBackground
  197.     ELSE
  198.         Params.TitleBackground = "White"
  199.         Params.TitleBackground = "\" + CHR$(34) + Params.TitleBackground + "\" + CHR$(34)
  200.         SHELL$ = SHELL$ + " " + Params.TitleBackground
  201.     END IF
  202.  
  203.     IF Params.ButtonTextForeground <> "" AND INSTR(Params.ButtonTextForeground, "\" + CHR$(34)) = 0 THEN
  204.         Params.ButtonTextForeground = "\" + CHR$(34) + Params.ButtonTextForeground + "\" + CHR$(34) '$args[18]
  205.         SHELL$ = SHELL$ + " " + Params.ButtonTextForeground
  206.     ELSE
  207.         Params.ButtonTextForeground = "Black"
  208.         Params.ButtonTextForeground = "\" + CHR$(34) + Params.ButtonTextForeground + "\" + CHR$(34)
  209.         SHELL$ = SHELL$ + " " + Params.ButtonTextForeground
  210.     END IF
  211.  
  212.     IF Params.Sound <> "" AND INSTR(Params.Sound, "\" + CHR$(34)) = 0 THEN
  213.         Params.Sound = "\" + CHR$(34) + Params.Sound + "\" + CHR$(34) '$args[19]
  214.         SHELL$ = SHELL$ + " " + Params.Sound
  215.     ELSE
  216.         Params.Sound = "Windows Notify System Generic"
  217.         Params.Sound = "\" + CHR$(34) + Params.Sound + "\" + CHR$(34)
  218.         SHELL$ = SHELL$ + " " + Params.Sound
  219.     END IF
  220.  
  221.     IF Params.CustomButton1 <> "" AND INSTR(Params.CustomButton1, "\" + CHR$(34)) = 0 THEN
  222.         Params.CustomButton1 = "\" + CHR$(34) + Params.CustomButton1 + "\" + CHR$(34) '$args[20]
  223.         SHELL$ = SHELL$ + " " + Params.CustomButton1
  224.     END IF
  225.  
  226.     IF Params.CustomButton2 <> "" AND INSTR(Params.CustomButton2, "\" + CHR$(34)) = 0 THEN
  227.         Params.CustomButton2 = "\" + CHR$(34) + Params.CustomButton2 + "\" + CHR$(34) '$args[21]
  228.         SHELL$ = SHELL$ + " " + Params.CustomButton2
  229.     END IF
  230.  
  231.     IF Params.CustomButton3 <> "" AND INSTR(Params.CustomButton3, "\" + CHR$(34)) = 0 THEN
  232.         Params.CustomButton3 = "\" + CHR$(34) + Params.CustomButton3 + "\" + CHR$(34) '$args[22]
  233.         SHELL$ = SHELL$ + " " + Params.CustomButton3
  234.     END IF
  235.  
  236.     SHELL$ = SHELL$ + ";exit $LASTEXITCODE" + CHR$(34)
  237.     a = _SHELLHIDE(SHELL$)
  238.  
  239.     WPFMessageBox = a
« Last Edit: February 22, 2020, 09:32:31 pm by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: WPF Message Boxes in QB64!
« Reply #1 on: February 21, 2020, 08:17:13 pm »
Hi SpriggsySprings,

Welcome to the forum and a fan of the messagebox!

I tried one too, all in QB64:
https://www.qb64.org/forum/index.php?topic=1511.msg107661#msg107661

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: WPF Message Boxes in QB64!
« Reply #2 on: February 21, 2020, 08:21:29 pm »
Thanks! Your code output looks really cool!
Shuwatch!

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: WPF Message Boxes in QB64!
« Reply #3 on: February 21, 2020, 11:49:48 pm »
Thank you for the new message box method. Really cool.

Also, Welcome :-)
In order to understand recursion, one must first understand recursion.