NO

Author Topic: Hello everybody  (Read 5378 times)

ResourceHog

  • Guest
Hello everybody
« on: May 21, 2012, 02:33:24 PM »
Hi there. I've been programming C since the 90's in control and instrumentation using LabWindows/CVI if anybody knows it, but have come late to 'proper' Windows programming. First impression is that an astonishing amount of work has gone into this to give away for free, so many thanks Pelle :)

Second impression is that it's all very 'bitty' and disconnected (not Pelle's fault, VC++ is the same). I'm used to putting a control on a dialog and defining its associated callback in the properties box. Resource files are a mystery to me. Oh well, see how I get on.

Has anyone got a full-blown demo program that demonstrates all the main features, or is that a 'piece-of-string' question?

CommonTater

  • Guest
Re: Hello everybody
« Reply #1 on: May 21, 2012, 03:39:06 PM »
Hello ResourceHog, and welcome to the forums.
 
One of the best things about Pelles C is that it's real nuts and bolts programming. Nothing is done for you, so you have total control over everything.  No RAD crap here, folks. 
 
Windows GUI programming is message driven which is likely to be quite different from what you're used to.  Basically you need to construct an entry point, register a class, create your main window, set up a message tosser to handle messages and a message loop to retrieve them from the queue.  This may at first seem rather tedious and overly complex, but once you see how remarkably efficient it is, you'll probably agree it's worth the effort.
 
Resource files are merely data stored in a special section of a windows program.  It lets you store the configuration for a dialog, menu, etc. in a script like form that is loaded from the program image by your code during use. You can also store groups of strings, error messages, icons, pictures, sounds, even videos in  there for use by your program. 
 
The general strategy for all but the simplest of programs is to hand-code your main window using the CreateWindow() and CreateWindowEx() API calls and then place your smaller pop-up windows in resources or use the pre-fab windows dialogs when appropriate.  While it is possible to build dialog based programs, this isn't always the best way since it can impose some limitations upon what you can do. 
 
I'm attaching a very simple (and ugly) unicode editor example you can load up and study. It will show you some of the basics of making a window, handling messages, resizing, etc. as well as opening and saving files. 
 
There is an excellent tutorial --> HERE <-- and I suggest that no matter your skill level, if this is your first encounter with windows, you should play beginner and follow it step by step.
 
You may also want to get a localized copy of the Windows API Reference which you can download --> HERE <-- You will find this to be beyond useful.  There are more than 20,000 API calls, don't even pretend you can memorize them all :D
 
If your preference is for online lookups (  :-\  ) and are using ver7.00 you can install this  --> AddIn <-- which will let you do msdn and web keyword lookups by pressing F1 on your keyboard.
 
 
« Last Edit: May 21, 2012, 03:54:11 PM by CommonTater »

ResourceHog

  • Guest
Re: Hello everybody
« Reply #2 on: May 21, 2012, 03:53:14 PM »
That's an impressive welcome, cheers!  ;D

Will checkout the links and the sample, I can feel a steep learning curve coming on. Currently fiddling with the inital dialog box sample, even that's defeating me. If you create a dialog then delete it, it still exists in memory and insists on recreating the old one when you want a nice clean start. Renaming is a PITA, the header file seems to never be rebuild, so the new dialog won't compile or retains the old numeric ID and whinges. Editing the .h file appears to destroy the .rc file. I'm sure I'll get the hang of it though.

Btw, since VC++ is available free, what are the benefits of using Pelle's C?

CommonTater

  • Guest
Re: Hello everybody
« Reply #3 on: May 21, 2012, 04:19:07 PM »
Currently fiddling with the inital dialog box sample, even that's defeating me. If you create a dialog then delete it, it still exists in memory and insists on recreating the old one when you want a nice clean start. Renaming is a PITA, the header file seems to never be rebuild, so the new dialog won't compile or retains the old numeric ID and whinges. Editing the .h file appears to destroy the .rc file. I'm sure I'll get the hang of it though.

In my experience, the resource editor in Pelles C is just a tad clumsy at first but once you get the hang of it, it's actually very easy to work with. 
 
If you are going to delete a dialog from your resources do it from the .RC file, not by wiping out the controls and starting over.  That is don't clear the dialog's face... destroy the entire dialog.
 
Also, I don't tend to use header files with my .rc files.  I just give things real names in quotes--"MAINMENU" instead of #2000, "ASKUSER" instead of #4000, etc.  Thus you don't need any macros in your api calls and you don't need the header files... just use the name of the dialog, menu, icon, etc.
 
An example of a dialog based program using this technique is attached...
Also TheForger's tutorial should clarify a lot of these issues for you.

Quote
Btw, since VC++ is available free, what are the benefits of using Pelle's C?

Oh my, where to begin...
 
1) Standards... Pelles C tries to adhere to the most recent coding standards for C, Version 7.00 upgrades the compiler and libraries to C-11, from C-99... neither of which have ever been supported by VC++ which is proprietary to Microsoft.  This means you can write code that will compile correctly on other standards based compilers... try that with VC++...

2) Nuts and Bolts... As I noted in the edit to my first reply, Pelles C lays it all out there for you, nothing is hidden behind RAD tools or Visual Designers.  While at first this seems a shortcomming, it actually is an advantage because it gives you far more control over the code you write.  You know everything about your programs, nothing is left to chance.
 
