Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi Constant expression expected (https://www.delphipraxis.net/200712-constant-expression-expected.html)

EWeiss 17. Mai 2019 18:55


Constant expression expected
 
Mein Problem ist folgendes

Ich schicke von außen also aus der Anwendung eine ID für meine Combobox.

Innerhalb der DLL möchte ich diese nun in einer Case Anweisung auswerten.
Nun kommt oben genannter Fehler!

Wie kann ich diese trotzdem als Constante übergeben ?

In der Anwendung..
Delphi-Quellcode:
  ID_COLORPICK = WM_APP + 119; // ColorPicker auf TAB 1

Delphi-Quellcode:
    HCombo := CreateWindowEx(0, 'COMBOBOX', PWideChar(ColorLabel), LStyle,
      Width - 24, 0, 1, 152, FHColorPicker, DlgItemID, SkinEngine.skInstance,
      nil);
DlgItemID ist dann die Constante.. Die will die Case Anweisung aber nicht!

gruss

hoika 17. Mai 2019 21:50

AW: Constant expression expected
 
Hallo,
die Konstante wird ja durch die Übergabe als Variable ja zu eben dieser,
also zu einer Variable.

Es klappt also nur
if statt case.

EWeiss 17. Mai 2019 21:56

AW: Constant expression expected
 
Zitat:

Zitat von hoika (Beitrag 1432469)
Hallo,
die Konstante wird ja durch die Übergabe als Variable ja zu eben dieser,
also zu einer Variable.

Es klappt also nur
if statt case.

Ok Danke dann muss ich wohl IF nutzen.. ;)

gruss

peterbelow 17. Mai 2019 22:20

AW: Constant expression expected
 
Zitat:

Zitat von EWeiss (Beitrag 1432457)
Mein Problem ist folgendes

Ich schicke von außen also aus der Anwendung eine ID für meine Combobox.

Innerhalb der DLL möchte ich diese nun in einer Case Anweisung auswerten.
Nun kommt oben genannter Fehler!

Wie kann ich diese trotzdem als Constante übergeben ?

In der Anwendung..
Delphi-Quellcode:
  ID_COLORPICK = WM_APP + 119; // ColorPicker auf TAB 1

Delphi-Quellcode:
    HCombo := CreateWindowEx(0, 'COMBOBOX', PWideChar(ColorLabel), LStyle,
      Width - 24, 0, 1, 152, FHColorPicker, DlgItemID, SkinEngine.skInstance,
      nil);
DlgItemID ist dann die Constante.. Die will die Case Anweisung aber nicht!

gruss

Dann hast Du das falsch aufgesetzt. ein

Delphi-Quellcode:
  case DlgItemID of
    ID_COLORPICK: ....
  end;
funktioniert auf jeden Fall. Falls Du versucht haben solltest, DlgItemID als einen der Selektoren zu verwenden brauchst Du definitiv mehr Schlaf, das wäre ein übler neuronaler Kurzschluß :).

EWeiss 17. Mai 2019 22:23

AW: Constant expression expected
 
Delphi-Quellcode:
case DlgItemID of
funktioniert nicht!
DlgItemID wird aus der Anwendung an die DLL geschickt und dann wird aus DlgItemID eine Variable.
So wie @hoika schon sagte.

so geht's.

Delphi-Quellcode:
property DlgItemID: Integer read GetDlgItemID write SetDlgItemID;
Delphi-Quellcode:
              if (PDis^.CtlID = DWORD(SkinColorPicker.DlgItemID)) then
                  begin
                    case PDis^.itemAction of

                      ODA_DRAWENTIRE:
                        begin
                          if (PDis.itemAction and ODA_DRAWENTIRE) = ODA_DRAWENTIRE then
                          begin
so war es vorher
Delphi-Quellcode:
              case PDis^.CtlID of
                SkinColorPicker.DlgItemID: // error weil Variable Constant expression expected
                  begin
                    case PDis^.itemAction of

                      ODA_DRAWENTIRE:
                        begin
                          if (PDis.itemAction and ODA_DRAWENTIRE) = ODA_DRAWENTIRE then
                          begin
gruss

peterbelow 17. Mai 2019 22:30

AW: Constant expression expected
 
Zitat:

