-
Basic Question...
I just got petzolds book... (about a month ago)... I've been avoiding it and programming for console because of one thing I couldn't get answered... where does the code go? I use borland v4.52 and I can make a window... but... does the code go after the window creation or within it? if you have the book it comes with the cd including examples... the example is chapter 4 i think.
-
...
Has anyone read petzolds win32 api book? can anyone answer my question???
-
Depends on what you are doing. Do you want to draw stuff? Put your code in the WM_PAINT message in the wndproc message handler. You have a main message loop right? So, you pretty much need to have your code in the message proc in response to messages sent/posted by your application. Maybe if you tell me something specific you are trying to do I can help more.
-
...
So let's say I wanted to put simple text... I'd have to put it in the text section thingy?... Then how does it all come together?
-
You could do something like this in your WM_PAINT message in your window proc.
Code:
case WM_PAINT:
{
HDC hDC = GetDC( hWnd );
char foo[] = "Hello World";
TextOut( hDC, 200, 200, foo, strlen( foo ) );
ReleaseDC( hWnd, hDC );
return 0;
}
Thats a pretty simple example but it will get something to the screen. Everytime the application receives a WM_PAINT message it will output that.
-
...
But what is the order of the functions? Do I have any control over it?
-
If I'm understanding you correctly , you just want to write a few functions for an application? That's no problem, just write a regular function and call it BEFORE the main message loop but after window creation etc.
-
...
I'm sooo confused... don't mind me.
-
...
Can someone post an example?
-
...
Can anyone tell me why win32 api is so cryptic? I mean the change from console to win32 api is like positioning myself to smack my head on the wall. <sarcasm>Not a very good analogie... I wood have thought of a better one if I just hadn't smacked my head too many times...</sarcasm>
-
Here I through this together in two seconds. It's sloppy but you get the idea.
Code:
#include <windows.h>
#define APPNAME "TEST"
#define CLASSNAME "MyClass"
LRESULT CALLBACK MyWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
case WM_CLOSE:
PostQuitMessage( 0 ); // Exit our program
return 0;
case WM_KEYUP:
if( wParam == 27 ) // 27 = ESC key
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
{
// Obtain our device context
HDC hDC = GetDC( hWnd );
// Output a text string on screen
// Params: HDC, XPOS, YPOS, string, string length
TextOut( hDC, 50, 50, "Hello World", 11 );
// Give back our device context
ReleaseDC( hWnd, hDC );
return 0;
}
}
// Let the default proc handle messages we don't
return DefWindowProc( hWnd, msg, wParam, lParam );
}
BOOL RegisterWindow( HINSTANCE hInst )
{
WNDCLASSEX wndclex = { 0 };
wndclex.cbSize = sizeof( WNDCLASSEX );
wndclex.hInstance = hInst;
wndclex.style = 0;
wndclex.hIcon = NULL; // Default icon
wndclex.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclex.cbClsExtra = 0;
wndclex.cbWndExtra = 0;
wndclex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclex.lpszMenuName = 0;
wndclex.lpfnWndProc = MyWndProc;
wndclex.lpszClassName = CLASSNAME;
if( RegisterClassEx( &wndclex ) == 0 )
return FALSE;
return TRUE;
}
int __stdcall WinMain( HINSTANCE hInst,
HINSTANCE hPrev,
LPSTR lpCmdLine,
int nShowCmd )
{
HWND hWnd;
MSG msg;
if( RegisterWindow( hInst ) == FALSE )
{
MessageBox( 0, "Register Window Failed", "Failure", MB_OK );
return 0;
}
// Here we create our main window
hWnd = CreateWindowEx( WS_EX_TOPMOST,
CLASSNAME,
APPNAME,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL );
if( hWnd == NULL )
{
MessageBox( 0, "CreateWindowEx Failed", "Failure", MB_OK );
return 0;
}
// Update the window then display it
UpdateWindow( hWnd );
ShowWindow( hWnd, SW_SHOW );
// Main message pump
while( GetMessage( &msg, 0, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
Sleep(1);
}
// Quit the program
return msg.wParam;
}
-
...
my question is now... how do you select order if it is always the same when creating windows?
-
You don't select order. You do things based on messaging (didn't you read the book's introduction? :rolleyes: )
See, windows is a message-based system. That means you can't really do anything in order, you just roll with the punches and work it that way.
You can still rewrite programs that you've written in DOS. Let's say you have some program that does a fancy text conversion to spanish from english, and it's all in console. You'd simply create an edit box and a button. The person inputs the text to be translated into the edit box, and clicks the button. That button then sends a message to your program telling you that it's been pressed. Now here's where you come in and tell the program what to DO after that button's been pressed - in this case you'd get the text from the edit boxes, and use your conversion code from your DOS program to convert it (and output it somehow - possibly clear & send it to the eidt box).
In WinAPI you don't have to do much more than Think Different (Copyright - apple computers) about how you're going to input and output everything.