Pelles C forum
C language => Beginner questions => Topic started by: goom on March 16, 2011, 04:38:22 PM
-
I see there is a resource editor but I am really new to
this and see there isn't much online documentation
or tutorial on how you create a resource and then
use it (while using Pelles C). Anyone know where
I should start
-
Here is where you can start :
http://msdn.microsoft.com/en-us/library/feh4ww6k.aspx
-
Anyone know where I should start
You can read up on resources, that's a pretty helpful thing.
But just for the sake of getting you going....
Create a new project (gui or console) and get your main page running...
"Hello world" is fine...
Now select an icon (*.ico) for your program and place it in the folder with your source code.
Next click New -> Resources
You should see an untitled page open...
Right click on untitled and select Import
Select your icon and click OK.
Then save the tab as "resources".
When you compile the icon is included into the file and you can see it in explorer.
Now that you have resources in your project you can expand them as much as you like
You simple right click, New - > Dialog and you can make your own dialog boxes
IOW... mess around with it a bit... It's not that hard to figure out.
-
thanks @CommonTater ,I managed with a cursor and a icon(I created a header file),but how make a menu?to understand,I want to do menu manually...can you tell me how?
-
Read this:
http://www.winprog.org/tutorial/menus.html (http://www.winprog.org/tutorial/menus.html)
PellesC have good resource editor, learn to use it.
Example of dynamic menu.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commctrl.h>
//#pragma lib "comctl32.lib"
#define IDM_EXIT 6000
#define IDM_OPEN 6001
#define IDC_STATUS 4000
#define IDC_TOOLBAR 4001
#ifndef NELEMS
#define NELEMS(a) (sizeof(a) / sizeof(a[0]))
#endif
LRESULT WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
void OnDestroy(HWND hwnd);
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
void OnSize(HWND hwnd, UINT state, int cx, int cy);
TCHAR szAppName[] = TEXT("WSDIFrame");
TCHAR szFrameClass[] = TEXT("cWSDIFrame");
HWND hFrame, hStatus, hToolbar;
HANDLE hInst;
HMENU hMenu, hMenuT;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcx;
MSG msg;
wcx.cbSize = sizeof(WNDCLASSEX);
//GetClassInfoEx(hInstance, MAKEINTRESOURCE(32770), &wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = (WNDPROC) WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
//wcx.hbrBackground = (HBRUSH)COLOR_WINDOW;
wcx.hbrBackground= (HBRUSH)COLOR_APPWORKSPACE+1;
wcx.lpszMenuName = NULL;
wcx.lpszClassName= szFrameClass;
wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wcx))
return 0;
hInst = hInstance;
hFrame = CreateWindowEx(0, szFrameClass, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
if(!hFrame) return 0;
ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
HWND MakeToolbar(HWND hWnd, UINT wID)
{
TBBUTTON tbb[] = {
//{STD_, IDM_, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, 0},
//{STD_FILENEW, IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
};
HWND hToolbar = CreateToolbarEx(hWnd,
WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT
, wID, 0, HINST_COMMCTRL, IDB_STD_SMALL_COLOR,
tbb, NELEMS(tbb), 100, 0, 30, 30, sizeof(TBBUTTON));
return hToolbar;
}
HWND MakeStatusbar(HWND hWnd, UINT wID)
{
HWND hStatus = CreateStatusWindow(WS_CHILD | WS_VISIBLE, // | SBARS_SIZEGRIP,
NULL, hWnd, wID);
return hStatus;
}
HMENU MakeMenubar(HWND hWnd)
{
HMENU hMenu, hMenuT;
hMenu = CreateMenu();
hMenuT = CreatePopupMenu();
AppendMenu(hMenuT, MF_ENABLED | MF_STRING, IDM_OPEN, TEXT("&Open"));
AppendMenu(hMenuT, MF_ENABLED | MF_STRING, IDM_EXIT, TEXT("&Exit"));
AppendMenu(hMenu, MF_ENABLED | MF_POPUP, (UINT)hMenuT, TEXT("&File"));
SetMenu(hWnd, hMenu);
return hMenu;
}
LRESULT WINAPI WndProc(HWND hwnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch(wMsg) {
case WM_COMMAND:
return OnCommand(hwnd,(int)(LOWORD(wParam)),(HWND)(lParam),(UINT)HIWORD(wParam)),0;
case WM_SIZE:
return OnSize(hwnd,(UINT)(wParam),(int)(short)LOWORD(lParam),(int)(short)HIWORD(lParam)),0;
case WM_CREATE: return OnCreate(hwnd,(LPCREATESTRUCT)(lParam)),0;
case WM_DESTROY: return OnDestroy(hwnd),0;
default:
return DefWindowProc(hwnd, wMsg, wParam, lParam);
}
}
BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
InitCommonControls();
hToolbar = MakeToolbar(hwnd, IDC_TOOLBAR);
hStatus = MakeStatusbar(hwnd, IDC_STATUS);
hMenu = MakeMenubar(hwnd);
return 0;
}
void OnDestroy(HWND hwnd)
{
PostQuitMessage(0);
}
void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id) {
case IDM_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0L);
return;
}
}
void OnSize(HWND hwnd, UINT state, int cx, int cy)
{
SendMessage(hToolbar, WM_SIZE, state, cx);
SendMessage(hStatus, WM_SIZE, state, cx);
}
-
you must add ,in your project,all files(.h and .rc)?not enough to write include "resource.h" ?
-
You must insert .rc file to project where menu recource is.
After that you can edit resource with resource-editor.
From IDE menu File -> New -> Resources you can insert empty resouce to project.
-
So better,I'm learning how to make a dynamic menu?
-
I would start with a static one to get a feeling, then you can advance to the dynamic one.
-
thanks @CommonTater ,I managed with a cursor and a icon(I created a header file),but how make a menu?to understand,I want to do menu manually...can you tell me how?
Y'know what... read the help file under "Integrated Environment" everything is there.
To ad a menu to your resources simply open the resource you created in my first example and right click on it... Then click New and Menu... That will open the menu editor.
Really this isn't all that hard... You just gotta be a little bit experimental... cllick on stuff, right click on stuff, explore! When in doubt read the help file... Pelles help file is one of the best I've ever seen.
-
When you say that everything is in the help file, you are not entirely correct. For example, the Menu resource is very easy to manipulate but nowhere does it point out that one has to add "lpszMenuName = MAKEINTRESOURCE(IDR_MAIN_MENU);" to actually make it appear on the dialog. I found this information in a book that I just happen to have.
My problem now is that although I can use the resource editor to create a ToolBar, when I run the program, it doesn't show on the dialog. Unfortunately, in this case, there is no parameter in WNDCLASSEX for it to go. It appears in the TEXT version of the resource file just like all the other controls (which do appear and work OK) but is invisible when the program runs. Is there something that needs to be added to make it show up? (In Window styles, Visible is set to Yes).
Online articles say that one has to create the toolbar from scratch but there is a ton of stuff in the commdlg header file which seems to me to have done all of that work for us and we just need to access it somehow.
Any help would be appreciated.
John.
-
When you say that everything is in the help file, you are not entirely correct. For example, the Menu resource is very easy to manipulate but nowhere does it point out that one has to add "lpszMenuName = MAKEINTRESOURCE(IDR_MAIN_MENU);" to actually make it appear on the dialog. I found this information in a book that I just happen to have.
Because you don't have to do that. You can name your menu "MAINWM" (or whatever you choose) then when registering your windclass just use it's text name.... lpszMenuName = "MAINWM";
In fact you can do that with any resource... Just right click the resource, select properties and give it a text name in quotes. (See the attached image below)
My problem now is that although I can use the resource editor to create a ToolBar, when I run the program, it doesn't show on the dialog. Unfortunately, in this case, there is no parameter in WNDCLASSEX for it to go. It appears in the TEXT version of the resource file just like all the other controls (which do appear and work OK) but is invisible when the program runs. Is there something that needs to be added to make it show up? (In Window styles, Visible is set to Yes).
Toolbars are created as child windows, just like EDIT or BUTTON controls. Before you're going to see it you need to add your buttons, separators etc to it. This would typically be done during WM_INITDIALOG or WM_CREATE.
Online articles say that one has to create the toolbar from scratch but there is a ton of stuff in the commdlg header file which seems to me to have done all of that work for us and we just need to access it somehow.
'Fraid not... To create a toolbar you will need to load your button images (or you can use the defaults) and then add each button to the toolbar manually.
-
Example for WM_INITDIALOG
FillToolbar(hwndDlg, 4001);
void FillToolbar(HWND hWndDlg, int iCtl)
{
TBBUTTON tbb[] = {
{STD_FILENEW, IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON},
{STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON},
};
HWND hToolbar = GetDlgItem(hWndDlg, iCtl);
if (hToolbar) {
// Load the button images.
SendMessage(hToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);
// Add buttons.
SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hToolbar, TB_ADDBUTTONS, (WPARAM)NELEMS(tbb), (LPARAM)&tbb);
}
}
-
Thanks Timo... I was looking for an example in my own code but they were all far more complex than yours.
-
Many many thanks Timo; I would never have found that out by myself.
May I be cheeky and ask another question? Where are the standard bitmaps for the buttons stored?
John.
-
In comctl32.dll resources
small bitmap 120
bigger bitmap 121
HBITMAP hBitmap = LoadBitmap(GetModuleHandle(TEXT("comctl32")), MAKEINTRESOURCE(120));
Bitmap from resource with 2 buttons
void FillToolbar(HWND hWndDlg, int iCtl)
{
TBBUTTON tbb[] = {
{0, IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON},
{1, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON},
};
HWND hToolbar = GetDlgItem(hWndDlg, iCtl);
if (hToolbar) {
// Load the button images.
TBADDBITMAP tbBitmap;
tbBitmap.hInst = GetModuleHandle(NULL);
tbBitmap.nID = IDR_TOOLBAR1;
SendMessage(hToolbar, TB_ADDBITMAP, 2, (WPARAM)&tbBitmap);
//SendMessage(hToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);
// Add buttons.
SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hToolbar, TB_ADDBUTTONS, (WPARAM)NELEMS(tbb), (LPARAM)&tbb);
}
}
EDIT:
If need ToolTips insert TBSTYLE_TOOLTIPS|TBSTYLE_LIST to resource
and use this function:
void FillToolbar(HWND hWndDlg, int iCtl)
{
TBBUTTON tbb[] = {
{STD_FILENEW, IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, (INT_PTR)"New"},
{STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, (INT_PTR)"Open"},
};
HWND hToolbar = GetDlgItem(hWndDlg, iCtl);
if (hToolbar) {
// Load the button images.
SendMessage(hToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);
// Add buttons.
SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hToolbar, TB_ADDBUTTONS, (WPARAM)NELEMS(tbb), (LPARAM)&tbb);
SendMessage(hToolbar, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_MIXEDBUTTONS);
}
}
-
I think you should start at reading through the toolbar section at MSDN (http://msdn.microsoft.com/en-us/library/bb760435%28VS.85%29.aspx).
-
Thank you once again Timovjl. This time, not only did you answer my question but you did so even without me having to ask it. I am very grateful for the 'tooltip' edit that you made to your original post.
The beauty of your program snippets is that they are concise and they tie in beautifully with the Pelles IDE.
I am a C programmer of old but recently have been using Visual Basic Express. The problem with VB is that the programs are messy to disseminate due to the .NET runtime. With Pelles C, the programs I write can be passed on simply and even run as 'no-install' applications.
Many thanks for your help.
John.
-
An old topic, but I think there is no need for a new one.
I don't seem to find how to disable snapping when creating/moving controls let's say inside a dialog parent. They always get snapped to the grid.
I did a quick search on the net, the forum and the help files, but I don't seem to find what I'm looking for.
Is there a command to disable snapping in the Pelles C resource-editor?
-
Tools -> Options -> Dialog -> Use grid
-
Thank you!
PS. It would be useful if this could be also addressed with the use of any of the Alt/Ctr/Shift keys. Perhaps in a future version. Thanks again!
-
Thank you!
PS. It would be useful if this could be also addressed with the use of any of the Alt/Ctr/Shift keys. Perhaps in a future version. Thanks again!
Tools -> Customize -> Keyboard -> Dialog
However the grid setting is global, if you enable it *all* controls will snap to their nearest gridline.
-
Hello folks, loooooong time no see! Hope everyone is doing fine!
An ancient topic but I thought there is no need to start a new one. I just D/Led Pelles C v10.00.6 and playing a bit with the resource editor. I think the Properties dialog used to show brief hints for the selected property, at the bottom of the dialog. It has been removed or is it an option I can't find?
I mean this dialog: http://prntscr.com/tx7dd4 (shouldn't a brief hint about the selected "Group" property show at the bottom of the dialog)?
-
I don't recall seeing that in Pelles C. VB has it.
John Z