Zitat von EWeiss (Beitrag 1432474)
Delphi-Quellcode:
case DlgItemID of
funktioniert nicht!
DlgItemID wird aus der Anwendung an die DLL geschickt und dann wird aus DlgItemID eine Variable.

gruss

Das spielt keine Rolle, solange die Variable ein ordinal type ist. Das Case-Statement will nur in den einzelnen case-Werten Konstanten haben, der Wert, der ausgewertet werden soll kann aus einer Variablen kommen. Aber es muß halt eine Variable von einem Typ sein, auf den die Ord-Funktion angewendet werden kann (also Boolean, enumerated type, ganze Zahlen).

EWeiss 17. Mai 2019 22:33

AW: Constant expression expected
 
Zitat:

Zitat von peterbelow (Beitrag 1432475)
Zitat:

Zitat von EWeiss (Beitrag 1432474)
Delphi-Quellcode:
case DlgItemID of
funktioniert nicht!
DlgItemID wird aus der Anwendung an die DLL geschickt und dann wird aus DlgItemID eine Variable.

gruss

Das spielt keine Rolle, solange die Variable ein ordinal type ist. Das Case-Statement will nur in den einzelnen case-Werten Konstanten haben, der Wert, der ausgewertet werden soll kann aus einer Variablen kommen. Aber es muß halt eine Variable von einem Typ sein, auf den die Ord-Funktion angewendet werden kann (also Boolean, enumerated type, ganze Zahlen).

Siehe mein Edit..

Und wie schon gesagt die DLL kennt die Konstante ID_COLORPICK: nicht!
Die wird in der Anwendung erstellt.

Und DlgItemID = ID_COLORPICK wird zur DLL geschickt.

gruss

peterbelow 18. Mai 2019 10:48

AW: Constant expression expected
 
Zitat:

Zitat von EWeiss (Beitrag 1432476)
Zitat:

Zitat von peterbelow (Beitrag 1432475)
Zitat:

Zitat von EWeiss (Beitrag 1432474)
Delphi-Quellcode:
case DlgItemID of
funktioniert nicht!
DlgItemID wird aus der Anwendung an die DLL geschickt und dann wird aus DlgItemID eine Variable.

gruss

Das spielt keine Rolle, solange die Variable ein ordinal type ist. Das Case-Statement will nur in den einzelnen case-Werten Konstanten haben, der Wert, der ausgewertet werden soll kann aus einer Variablen kommen. Aber es muß halt eine Variable von einem Typ sein, auf den die Ord-Funktion angewendet werden kann (also Boolean, enumerated type, ganze Zahlen).

Siehe mein Edit..

Und wie schon gesagt die DLL kennt die Konstante ID_COLORPICK: nicht!
Die wird in der Anwendung erstellt.

Und DlgItemID = ID_COLORPICK wird zur DLL geschickt.

gruss

Wenn die DLL nicht weis, was die Inhalte von DlgItemID bedeuten, was soll sie dann damit anfangen? Du hast da einen Fehler im Design. Normalerweise macht man das so, dass man eine Unit erstellt, die in ihrem Interface-Teil die Konstanten und eventuell Typen (z. B. enumerated type) deklariert, die sowohl die DLL als auch die Hostapplikation kennen müssen. Diese Unit benutzt man dann in beiden Projekten.

EWeiss 18. Mai 2019 11:03

AW: Constant expression expected
 
Zitat:

Du hast da einen Fehler im Design.
Ich denke du hast einen Fehler im Verständnis. ;)

Das Problem ist bereits behoben.