3) The Help file... Seriously... spend some time exploring the help file. Everything --and I do mean everything-- you need to know about Pelles C and it's libraries is in there.
 
4) Support... As I'm guessing your going to find out over the next while, Pelle is one darned smart guy... and these forums are filled with some of the best programmers around.  (Most of them make me look like a beginner!)
 
5) Resources ... VC++ free versions do not include resource editors resulting in some of the crapiest widget based code I've ever seen. 
 
 
Or, in more direct terms... if'n you ain't some lasy arsed, skript kiddie doing scoop and poop coding with all them thar RAD tools and Widgets and stuff... Pelles C is probably where yo want to be :D
 
« Last Edit: May 21, 2012, 04:32:10 PM by CommonTater »

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Hello everybody
« Reply #4 on: May 21, 2012, 06:07:54 PM »
I would like to say that for the main window of my application I like to use the dialog editor and make a dialog based application.  Initially I preferred this method because I liked to be able to visualize where I placed widjets and such.  However later I found that I was increasingly just tossing widjets on the dialog and doing all of the positioning when handling WM_SIZE messages anyway.  Now I still prefer the dialog method but for different reasons.  I don't have to set up the message loop and handle tabbing (focusing) so that makes the code simpler.  There are however, other techniques that I need to employ to override default behavior in a dialog based application when I want to customize certain control behavior.

Either way you do it, it takes time to get a feel for windows coding.  I find that the C coding I have done in Pelles has influenced the way I code applications in Visual Studio C#.  I tend to customize UI behavior more in code now, than in  the visual designer because I want a more dynamic interface.
No one cares how much you know,
until they know how much you care.

CommonTater

  • Guest
Re: Hello everybody
« Reply #5 on: May 21, 2012, 06:52:23 PM »
I I don't have to set up the message loop and handle tabbing (focusing) so that makes the code simpler.  There are however, other techniques that I need to employ to override default behavior in a dialog based application when I want to customize certain control behavior.

For your message loop try something like this...

Code: [Select]
    while( GetMessage( &msg, NULL, 0, 0 ) != 0)
      if (! TranslateAccelerator(Accel,&msg))
        if (! IsDialogMessage(GetForegroundWindow(),&msg))
          { TranslateMessage( &msg );
             DispatchMessage( &msg ); }

This gives you dialog style tabbing and keyboard acclerators in *any* window.  The tab order is creation order... so work from top to bottom when making your windows.
 

Offline DMac

  • Member
  • *
  • Posts: 272
Re: Hello everybody
« Reply #6 on: May 21, 2012, 10:50:42 PM »
Thanks //Tator

I'll give that a try on one of my next projects.
No one cares how much you know,
until they know how much you care.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Hello everybody
« Reply #7 on: May 22, 2012, 06:26:47 AM »
@ DMac CommonTater
It is possible to use dialog inside window too.
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

char *szAppName = "WinFrame";
char *szFrameClass = "cWinFrame";
HWND hFrame;
HWND hWndDlg;
HANDLE hInst;

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcx;
MSG msg;

wcx.cbSize = sizeof(WNDCLASSEX);
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_3DSHADOW;
wcx.lpszMenuName = MAKEINTRESOURCE(2001);
wcx.lpszClassName= szFrameClass;
wcx.hIconSm = 0;

if (!RegisterClassEx(&wcx))
return 0;
hInst = hInstance;

hFrame = CreateWindowEx(0, szFrameClass, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
300, 200,
NULL, NULL, hInst, NULL);
if(!hFrame)
return 0;

hWndDlg = CreateDialog(hInst, MAKEINTRESOURCE(1001), hFrame, (DLGPROC)WndProc);

ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);

while(GetMessage(&msg, NULL, 0, 0))
{
if((hWndDlg && !IsDialogMessage(hWndDlg, &msg)))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 4001:
SetWindowText(hFrame, "OK");
return 0;
case 4002:
SetWindowText(hFrame, "Cancel");
return 0;
}
break;

case WM_SIZING:
((RECT*)lParam)->right = ((RECT*)lParam)->left+300;
((RECT*)lParam)->bottom = ((RECT*)lParam)->top+200;
return 0;

case WM_INITDIALOG:
OutputDebugString("WM_INITDIALOG");
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Code: [Select]
// RESOURCE SCRIPT generated by "Pelles C for Windows, version 7.00".

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>

LANGUAGE LANG_ENGLISH,SUBLANG_ENGLISH_US

2001 MENUEX
{
  POPUP "File", 0, 0, 0
  {
    MENUITEM "&Exit", 6001, 0, 0
  }
}

1001 DIALOGEX DISCARDABLE 0, 0, 310, 18
STYLE DS_SHELLFONT|WS_CHILD|WS_VISIBLE
FONT 8, "MS Shell Dlg", 0, 0, 1
{
  CONTROL "OK", 4001, "Button", WS_TABSTOP, 0, 0, 45, 15
  CONTROL "Cancel", 4002, "Button", WS_TABSTOP, 48, 0, 45, 15
  CONTROL "", 4003, "Edit", ES_AUTOHSCROLL|WS_BORDER|WS_TABSTOP, 96, 0, 40, 12
}
May the source be with you