Beginner in Windows Programming

Hello everyone, i am new in this windows programming and this is my first program which i seen in msdn library.

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";

WNDCLASS wc = { };

wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);

// Create the window.

HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style

// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);

if (hwnd == NULL)
{
return 0;
}

ShowWindow(hwnd, nCmdShow);

// Run the message loop.

MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;

case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);

FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

EndPaint(hwnd, &ps);
}
return 0;

}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}




I have some questions in my mind that:

Is it necessary to remember the function names and all parameters it passed and what they return?

I didn't see yet the full definition of the functions used in this program in msdn library.So please help me, how to learn windows programming.How to get through functions used in library.
You ought to get yourself a book. Windows programming is basically a six month to twelve month ordeal, just to get started. I'd recommend Charles Petzold's "Programming Windows", fifth edition. Here is a link to some material I've written...

http://www.jose.it-berater.org/smfforum/index.php?topic=3389.0
Unfortunatly yes it is neccessary to remeber most of these functions if you really want to have that nice looking Win32 UI. The good news is that they are easier then you might think once you've done it a few times. For example, if you want a window for the UI then you need to use "CreateWindow()" or "CreateWindowEx()", like most functions that deal with the core Windows components this returns a HANDLE data type (void *). In order to use either one of those functions, you need to register you Windows class with... ready?... "RegisterClass()". Then if you want to show your window you use "ShowWindow()". The "Message Pump" becomes pretty much second nature, I actually feel weird if I write something that doesn't require one now a days. So over all it's not that much to remember, links in "Favorites" to MSDN are your best friend early on.

I haven't ready freddie1's post on that other site yet, but I plan to if my time permits.
Last edited on
That's good stuff, Freddie1. And I wholeheartedly agree that Petzold is an absolute MUST for anyone who wants to seriously learn Windows programming. I never could have written the apps I've written without Petzold as a reference. In fact, there are a LOT of hidden little nuggets in Petzold that you can discover if you go over it enough times.

I haven't ready freddie1's post on that other site yet, but I plan to if my time permits.


Let me know what you think Computergeek01 after you've had time to scan it. That is an ongoing project of mine I'd like to keep improving as time permits, so I welcome constructive criticisim. Just a couple weeks ago I redid that example 37, and I need to redo the several ones after that which I did a couple years back.
That way, these program examples of mine here and your programs will work irregardless of whether some define is present or not.

To have typed up this detailed and informative article and then to have dropped the ball on something so trivial right at the end... Please fix this minor issue.

- No love for MSDN? Windows is hands down the best documented API on the web, I personally think this is something that all of the new programmers should know.

- At first I was ready to point out that you didn't mention the COM, but then on my second pass I thought you may have omited it (rather then forgot about it). Now I'm torn, you mention the need for people that want a top down understanding of how to program for Windows to know both the frameworks they are writting for and the language that those frameworks are built on. But to not mention a key component that makes these things possible seems odd to me. I guess what I'm saying is that IMO it's worth mentioning what the COM is, if for no other reason then to save a few more people from trying to figure it out on their own.

Regarding the creation of a window object:
In C++ speak, its a call to an object constructor.

Awkwardly written and slightly misleading, this should be reworded.

Shortley after the quote from above:
It might be pointed out that at the point of the big CreateWindow() call down in
WinMain(), the Window Procedure will be sent a WM_CREATE message before the
CreateWindow() call in WinMain() returns. Therefore, the Window Procedure will have
first knowledge of the Window Handle Windows has assigned to the window as a result
of the CreateWindow() call. That hwnd will come through to the Window Procedure as
the first parameter of the call. In our case below a MessageBox is put up. After
that message has been processed by the Window Procedure and fnWndProc returns, the
CreateWindow() call down in WinMain() will return and the hWnd will receive the handle
to the Main Program Window.

This paragraph needs to be cleaned up. It's unclear what you are trying to say and it doesn't fit the flow of the rest of the article.

- You mentioned that certain DLL's "...are a part of every Windows installation...". It's more accurate to say that DLL (Kernel32.dll) is automatically included in every process run in Windows at the same address. This is why you don't need to link to the import library for it, but you will need to for other Windows functions. I'm pretty sure that User32.dll is only included automatically for processes run in User Space.

- I like the observations that you made in the CBox examples. I don't think that's something that I've noticed before.

Overall this was a very well written article, I would recommend it for intermidiate users
Thanks very much for the detailed feedback ComputerGeek! I'm in the process of working on your suggestions. I'll get back in touch shortly.
Topic archived. No new replies allowed.