Tech Support > Microsoft Windows > Development Resources > Creating two windows of different colour
Creating two windows of different colour
Posted by ritul on April 5th, 2008


Christian Astor My previous mail was just a test .Actually
I am using the newsgroup for the first time .Well let me first introduce
myself . I am a boy living in India .I have an average knowledge of
c++(pointer,linked list .oops.etc ).I am self taught programmer.
Right now I am trying to learn win32 api and Asm.I have not learnt much .I
am using the book by Charles Petzlod.So it would be great if you help me
out.Plzzz i really need your help brother .I have done the hello windows
programme in one window .I am really confused about which one is window
class,window handle. Now i am tyrying to create it for two windows.The code
that i am typing in is :-
#include <windows.h>

LRESULT CALLBACK WndProc (HWND, HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = "HelloWin";
HWND hwnd, hwnd2 ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, "This program requires Windows NT!",
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
"The Hello Program", // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window
handle
NULL, // window menu handle
hInstance, // program instance
handle
NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
hwnd2 = Createwindow(szAppname,
"sceond window"
WS_OVERLAPPEDWINDOW, // window style
200, // initial x position
123, // initial y position
123, // initial x size
143, // initial y size
NULL, // parent window
handle
NULL, // window menu handle
hInstance, // program instance
handle
NULL) ; // creation parameters
ShowWindow (hwnd2, iCmdShow) ;
UpdateWindow (hwnd2) ;

//ShowWindow (hwnd, iCmdShow) ;
//UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, HWND hwnd2, UINT message, WPARAM
wParam, LPARAM lParam)
{
HDC hdc, hdc2 ;
PAINTSTRUCT ps,ps2 ;
RECT rect, rect2 ;

switch (message)
{
//case WM_CREATE:
// PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
//return 0 ;

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, "Hello, Windows 98!", -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
hdc2 = BeginPaint (hwnd2, &ps2) ;
GetClientRect (hwnd2, &rect2) ;
DrawText (hdc, "Hello, Windows 98!", -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd2, &ps2) ;


return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
Since i am a learner i know there will be lots of mistake (probably more
than one in each line)
So it would be great if some body tells me where i am going wrong.
Plzzz i really need Christian Astor
Thank You
Another thing is that in my ms outlook i am being able to see posts from
19/3/2008 . What will i have to do to see posts before that(i told you i
using the newsgroup for first time)


Posted by Christian ASTOR on April 5th, 2008


ritul wrote:

The main mistake is the wrong prototype of WndProc (only one WHWND)
And only one BeginPaint()-EndPaint()
If you register only one class, you can subclass the 2nd windows and
handle WM_ERASEBKGND =>

LRESULT CALLBACK NewWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT OldWndProc;

// ...
OldWndProc = SetWindowLong(hwnd2, GWL_WNDPROC, (LONG)(WNDPROC) NewWndProc);
ShowWindow(hwnd2, iCmdShow);
UpdateWindow(hwnd2);
//...

LRESULT CALLBACK NewWndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)
{
switch (iMsg)
{
case WM_DESTROY:
return 0;
break;

case WM_ERASEBKGND:
{
HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0) );
RECT rect;
GetClientRect(hwnd, &rect);
HDC hDC = (HDC)wParam;
FillRect(hDC, &rect, hBrush);
DeleteObject(hBrush);
}
return TRUE;
break;

}
return( CallWindowProc((WNDPROC)OldWndProc, hwnd, iMsg, wParam,
lParam ));
}

http://groups.google.com/


Similar Posts