/*
//If it concern only two coördinates, you have WPARAM and LPARAM to store those values.
//Of course you may need to send more than two value with your message into two param..
//In this case you can use other MACRO's as MAKELPARM, MAKEWPARAM,....
//
//This is an demo that show the two ways....
//I used CBUILDER 5
//You don't need res or def files with CBUILDER 5 for this code
//CBUILDER will generate them automaticly...
*/
//MyMain.h
#define APPNAME "Example WinMain"
#define WNDNAME "WExampleWinMain"
//Function prototype
void AppInit(HINSTANCE hInst,HINSTANCE hPrevInst, int nCmdShow);
void AppRun(void);
void WExampleRegister(HINSTANCE hInst,HINSTANCE hPrevInst);
LRESULT CALLBACK WExampleWndProc(HWND,UINT,WPARAM,LPARAM);
//
//MyMain.cpp
// click with the rbuttom in the window for the first demo
//click with the lbutton in the window for the second demo
#include <windows.h>
#include <windef.h>
#include <stdlib.h>
#include "MyMain.h" //windows declarations
HWND hWndMain; //Handle to main window
HINSTANCE hThisInst; //Handle to this program instance
//the start of a window program
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
LPSTR lpCmdLine, int nCmdShow){
AppInit(hInst,hPrevInst,nCmdShow);
AppRun();
return 0;
}
//AppInit -- Register and Create a window --
void AppInit(HINSTANCE hInst,HINSTANCE hPrevInst,int nCmdShow){
HWND hWnd; //Window identification handle
WExampleRegister(hInst,hPrevInst);
hWnd = CreateWindow(
WNDNAME, //The window's class name
APPNAME, //Caption for title bar
WS_OVERLAPPEDWINDOW, //The window's style
CW_USEDEFAULT, //starting x coordinate
CW_USEDEFAULT, //starting y coordinate
CW_USEDEFAULT, //starting width
CW_USEDEFAULT, //starting height
NULL, //Handle to parent window (none)
NULL, //Handle to menu
hInst, //Program istance handle
NULL); //Optional user parameter (none)
hWndMain = hWnd; //Save window handle in global var
hThisInst = hInst; //Save program instance handle
ShowWindow(hWnd,nCmdShow); //Make window visible
UpdateWindow(hWnd); //Update window contents
}
//AppRun --The infamous 'message loop'
void AppRun(void){
MSG msg;
while(GetMessage(&msg,NULL,NULL,NULL)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//WExampleRegister -- Register the MainWindow --
void WExampleRegister(HINSTANCE hInst,HINSTANCE hPrevInst){
WNDCLASS wc;
if (!hPrevInst){
wc.style =NULL;
wc.lpfnWndProc =WExampleWndProc;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hInstance =hInst;
wc.hIcon =0;
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW +1);
wc.lpszMenuName =NULL;
wc.lpszClassName =WNDNAME;
RegisterClass(&wc);
}
}
// -- WExampleWndProc -- Process window messages
LRESULT CALLBACK WExampleWndProc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp){
RECT rcWindow;
POINT pt1,pt2;
LONG lT=0,lB=0,lW=0,lH=0;
char str[255];
switch(uMsg) {
case WM_RBUTTONDOWN:
{
GetWindowRect(hWnd,&rcWindow);
GetCursorPos(&pt1);
//Store the new top and left
lT=pt1.x;
lB=pt1.y;
//Store the originate with and height
lW=rcWindow.right-rcWindow.left;
lH=rcWindow.bottom-rcWindow.top;
//Store the originate horizon. pos. and vert. pos.
pt2.x=rcWindow.top;
pt2.y=rcWindow.left;
MoveWindow(hWnd,lT,lB,lW,lH,1);
MessageBox(hWnd,"The Windows was moved, klik OK to move it Back","INFO",MB_OK);
//Store the old xpos and ypos in WParam and LPARAM to put the window back
SendMessage(hWnd,WM_USER + 1,pt2.x,pt2.y);
}
break;
case WM_LBUTTONDOWN:
{
GetWindowRect(hWnd,&rcWindow);
GetCursorPos(&pt1);
lT=pt1.x;
lB=pt1.y;
lW=rcWindow.right-rcWindow.left;
lH=rcWindow.bottom-rcWindow.top;
pt2.x=rcWindow.top;
pt2.y=rcWindow.left;
MoveWindow(hWnd,lT,lB,lW,lH,1);
MessageBox(hWnd,"The Windows was moved, klik OK to move it Back","INFO",MB_OK);
//Store the old xpos and ypos in WParam and LPARAM to put the window back
SendMessage(hWnd,WM_USER + 2,MAKEWPARAM(pt2.x,pt2.y),MAKELPARAM(lW,lH));
}
break;
case WM_USER + 1:
if (InSendMessage())
ReplyMessage(TRUE);
GetWindowRect(hWnd,&rcWindow);
lW=rcWindow.right-rcWindow.left;
lH=rcWindow.bottom-rcWindow.top;
MoveWindow(hWnd,lp,wp,lW,lH,1);
MessageBox(hWnd,"Back x.coor in WPARAM, y.coor in LPARAM","INFO",MB_OK);
break;
case WM_USER + 2:
if (InSendMessage())
ReplyMessage(TRUE);
MoveWindow(hWnd,LOWORD(wp),HIWORD(wp),LOWORD(lp),HIWORD(lp),1);
MessageBox(hWnd,"Back x.coor and y.coor in WPARAM, with and lenth in LPARAM","INFO",MB_OK);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0L;
default:
break;
}
return DefWindowProc(hWnd,uMsg,wp,lp);
}
//I will not return ;-)
//Alias HackServ with the stress on Serv