Du siehst die DLL weis sehr wohl was DlgItemID darstellt.
Aber extra nochmal für dich!
ANWENDUNG
Delphi-Quellcode:
{$REGION 'Interface ISkinColorPicker'}
  ISkinColorPicker = interface
    ['{EF99A6D4-16F9-4675-B006-CF7D0AAA0065}']
    {$REGION 'Getter+Setter'}
    function GetHandle: HWND;
    function GetHeight: Integer;
    procedure SetHeight(const Value: Integer);
    function GetLeft: Integer;
    procedure SetLeft(const Value: Integer);
    function GetTop: Integer;
    procedure SetTop(const Value: Integer);
    function GetWidth: Integer;
    procedure SetWidth(const Value: Integer);
    function GetDlgItemID: DWord;
    procedure SetDlgItemID(const Value: DWord);
    function GetColorLabel: string;
    procedure SetColorLabel(const Value: string);
    function GetUseColor: string;
    procedure SetUseColor(const Value: string);
    function GetDropDownImg: WideString;
    procedure SetDropDownImg(const Value: WideString);
    function GetColorImg: WideString;
    procedure SetColorImg(const Value: WideString);
    function GetPipetteCursor: WideString;
    procedure SetPipetteCursor(const Value: WideString);
    {$ENDREGION}
    function CreateWindow(ParentHandle: hWnd): HWND;
    procedure DestroyWindow;
    property Handle: HWND read GetHandle;
    property Left: Integer read GetLeft write SetLeft;
    property Top: Integer read GetTop write SetTop;
    property Width: Integer read GetWidth write SetWidth;
    property Height: Integer read GetHeight write SetHeight;
    property DlgItemID: DWord read GetDlgItemID write SetDlgItemID;
    property ColorLabel: string read GetColorLabel write SetColorLabel;
    property UseColor: string read GetUseColor write SetUseColor;
    property DropDownImg: WideString read GetDropDownImg write SetDropDownImg;
    property ColorImg: WideString read GetColorImg write SetColorImg;
    property PipetteCursor: WideString read GetPipetteCursor write SetPipetteCursor;
  end;
{$ENDREGION}
Delphi-Quellcode:
// ColorPicker
function CTRL_ColorPickerCreate(): ISkinColorPicker; stdcall; external dllfile;
Delphi-Quellcode:
        ColorPicker := CTRL_ColorPickerCreate;
        ColorPicker.ColorLabel := SKAERO_COLORLABEL;
        ColorPicker.Left := 20;
        ColorPicker.Top := 130;
        ColorPicker.Width := 200;
        ColorPicker.Height := 21;
        ColorPicker.DlgItemID := ID_COLORPICK; // SIEHE hier!
        SKAERO_SplitColorARGB(SKAERO_AEROCOLOR, Alpha, Red, Green, Blue);
        ColorPicker.UseColor := SKAERO_RGBtoHex(RGB(Red, Green, Blue));
        ColorPicker.DropDownImg := SKAERO_FOLDER + 'ColorPicker\BTN_DropDown.png';
        ColorPicker.ColorImg := SKAERO_FOLDER + 'ColorPicker\Color190.png';
        ColorPicker.PipetteCursor := SKAERO_FOLDER + 'ColorPicker\pipette.cur';
        ColorPicker.CreateWindow(TabHandle);
        SKAERO_SetAnchorMode(ColorPicker.Handle, ANCHOR_CENTER_HORZ_BOTTOM);
        SKAERO_SetZorder(ColorPicker.Handle, HWND_TOP);
DLL
Delphi-Quellcode:
function TSkinColorPicker.CreateWindow(ParentHandle: HWND): HWND;
var
  Font: HFont;
  SetItem, K: Integer;
  R, G, B, R1, G1, B1: Byte;
  Count: Integer;
  CLName, Color: string;
  Parse, ColorName: TSplitStrArray;
