Common Dialog Ex. w/ Win32 API menus (Lite-C pure)

Posted By: yorisimo

Common Dialog Ex. w/ Win32 API menus (Lite-C pure) - 09/20/07 18:45

Hope you find this useful! Now that I finally got the callbacks to work, I will use the win32 menu system instead of having to create Panels and buttons and all Plus, it's easy to make things look professional.
Code:
 
//Press the 'c' key, right click in the window, or click on file menu to bring up color selection dialog
#include <acknex.h>
#include <default.c> //optional
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
function SelectColor(COLOR* bgr);

//See MSDN (Visual Studio) documentation for info about the Flags and any win32 API functions)
//Make sure you use windows.h with CHOOSECOLOR declared (like in V7.06 beta)
//I couldn't get RGB(r,g,b) macros to work so I just used hex values (0x00BBGGRR) for colors
CHOOSECOLOR cc; //choosecolor struct for ChooseColor function
COLORREF customcolors[16]={0x000000fe,0x0000fe00,0x00fe0000,0x00fefe00,0x00fe00fe,0x0000fefe,0x00fefefe, 0, 0, 0, 0, 0, 0, 0, 0, 0};
COLORREF defaultcolor=0x00000000;

HMENU hMenu; //main menu
HMENU hSubMenu;//dropdown menu
HMENU hPopup; //popup menu on rightclick

POINT cursor; //point struct for cursor position

var setup_flag=ON;
var choose_flag=OFF;

function main()
{
wait(1);

COLOR usercolor;
hMenu=CreateMenu();
hSubMenu=CreateMenu();
hPopup=CreatePopupMenu();

InsertMenu(hMenu,0,MF_BYPOSITION|MF_STRING|MF_POPUP,hSubMenu,"File");
InsertMenu(hSubMenu,0,MF_BYPOSITION|MF_STRING,1,"Choose Color");
InsertMenu(hSubMenu,1,MF_BYPOSITION|MF_STRING,2,"QUIT");
InsertMenu(hPopup,0,MF_BYPOSITION|MF_STRING,3,"Choose Color");
InsertMenu(hPopup,1,MF_BYPOSITION|MF_STRING,4,"QUIT");

SetWindowLong(hWnd,GWL_WNDPROC, WndProc); //change the window procedure
//Change Window style to activate Maximize button
SetWindowLong(hWnd,GWL_STYLE, WS_CAPTION+WS_SYSMENU+WS_MINIMIZEBOX+WS_MAXIMIZEBOX+WS_SIZEBOX);
SetMenu(hWnd,hMenu); //activate the main menu
ShowWindow(hWnd,SW_SHOW);

while (1)
{
if(choose_flag){
if(SelectColor(&usercolor)) {vec_set(screen_color,usercolor);}
choose_flag=OFF;
}
wait(1);
}
}

