Why color is not displayed correctly?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Why color is not displayed correctly?

Post by slishnevsky » 16 May 2024, 06:00

Code: Select all

Color_Primary := "5573B5"
Color_Danger := "BE5A52"
Color_Success := "7CBB5F"
; -------------------------------------------------------------------------------
; ShowMessageBox
; -------------------------------------------------------------------------------
ShowMessageBox(message, stay := 0, color := Color_Primary, interval := 1000) {
  MyGui := Gui("-ToolWindow +Border -Caption +AlwaysOnTop")
  MyGui.BackColor := color
  MyGui.SetFont("s20 cWhite", "Bahnschrift")
  MyGui.AddText("Center", message)
  MyGui.Show()
  if (stay = 0)
    SetTimer(() => MyGui.Hide(), interval)
  Sleep(1000)
}
image.png
image.png (51.62 KiB) Viewed 140 times
Color_Success is Green

When I call

Code: Select all

ShowMessageBox("Task completed", Color_Success, 2000)
I get a Blue color


image.png
image.png (98.56 KiB) Viewed 140 times

User avatar
mikeyww
Posts: 27344
Joined: 09 Sep 2014, 18:38

Re: Why color is not displayed correctly?

Post by mikeyww » 16 May 2024, 06:06

Hello,

Although you can omit an optional parameter in your function call, you cannot omit the comma that marks its place. This is explained in the documentation as follows.
It is not possible to have optional parameters isolated in the middle of the parameter list. In other words, all parameters that lie to the right of the first optional parameter must also be marked optional. However, optional parameters may be omitted from the middle of the parameter list when calling the function, as shown below:

Code: Select all

MyFunc(1,, 3)
MyFunc(X, Y:=2, Z:=0) {  ; Note that Z must still be optional in this case.
    MsgBox X ", " Y ", " Z
}
Source: Functions - Definition & Usage | AutoHotkey v2
A demonstration is below (for your script).

Code: Select all

ShowMessageBox("Task completed", STAY := Color_Success, COLOR := 2000)

slishnevsky
Posts: 34
Joined: 07 Mar 2024, 06:50

Re: Why color is not displayed correctly?

Post by slishnevsky » 16 May 2024, 08:43

Oh, true.
I forgot about the stay parameter, missed it. My fault. Thank you!

The stay parameter is actually redundant, I planned to remove it, but forgot :)

Post Reply

Return to “Ask for Help (v2)”