Win32 Api: How create a Color Dialog?

Dennis dkorpel at gmail.com
Thu Jan 9 21:55:26 UTC 2020


On Thursday, 9 January 2020 at 13:04:33 UTC, Marcone wrote:
> I am creating a GUI using winsamp.d as model.
> See the window here: https://i.ibb.co/ZJ4v2KD/Sem-t-tulo.png
> I want to ask a user for choose a color when click 
> Configuration/Color, and then change backgroud color of GUI. 
> But how can I create a Color Dialog? There's dlang example in 
> the web and I can not make c++ example work.

Windows has the 'common dialog' module with a ChooseColor 
function you can use.

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms646912(v=vs.85)

(The link is not clickable on the forum because of the closing 
bracket, you have to manually copy-paste it)

An example of how to use it can be found here:

https://github.com/adamdruppe/arsd/blob/fc896e74cb6939ef3d81d592045885fbe1cc6f98/minigui_addons/color_dialog.d#L14

Or try this (I am not on Windows currently so this isn't tested):
```
import core.sys.windows.windows;
pragma(lib, "comdlg32");

uint getColor(uint defaultColor) {
	COLORREF[16] acrCustClr; // array of custom colors

	CHOOSECOLOR cc;
	cc.lStructSize = cc.sizeof;
	cc.hwndOwner = null; // owner window, allowed to be NULL if no 
owner
	cc.lpCustColors = cast(LPDWORD) acrCustClr;
	cc.rgbResult = defaultColor;
	cc.Flags = CC_FULLOPEN | CC_RGBINIT;

	if (ChooseColor(&cc)) {
		return cc.rgbResult;
	} else {
		return 0; // user clicked cancel
	}
}

import std;

void main() {
     const color = getColor(0xFF0000FF); // default color red
     const red = color & 0xFF;
     const green = (color >> 8) & 0xFF;
     const blue = (color >> 16) & 0xFF;
     writeln("Selected color is (", red, ",", green, ",", blue, 
")");
}

```


More information about the Digitalmars-d-learn mailing list