function SelectColor(COLOR* bgr){
if(setup_flag==ON){ //initialize the struct if first time through
cc.lStructSize=sizeof(CHOOSECOLOR);
cc.hwndOwner=hWnd; //can be NULL (will not appear within engine window)
cc.hInstance=NULL;
cc.rgbResult=defaultcolor; //can be NULL (black)
cc.lpCustColors=customcolors; //may not be NULL!
cc.Flags=CC_RGBINIT;
cc.lCustData=NULL;
cc.lpfnHook=NULL;
cc.lpTemplateName=NULL;
setup_flag=OFF;
}

if(ChooseColor(&cc)){
bgr.blue=GetBValue(cc.rgbResult);
bgr.green=GetGValue(cc.rgbResult);
bgr.red=GetRValue(cc.rgbResult);
return(1);
}
return(0);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
switch(message){
case WM_RBUTTONUP:
GetCursorPos(&cursor); //or use GS cursor function
//Activate the popupmenu at the cursor location
TrackPopupMenu(hPopup,TPM_LEFTALIGN,cursor.x,cursor.y,0,hWnd,0);
break;
case WM_CHAR:
switch(wParam){
case 99: choose_flag=ON; break; //letter 'c'
}
case WM_COMMAND:
switch(wParam){
case 1: //main menu 'Coose Color'
case 3: choose_flag=ON; break;//popup menu 'Coose Color'
case 2: //main menu 'Quit'
case 4: sys_exit(NULL); //popup menu 'Quit'
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return(0);
}


Posted By: Shadow969

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C pure) - 09/21/07 04:00

Thanks again yorisimo!
gonna try it. but does it work after publishing?
Posted By: yorisimo

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 09/21/07 14:29

yes. you scared me there for a moment.

For others trying this, if you don't have access to the 7.06beta windows.h file, just add the following code at the end of your windows.h
Code:
 
//from windows.h V7.06.0 beta
typedef DWORD COLORREF;
typedef struct CHOOSECOLOR {
long lStructSize;
long hwndOwner;
long hInstance;
COLORREF rgbResult;
COLORREF *lpCustColors;
long Flags;
long lCustData;
long lpfnHook;
char* lpTemplateName;
} CHOOSECOLOR;

#define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))
#define GetRValue(rgb) ((BYTE)(rgb))
#define GetGValue(rgb) ((BYTE)(((WORD)(rgb)) >> 8))
#define GetBValue(rgb) ((BYTE)((rgb)>>16))

BOOL WINAPI ChooseColor(CHOOSECOLOR* lpcc);
#define PRAGMA_API ChooseColor;comdlg32.dll!ChooseColorA


Posted By: amadeu

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 09/21/07 21:00

hi

I tried to run your script but did nor work.
Appear this:
CreatePopupMenu(); wrong number/type of parameters

What is the right code?

thx
Posted By: yorisimo

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 09/25/07 15:43

this works for me, I don't know why you are getting this error. You might try editing windows.h line: "long WINAPI CreatePopupMenu(long);" to "long WINAPI CreatePopupMenu(void);"
as CreatePopupMenu does not have any inputs.
Posted By: yorisimo

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 09/28/07 15:37

when i updated to v7.06 beta i got this error, and my fix fixed it.

Note: you should avoid using the method I used here, and rather use ScanMessage. Changing the windows procedures prevents all the default things like keystrokes from being detected unless you code them yourself. See my post on ScanMessage in Lite-C Scripting/Win API with Lite-C pure mode
Posted By: ACKNEX007

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 09/28/07 19:00

it worked after adding the lines in windows.h as 'yorisimo' said.
Posted By: TripleX

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 09/28/07 21:41

fascinating.. would have been great for GameEdit.. And I've programmed all that damn [censored] with panels.. hmm

Thanks for the contrib, will maybe be a great possibility for future generators ^^
Posted By: D3D

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 10/12/07 23:54

Excellent contribution yorisimo, thanks for sharing!
Posted By: MrCode

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 10/14/07 06:13

But from a programming standpoint....isn't it easier just to use Acknex buttons and panels? I mean, what with all the instances and message handling with Windows... Ugh, no wonder I don't take interest in learning the Windows API.
Posted By: ventilator

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 10/18/07 09:30

thanks! this is very useful. it would be great if you could make more examples with other windows gui elements.

all editor tools like from triplex and shadow969 should use this!
Posted By: MrCode

Re: Common Dialog Ex. w/ Win32 API menus (Lite-C p - 10/19/07 22:04

Umm, just 2 clarify on what I posted earlier (my edit time expired):

I'm not knocking you contribution, I think it's great that you were able to work this out, and I would use it if I really needed it. I'm just saying that if I were programming this sort of stuff myself, it would be easier to use the acknex.dll functions. It's mostly a matter of skill level.
Posted By: yorisimo

Custom Dialog w/ Win32 API ? - 10/23/07 20:21

I've looked into making custom dialog boxes, (modal and modeless) but I'm not getting very far. It seems that the usual way to do this is using resources (*.rc, not to be confused with gamestudio's *.wrs). I'm not very familiar with resources and don't know how to use them or even if it's possible to use them with Lite-C.
It seems making custom dialog boxes should be possible using Lite-C as the functions CreateDialogParam and DialogBoxParam are included in windows.h.
Any ideas/suggestions?
Posted By: S3an

Re: Custom Dialog w/ Win32 API ? - 12/30/07 03:08

it is possible, although it takes alot of programming. You have to create the DLGTEMPLATE and DLGITEMTEMPLATE structures in memory. That means you must fill in a structure for each control on the dialog. Then pass the entire unified structure to CreateDialogIndirectParamA(...) and then call ShowWindowA(...) I'm sorry i don't have a 'C' code sample of how to do this, all i have is a BASIC version written in my own language, not much good if you aren't familiar with it.
Posted By: yorisimo

Re: Custom Dialog w/ Win32 API ? - 01/15/08 15:02

I have gotten my own modal and modeless dialog boxes working now (all in C, no resources). Thanks S3an for the reply though.
Posted By: raiden

Re: Custom Dialog w/ Win32 API ? - 03/16/08 14:41

First, thanks very much for this contribution

I noticed something however when running this in windowed mode, for me when I closed out the engine using the "X" in the top right corner, it seems as though the WndProc would not release causing the acknex.exe to continue to run. I checked System Processes in Task Manager to confirm this.

I added this case to the WndProc and it has corrected the issue:

Code:

case WM_DESTROY:
{
PostQuitMessage(0);
sys_exit(NULL);
return(0);
}



-raiden
© 2024 lite-C Forums