PSP Coding


 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 [Win32 Programming] Basic Window Tutorial

Go down 
AuthorMessage
-LeetGamer-
Admin


Posts : 247
Points : 397
Reputation : 4
Join date : 2010-09-29
Age : 25

PostSubject: [Win32 Programming] Basic Window Tutorial   Wed Oct 20, 2010 5:36 pm

To run this you need to set up a Win32 project:

1] File > New > Project
2] Select Win32
3] If there is an option, select empty project
4] If you selected empty then add a new .cpp file to the project
5] Add the code shown here
6] Enjoy

Main.cpp:
Code:

// Include pre-written code to help us out
#include <Windows.h>

// Globals
HINSTANCE hInstance; // Holds the .exe in computer's memory
HWND hwnd; // Handles the window
MSG msg; // Message structure
WNDCLASSEX WindowClass; // WindowClass, this will hold some information on the window

// Function prototypes
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool InitWindow ();

// hInstance: Holds the .exe in computer's memory
// hPrevInstance: Not used in Win32
// lpCmdLine: Not used in this program
// nCmdShow: An integer to show how the window will be displayed
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lPCmdLine, int nCmdShow)
{
   // Call InitWindow ()
   // If it returns false then display a message box telling us then close the program
   if (!InitWindow ())
   {
      MessageBox (NULL,
               L"InitWindow () Returned False",
               NULL,
               MB_OK | MB_ICONWARNING);
      return 0;
   }

   // Message loop, this will loop around and send messages to the Window Procedure from the message qeueu to be processed
   while (GetMessage (&msg, NULL, 0, 0) > 0)
   {
      TranslateMessage (&msg); // Changes the msg around a little
      DispatchMessage (&msg); // Sends the message to the Window procedure
   }
   // End the program
   return msg.wParam;
}

// Window Procedure, where all the messages are handled
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch (msg)
   {
   // These three case statements are for if the message is to close the window
   // If it is, then we will actually close the window
   case WM_CLOSE:
      DestroyWindow (hwnd);
      break;
   case WM_DESTROY:
      PostQuitMessage (0);
      break;
   case WM_KEYDOWN:
      switch (wParam)
      {
      case VK_ESCAPE:
         DestroyWindow (hwnd);
      }
      break;
   }
   // Send all messages not handled to the Default Window procedure
   return DefWindowProc (hwnd, msg, wParam, lParam);
}

// InitWindow, this will create the window for us
bool InitWindow ()
{
   WindowClass.cbClsExtra = 0; // Extra bytes to allocate for the class
   WindowClass.cbSize = sizeof (WNDCLASSEX); // Size of the structure
   WindowClass.cbWndExtra = 0; // Extra bytes to allocate for the window
   WindowClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // background color, right now it's white, edit the 1
   WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW); // Cursor for the window, loads the default cursor
   WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION); // Icon
   WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION); // Small Icon
   WindowClass.hInstance = hInstance; // hInstance
   WindowClass.lpfnWndProc = WndProc; // The wndproc for the class
   WindowClass.lpszClassName = L"1"; // name of the class, will be refered to later
   WindowClass.lpszMenuName = NULL; // no menu so no menu name
   WindowClass.style = 0; // N/A
   if (!RegisterClassEx (&WindowClass)) // call registerclassex function, if it fails return false
   {
      return false;
   }
   hwnd = CreateWindowEx (WS_EX_CLIENTEDGE, // One variable of the style
                    L"1", // name of the class
                    L"Win32 Example", // title of the window
                    WS_OVERLAPPEDWINDOW, // Another variable of the style
                    315, 115, // X, Y co-ordinate values of where to start your window at
                    640, 480, // Width, Height of your window
                    NULL, // parent window
                    NULL, // menu handle
                    hInstance, // instance
                    NULL); // N/A
   if (hwnd == NULL) // Check to see if everything went ok
   {
      return false;
   }
   ShowWindow (hwnd, SW_SHOWNORMAL); // Show the window to use
   return true; // return true, because a window was created good
}

Note: A messsage is a key press or a mouse click. If you have ANY questions at all let me know here or with a PM and I will try my best to help

Very Happy

-Leet
Back to top Go down
View user profile
xBaD00BoYx



Posts : 5
Points : 5
Reputation : 0
Join date : 2010-09-30

PostSubject: Re: [Win32 Programming] Basic Window Tutorial   Sat Oct 23, 2010 6:59 am

It doesn't work :/ , i got 3 errors
Back to top Go down
View user profile
-LeetGamer-
Admin


Posts : 247
Points : 397
Reputation : 4
Join date : 2010-09-29
Age : 25

PostSubject: Re: [Win32 Programming] Basic Window Tutorial   Sat Oct 23, 2010 5:53 pm

What errors? Some of the code is changed depending on what compiler you use.
Back to top Go down
View user profile
xBaD00BoYx



Posts : 5
Points : 5
Reputation : 0
Join date : 2010-09-30

PostSubject: Re: [Win32 Programming] Basic Window Tutorial   Sun Oct 24, 2010 8:14 am

i use devc++
Back to top Go down
View user profile
-LeetGamer-
Admin


Posts : 247
Points : 397
Reputation : 4
Join date : 2010-09-29
Age : 25

PostSubject: Re: [Win32 Programming] Basic Window Tutorial   Sun Oct 24, 2010 8:16 am

Then get rid of the L before strings like L"some text here" change that to "some text here" and that should do it.
Back to top Go down
View user profile
xBaD00BoYx



Posts : 5
Points : 5
Reputation : 0
Join date : 2010-09-30

PostSubject: Re: [Win32 Programming] Basic Window Tutorial   Sun Oct 24, 2010 8:21 am

http://is.gd/gfV1a :s
Back to top Go down
View user profile
-LeetGamer-
Admin


Posts : 247
Points : 397
Reputation : 4
Join date : 2010-09-29
Age : 25

PostSubject: Re: [Win32 Programming] Basic Window Tutorial   Sun Oct 24, 2010 4:43 pm

Did you make a Win32 project?
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: [Win32 Programming] Basic Window Tutorial   

Back to top Go down
 
[Win32 Programming] Basic Window Tutorial
Back to top 
Page 1 of 1
 Similar topics
-
» Visual Basic Chess
» Ten Basic Checkmates to Know
» Curl Tutorial!
» Programming A Chess Engine In Pure Javascript! (youtube)
» AOE_Fan's RMS Command Tutorial

Permissions in this forum:You cannot reply to topics in this forum
PSP Coding :: Windows Programming :: Windows Programming Tutorials :: GUI Tutorials-
Jump to: