[SOLVED] C Windows API Issues....

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

[SOLVED] C Windows API Issues....

Post by jackpf »

Hello everyone...

I know this is a php forum, and I've posted this on a few C forums as well...but you know...since this is such a responsive community... ;)

Anyway, I've decided to have a bash at C. I can handle CLI programs fine - it's basically the same as PHP. However, I've been trying to use the windows API to create a graphical user interface...to no avail.

Here's my code:

Code: Select all

#include <windows.h>
 
const char Window_Class[] = "WindowClass";
 
//window callback handler
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    //switch message...
    switch(msg)
    {
        //close window
        case WM_CLOSE:
            MessageBox(hwnd, "Bye", ":)", 0);
            DestroyWindow(hwnd);
        break;
        //display window
        case WM_PAINT:
            RECT rcClient;
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
 
            DrawTextEx(hdc, "Hello...world?"+'\0', -1, &rcClient, DT_CENTER, NULL);
            
            EndPaint(hwnd, &ps);
        break;
        //register window process
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
    }
 
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    char *lpCmdLine, int nCmdShow)
{
    //register windows classes
    WNDCLASSEX  window;
    HWND        hwnd;
    MSG         msg;
 
    //register window with....windows ;)
    window.cbSize           = sizeof(WNDCLASSEX);
    window.style            = 0;
    window.lpfnWndProc      = WndProc;
    window.cbClsExtra       = 0;
    window.cbWndExtra       = 0;
    window.hInstance        = hInstance;
    window.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    window.hCursor          = LoadCursor(NULL, IDC_ARROW);
    window.hbrBackground    = (HBRUSH) (COLOR_WINDOW + 1);
    window.lpszMenuName     = NULL;
    window.lpszClassName    = Window_Class;
    window.hIconSm          = LoadIcon(NULL, IDI_APPLICATION);
 
    //...
    RegisterClassEx(&window);
 
    //create the window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        Window_Class,
        "Window!",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 250,
        NULL, NULL, hInstance, NULL);
 
    //run...
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    //run the message loop
    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
 
    return msg.wParam;
}
 
I'm using DrawTextEx() to display the text...but it's just not showing. I'm using Windows XP, and CodeBlocks as my compiler...

Any help would be awesome,

Cheers,
Jack.
Last edited by jackpf on Sat Aug 15, 2009 3:12 pm, edited 2 times in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: C Windows API Issues....

Post by VladSun »

I can't answer your question but you could use WinBinder instead ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: C Windows API Issues....

Post by jackpf »

Hmm....but don't these things just create command line programs? The point of this was to make a "window"... :D

I've always been sceptical of these PHP compilers...do you know how they actually work?

Anyway, I'd love to do this in C. It's such an awesome language....

Btw, I found that you're supposed to call EndPaint() after BeginPaint() (I updated my code) but it unfortunately didn't make a difference...
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: C Windows API Issues....

Post by redmonkey »

I'm not familiar with this particular Windows API but that code on it's own won't do anything, I'm guessing there must be more to your program?

Also, CodeBlocks isn't a compiler, it's an IDE. CodeBlocks has an optional/alternative installer which includes Mingw ports of some GNU compilers. The last time I looked at CodeBlocks it didn't seem to care if you chose a C or C++ project everything was punted to g++ (a C++ compiler) for building, which may lead to problems.
jackpf wrote:it's basically the same as PHP.
I think that's one of the biggest issues for people, C developers looking at PHP scripts for the first feel confident that they can deal with it easily as many areas of the syntax of the two languages are similar, this also appears to be the case vice versa for PHP developers looking at C for the first time. The reality is that while much of the syntax is similar the semantics are wildly different.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: C Windows API Issues....

Post by jackpf »

Ahh, I found what I was looking for at msdn.microsoft.com. It was TextOut(). So simple...

And all that code does is display a window with some text. It's pretty simple...I'm just trying to get the hang of the windows api. And this windows api...is the windows api. windows.h. I'm just using windows functions to display the window.

I'm not sure I agree...command line C programs are extremely similiar to php. They have the same constructs...similiar syntax....

I think working with an OS API is a bit harder...but once you learn a few of the API functions and classes, you get the hang of it.

Anyway, the final code:

Code: Select all

#include <windows.h>
 
#define Window_Class "WindowClass"
 
const char Title[]      = "Title!";
const char Message[]    = "Hello world :)";
 