begin

  FHColorPicker := CreateWindowX(0, '/', WS_CHILD or WS_VISIBLE, Left, Top,
    Width, Height, ParentHandle, @ColorProc, DlgItemID);

  FParentHandle := ParentHandle;
  if FHColorPicker <> 0 then
  begin
    Img := SkinEngine.AddResource(ParentHandle, PWideChar(FColorImg));

    LStyle := WS_CHILD {or WS_VISIBLE} or WS_TABSTOP or CBS_DROPDOWNLIST or
      CBS_HASSTRINGS or CBS_DISABLENOSCROLL or CBS_OWNERDRAWFIXED or WS_VSCROLL
      or CBS_NOINTEGRALHEIGHT;

    HCombo := CreateWindowEx(0, 'COMBOBOX', PWideChar(ColorLabel), LStyle,
      Width - 24, 0, 1, 152, FHColorPicker, DlgItemID, SkinEngine.skInstance,
      nil);

    Font := GetStockObject(ANSI_VAR_FONT);
    SendMessage(HCombo, WM_SETFONT, Font, 0);

    SkinColorPicker := self;

    if ColorLabel > '' then
    begin
      SetItem := -1;
      Parse := Split(ColorLabel, '_');
      Count := High(Parse);

      SkinEngine.SplitColorARGB(SkinEngine.SK_AEROCOLOR, Alpha, Red, Green, Blue);
      R1 := Red;
      G1 := Green;
      B1 := Blue;

      for K := 0 to Count - 1 do
      begin
        // Get Color Name
        ColorName := Split(Parse[K], ',');
        CLName := ColorName[Low(ColorName)];
        // Remove first ' " ' char
        if K = 0 then
          CLName := MidStr(CLName, 2, Length(CLName));

        SendMessage(HCombo, CB_ADDSTRING, 0, LParam(CLName));
        // Get Color Value
        Color := Trim(ColorName[High(ColorName)]);
        R := StrToInt('$' + Copy(Color, 2, 2));
        G := StrToInt('$' + Copy(Color, 4, 2));
        B := StrToInt('$' + Copy(Color, 6, 2));

        SendMessage(HCombo, CB_SETITEMDATA, K, LParam(RGB(R, G, B)));

        if (R = R1) and (G = G1) and (B = B1) then
          SetItem := K;
      end;
      if SetItem > -1 then
        SendMessage(HCombo, CB_SETCURSEL, SetItem, 0);
    end;

    if UseColor > '' then
    begin
      R := Byte(StrToInt('$' + Copy(UseColor, 2, 2)));
      G := Byte(StrToInt('$' + Copy(UseColor, 4, 2)));
      B := Byte(StrToInt('$' + Copy(UseColor, 6, 2)));
    end
    else
    begin
      R := 255;
      G := 255;
      B := 255;
    end;

    SkinEngine.SetProperty(FHColorPicker, COLORPICK_COLOR, SkinEngine.ColorARGB
        (255, RGB(R, G, B)));
    SkinEngine.SetProperty(FHColorPicker, COLORPICK_COMBO, HCombo);

    btnDropDown := TSkinPushButton.Create();
    btnDropDown.ImagePath := DropDownImg;
    btnDropDown.Text := '';
    btnDropDown.Left := FWidth - 24;
    btnDropDown.Top := 0;
    btnDropDown.Width := 24;
    btnDropDown.Height := Max(FHeight, 24);;
    btnDropDown.DlgItemID := ID_COLORPICKBUTTON;
    btnDropDown.TextCol := SKAERO_BTNTEXTCOLOR;
    btnDropDown.Label3D := 1;
    btnDropDown.TextAlligment := Center;
    btnDropDown.ImageStateMax := 0;
    btnDropDown.CreateWindow(FHColorPicker);
    SkinEngine.SetAnchorMode(btnDropDown.Handle, ANCHOR_NONE);
    SkinEngine.SetZorder(btnDropDown.Handle, HWND_TOP);
  end;

  Result := FHColorPicker;
end;
Delphi-Quellcode:
function ColorProc(WinHandle: HWND; Msg: UINT; wP: WParam; lP: LParam)
  : LRESULT; stdcall;
var
  PMis: PMeasureItemStruct absolute lP;
  PDis: PDrawItemStruct absolute lP;
  ClassName: array [0 .. 128] of Char;
  ps: TPaintstruct;
  hDCpaint, DC: HDC;
  wText: WideString;
  rc: TRect;
  imgW, imgH: Cardinal;
  p1, p2: TGPPoint;
  p: TPoint;
  strItem: string;
  Item: Integer;
  RGBcolor, Color: ColorRef;
begin
  FillChar(ClassName, SizeOf(ClassName), 0);

  case Msg of

    WM_MEASUREITEM:
      begin
        PMis := Pointer(lP);
        if PMis.CtlType = ODT_COMBOBOX then
        begin
          PMis.itemHeight := 16;
          Result := 1;
          Exit;
        end;
      end;

    WM_DRAWITEM:
      begin
        PDis := Pointer(lP);
        case PDis^.CtlType of

          ODT_COMBOBOX:
            begin

              SetBkMode(PDis.HDC, TRANSPARENT);
              if (PDis^.CtlID = SkinColorPicker.DlgItemID) then // Problem behoben! Was gibt es nun noch zu diskutieren?

Oh und Wunder es funktioniert sogar!

gruss


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:06 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz