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:
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);
}