//window callback handler
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    //switch message...
    switch(msg)
    {
        //display window
        case WM_PAINT:
            RECT rcClient;
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
 
            TextOut(hdc,
                    5, 5,
                    Message, strlen(Message));
 
            EndPaint(hwnd, &ps);
        break;
        //close window
        case WM_CLOSE:
            MessageBox(hwnd, "Bye", ":)", MB_OK);
            DestroyWindow(hwnd);
        break;
        //register window process
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
    }
 
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    char *lpCmdLine, int nCmdShow)
{
    //register windows classes
    WNDCLASSEX  window;
    HWND        hwnd;
    MSG         msg;
 
    //register window with....windows ;)
    window.cbSize           = sizeof(WNDCLASSEX);
    window.style            = 0;
    window.lpfnWndProc      = WndProc;
    window.cbClsExtra       = 0;
    window.cbWndExtra       = 0;
    window.hInstance        = hInstance;
    window.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    window.hCursor          = LoadCursor(NULL, IDC_ARROW);
    window.hbrBackground    = (HBRUSH) (COLOR_WINDOW + 1);
    window.lpszMenuName     = NULL;
    window.lpszClassName    = Window_Class;
    window.hIconSm          = LoadIcon(NULL, IDI_APPLICATION);
 
    //...
    RegisterClassEx(&window);
 
    //create the window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        Window_Class,
        Title,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 250,
        NULL, NULL, hInstance, NULL);
 
    //run...
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    //run the message loop
    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
 
    return msg.wParam;
}
I had a go at expanding on that today, and made my own browser :) I'm actually using it right now...
It's dead simple, but still...good to learn your way around the windows API.

If anyone wants to take a look, http://jackpf.co.uk/blog/Programming. There should be a download link (just about to post it).

But yeah, thanks for your help anyway,
Regards,
Jack.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [SOLVED] C Windows API Issues....

Post by Eran »

it's not just a different API and functions. Though syntactically similar there are some very basic core differences such as memory management / garbage collection, pointers, OOP support, exception handling and so forth.

Just cause the syntax looks familiar to you doesn't mean you can expect to use PHP programming practices in C and build significant, stable applications.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [SOLVED] C Windows API Issues....

Post by jackpf »

True.

Yeah, it took me a while to grasp pointers....with the deference operator and such.

In PHP, you'd just do:

Code: Select all

$var1 = 'something';
 
$var2 = &$var1;
 
//$var2 now accesses the same memory $var1 does
 
$var2 = 'something else'; // $var1 = $var2 = 'something_else'
Whereas in C:

Code: Select all

int a = 2, *b; //b is a pointer
b = &a;
//b has a's memory
*b = 5; //a = b = 5
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [SOLVED] C Windows API Issues....

Post by Eran »

important to note that C pointers are nothing like PHP references. The first points to a memory address and the second is simply an alias to the same parameter.

You can look in the PHP manual for examples of how they differ. http://us3.php.net/manual/en/language.references.php
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: [SOLVED] C Windows API Issues....

Post by redmonkey »

It doesn't matter if you're writing a console or GUI application in C, the syntax and semantics are the same.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: [SOLVED] C Windows API Issues....

Post by alex.barylski »

Jack: If you want to build real Windows applications you really really need to switch to MFC and use the application wizard in VS to build a base application and extend it from there. Windows API is a complex beast and understanding that is critical to Windows application development but MFC will save you SOOOOOOO much effort it's not even funny.

Also, you'd have far better luck in getting replies over at CodeProject.com which is a community with articles and C/C++ developers -- likewise their PHP community sucks unlike here. :P
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [SOLVED] C Windows API Issues....

Post by jackpf »

Yeah...I just wanted to see if I could make my own window using the windows API without having a program write it for me.

And I can :D

But yeah, I agree that VS does make stuff a lot easier it's unreal.

And I posted this on http://forums.devshed.com as well...got some pretty good responses there. As I said, since both languages are so similar, I just thought someone might have some sort of knowledge or experience about my problem here :D
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [SOLVED] C Windows API Issues....

Post by VladSun »

jackpf wrote:... since both languages are so similar ...
They are not, indeed.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [SOLVED] C Windows API Issues....

Post by jackpf »

I find them fairly similar.

Like, I think I "know" PHP.

I've never been to a C class or anything, but I'm still able to write programs in it.

I'd put that down to my prior knowledge of PHP.

If someone asked me to write a program in Delphi or something, I'd have no idea how to. Because it's nothing like PHP, or anything else I know.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [SOLVED] C Windows API Issues....

Post by VladSun »

Well, you could write a snippet in Perl and it will work, but still it doesn't mean that Perl and PHP are "so similar".
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: [SOLVED] C Windows API Issues....

Post by alex.barylski »

Perl, PHP, JavaScript, C++ and many others are all derivatives of C...but they really only share a common syntax.

To effectively program in C/C++ you need to understand pointers and memory management whereas in PHP this is a none-issue. Likewise in PHP you need to be familiar with the Internet and it's protocols (specifically HTTP) whereas most desktop applications get along fine without any Internet connectivity.

JavaScript requires an understanding of a DOM, Windows programming requires good understanding of Windows API, etc.

Perl has a cryptic syntax but still has the ubiquitous semi-colon to indicate the end of a statement and the curly braces for indicating block scope.

Learning the language, IMHO is not the difficult part (once you understand programming concepts; loops, conditionals, etc) as it's really a matter of learning the syntax. It's all the associated technologies that go along with the language as a technology itswelf.
Post Reply