Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Moonwalka

windows program

This topic is 5243 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just tried to write a simple "Hello Windows" program and am having a lot of problems. I have my WinMain() with a message loop; functions to register, create, update and show windows; I have a (blank) resource.h included with the main cpp file. Am I missing a vital component here? Do I need to include stdafx.h and/or any others? And if I do include stdafx.h, is this where I am supposed to include all other header files that the program uses?

Share this post


Link to post
Share on other sites
Advertisement
It''s hard to give any advice when all you say is that you''re "having a lot of problems". Exactly what kind of problems are you having?

Oh, and stdafx.h isn''t required in any project, though it does speed compile times if you use it right. If you generated the project by hand, leave it out for now. If the IDE spat one out for you, leave it in for now.

Share this post


Link to post
Share on other sites
Here''s a quick hello world example, you just need to set up a Win32 project, create a cpp file and paste the following code...


//------------------------------------------------------------------------

// A Windows Application

//

//------------------------------------------------------------------------


#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
HDC hdc;
PAINTSTRUCT ps;

case WM_DESTROY:
PostQuitMessage(0);
return 0;

case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
}

return DefWindowProc(hwnd, msg, wparam, lparam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, char*, int)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "HelloWin";
wc.hIconSm = LoadIcon(0, IDI_APPLICATION);

RegisterClassEx(&wc);

HWND hwnd;
hwnd= CreateWindowEx( 0, "HelloWin", "Hello World", WS_OVERLAPPEDWINDOW,
0, 0, 640, 480, 0, 0, hInstance, 0);

ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);

MSG msg;
while(true)
{
if(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

HDC hdc = GetDC(hwnd);

SetTextColor(hdc, RGB(0, 255, 0));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 0, 0, "Hello, world!", strlen("Hello, world!"));

ReleaseDC(hwnd, hdc);
}

return 0;
}

Share this post


Link to post
Share on other sites
quote:
Original post by simon10k
Here''s a quick hello world example, you just need to set up a Win32 project, create a cpp file and paste the following code...
...code...

I think normally you would want to put the drawing functions (SetTextColor(), SetBkMode(), TextOut()) under the WM_PAINT scope, and use the hdc returned by BeginPaint() instead.

Share this post


Link to post
Share on other sites
quote:
Original post by simon10k
I could of, but I don''t think in terms of events. I am more of a procedural type of person.
In short, a terrible Windows programmer.

Share this post


Link to post
Share on other sites
Would you like that? No, then it''s mean. And I''m pretty sure he knows that.

GameDev''s supposed to be a kind and helpfull community, if someone in this rooms thinks otherwise then let him speak today!! Or forever think in silence..

Or whatever..

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!