child window is not displaying

All questions regarding Windows programming, post here. API,COM, ActiveX, DirectX, OpenGL, MFC and so on...

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

child window is not displaying

Postby ashishabc5 » Wed Mar 16, 2011 5:17 am

i am doing Instant messenger program.in it when i double click the member of contact list a chat window must open. but it is showing registraton failed and not oppening up.can any one help.....


the code is given below


Code: Select all
#include "windows.h"
#include "resource.h"

#include <iostream>
#include <strsafe.h>
#include <windowsx.h> 

//for GET_WM_COMMAND_CMD function
using namespace std;
#define IDC_LIST 1

#define IDC_STATIC 2
#define WM_SETCURSOR  0x0020
#define ID_EDITCHILD 100

LPSTR pszText;
LPWSTR lpszPassword;
WORD cchPassword;
WORD cchUserName;

LPCTSTR ClsName = L"Client";
LPCTSTR WndName = L"IM TECHNOLOGIES";             

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,WPARAM wParam, LPARAM lParam);

LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
HINSTANCE hInst;
HWND hWnd;

typedef struct
{
   TCHAR name[30];
   TCHAR job[20];
   int age;
}Friends;

Friends friends[]=
{
    {TEXT("Erika"), TEXT("waitress"), 18},
    {TEXT("Thomas"), TEXT("programmer"), 25},
    {TEXT("George"), TEXT("police officer"), 26},
    {TEXT("Michael"), TEXT("producer"), 38},
    {TEXT("Jane"), TEXT("steward"), 28},
};
HWND Chhwnd, Chrichedit1, Chrichedit2;
MINMAXINFO * ChminMaxSize;
//LRESULT CALLBACK ChatWindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
class ChatWindow
{
public:
   int CwCreation(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow);
   
};
class Client
{
public:
   WNDCLASSEX WndClsEx;
   INT WINAPI cwind(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow);
    void createConnection();
};
class Login
{
   WORD LUsername;
   WORD LPassword;
public:
   LRESULT Username(HWND);
   LRESULT Password(HWND);
   void CreateUser();
};
LRESULT Login::Username(HWND hWnd)
{
   LUsername = (WORD) SendDlgItemMessage(hWnd,IDE_USERNAME,EM_LINELENGTH,(WPARAM)0,(LPARAM)0);
   if (LUsername == 0)
   {
      MessageBox(hWnd,L"Please enter Username",L"Error",MB_OK);
      return FALSE;
   }
   else
      Password(hWnd);
}
LRESULT Login::Password(HWND hWnd)
{
   LPassword = (WORD) SendDlgItemMessage(hWnd,IDE_PASSWORD,EM_LINELENGTH,(WPARAM)0,(LPARAM)0);
   if (LPassword >= 16)
   {
      MessageBox(hWnd,L"Too many characters.",L"Error",MB_OK);
      return FALSE;
   }
    else if (LPassword == 0)
   {
      MessageBox(hWnd,L"Please enter password.",L"Error",MB_OK);
        return FALSE;
   }
   else
   {
      MessageBox(hWnd,L"Signed in Successfully",L"Confirmation",NULL);
        EndDialog(hWnd, TRUE);
        return FALSE;
   }   
}
int ChatWindow::CwCreation(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
   LRESULT CALLBACK ChatWindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
   WNDCLASSEX Chwc;
    MSG Msg;

   Chwc.cbSize        = sizeof(WNDCLASSEX);
   Chwc.style         = 0;
   Chwc.lpfnWndProc   = ChatWindowProc;
   Chwc.cbClsExtra    = 0;
   Chwc.cbWndExtra    = 0;
   Chwc.hInstance     = hInstance;
   Chwc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
   Chwc.hCursor       = LoadCursor(NULL, IDC_CROSS);
   Chwc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
   Chwc.lpszMenuName  = NULL;
   Chwc.lpszClassName = L"WC";
   Chwc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
   
   if(!RegisterClassEx(&Chwc))
   {
      MessageBox(NULL, L"Window Registration Failed!", L"Error!",
         MB_ICONEXCLAMATION | MB_OK);
      return 0;
   }

   
   Chhwnd = CreateWindowEx(
      WS_EX_CLIENTEDGE,
      L"WC",
      L"IMCHAT",
      WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT, 381, 422,
      NULL, NULL, hInstance, NULL);
   
   if(Chhwnd == NULL)
   {
      MessageBox(NULL,L"Window Creation Failed!", L"Error!",
         MB_ICONEXCLAMATION | MB_OK);
      return 0;
   }
   
   ShowWindow(Chhwnd, nCmdShow);
   UpdateWindow(Chhwnd);
   static HINSTANCE hLib;
   static TCHAR     chCntrlName[32];
   hLib=LoadLibrary(TEXT("RICHED20.DLL"));
   if (!hLib)
   {
      hLib=LoadLibrary(TEXT("RICHED32.DLL"));
      if (!hLib)
      {
         MessageBox( NULL,
            TEXT("Failed to load rich edit library"),
            TEXT("ERROR"),
            MB_OK|MB_ICONERROR);
         return 0;
      }
      else
      {
         lstrcpy(chCntrlName,TEXT("RICHEDIT"));
      }
   }
   else
   {
      lstrcpy(chCntrlName,TEXT("RichEdit20A"));
   }
   
   Chrichedit1 = CreateWindowEx(WS_EX_CLIENTEDGE,chCntrlName,TEXT(""),
      WS_CHILD | WS_VISIBLE | WS_VSCROLL |ES_MULTILINE|ES_AUTOVSCROLL|ES_READONLY ,
      5,30,360,212,Chhwnd,NULL,hInstance,NULL);
   
   Chrichedit2 = CreateWindowEx(WS_EX_CLIENTEDGE,chCntrlName,TEXT(""),
      WS_CHILD | WS_VISIBLE | WS_VSCROLL |ES_MULTILINE|ES_AUTOVSCROLL,5,260,
      355,100,Chhwnd,NULL,hInstance,NULL);
   
   
   while(GetMessage(&Msg, NULL, 0, 0) > 0)
   {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
   }
   return Msg.wParam;
}


LRESULT CALLBACK ChatWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
   case WM_GETMINMAXINFO: 
      {
         ChminMaxSize = (MINMAXINFO *)lParam ;
         ChminMaxSize->ptMinTrackSize.x = 350 ;
         ChminMaxSize->ptMinTrackSize.y = 350 ;
         return 0;
      }
   case WM_SIZE:
      {
         SetWindowPos(Chrichedit1,0,5,30,LOWORD(lParam)-9,HIWORD(lParam)-152,0);
         SetWindowPos(Chrichedit2,0,5,HIWORD(lParam)-104,LOWORD(lParam)-10,100,0);
         return 0;
      }
   case WM_COMMAND:
      if( HIWORD(wParam) == BN_CLICKED ){
         
      }
      break;
   case WM_CLOSE:
      DestroyWindow(hwnd);
        break;
   case WM_DESTROY:
      PostQuitMessage(0);
        break;
   default:
      return DefWindowProc(hwnd, msg, wParam, lParam);
   }
   return 0;
}
INT WINAPI Client::cwind(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
   
   HWND       hWnd;
   WndClsEx.cbSize        = sizeof(WNDCLASSEX);
   WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
   WndClsEx.lpfnWndProc   = WndProcedure;
   WndClsEx.cbClsExtra    = 0;
   WndClsEx.cbWndExtra    = 0;
   WndClsEx.hIcon         = LoadIcon(hInstance,NULL);
   WndClsEx.hCursor       = LoadCursor(0, IDC_ARROW);
   WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
   WndClsEx.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
   WndClsEx.lpszClassName = ClsName;
   WndClsEx.hInstance     = hInstance;
   WndClsEx.hIconSm       = LoadIcon(hInstance,NULL);
   hInst=hInstance;
   // Register the application
   RegisterClassEx(&WndClsEx);

   // Create the window object
   hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                          ClsName,
                          WndName,                //OR WindowCaption
                          WS_OVERLAPPEDWINDOW  | WS_VISIBLE|WS_VSCROLL,// | WS_HSCROLL |
                          100,  //POSITION OF THE WINDOW X AXIS
                          100,  //POSITION OF THE WINDOW Y AXIS
                          300,  //WIDTH OF WINDOW                     
                          500,  //HEIGHT OF WINDOW
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

   // Find out if the window was created
   if( !hWnd ) // If the window was not created,
      return FALSE; // stop the application
   // Display the window to the user
   ShowWindow(hWnd, nCmdShow);// SW_SHOWNORMAL);
   UpdateWindow(hWnd);
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{         
   MSG        Msg;
   Client c;
   DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),hWnd, reinterpret_cast<DLGPROC>(DlgProc));  //SIGNIN WINDOW
    c.cwind(hInstance,hPrevInstance,lpCmdLine,nCmdShow);
   while( GetMessage(&Msg, NULL, 0, 0) )
   {
             TranslateMessage(&Msg);
             DispatchMessage(&Msg);
   }
   return Msg.wParam;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,WPARAM wParam, LPARAM lParam)
{   
   static LPSTR lpCmdLine;static  int nCmdShow;
   static HWND hwndList, hwndStatic;
    int i;
   ChatWindow cw;
    switch(Msg)
    {

            case WM_CREATE:
            hwndList = CreateWindow(TEXT("listbox") , NULL, WS_CHILD | WS_VISIBLE | LBS_NOTIFY,
                                                 10, 10, 150, 120, hWnd,(HMENU) IDC_LIST, hInst, NULL);
                hwndStatic = CreateWindow(TEXT("static") , NULL, WS_CHILD | WS_VISIBLE,
                                                 200, 10, 120, 45, hWnd,(HMENU) IDC_STATIC, hInst, NULL);
                for (i = 0; i < ARRAYSIZE(friends); i++)
            {
                     SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM) friends[i].name);                                 
                }
             break;
         case WM_COMMAND:
         {
            WORD notify=GET_WM_COMMAND_CMD(wParam,lParam);
            switch(GET_WM_COMMAND_ID(wParam, lParam))
                {
            case IDC_LIST:
               switch(notify)
               {
               case LBN_DBLCLK:
                  cw.CwCreation(GetModuleHandle(NULL),GetModuleHandle(NULL),lpCmdLine,nCmdShow);
      /*SetCursor(LoadCursorFromFile(L"C:\\WINDOWS\\Cursors\\drum.ani"));*/
               }
            break;
            }
         }
            break;
         case WM_LBUTTONDOWN:
                SetCursor(LoadCursorFromFile(L"C:\\WINDOWS\\Cursors\\drum.ani"));
              break;
         case WM_DESTROY:
                  PostQuitMessage(WM_QUIT);
                  break;
           default:                            // Process the left-over messages
                  return DefWindowProc(hWnd, Msg, wParam, lParam);
   
      }
}

LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
   switch(Msg)
   {
     case WM_INITDIALOG:
        break;
      case WM_COMMAND:
        {
           switch(wParam)
           {
           case ID_SIGNIN:
              Login l;
              l.Username(hWndDlg);
              break;
           case ID_REG:
            DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG3),hWnd, reinterpret_cast<DLGPROC>(DlgProc));
                break;
           case IDC_SEND:
            SendMessage(hWnd, WM_SETTEXT, 0,0);
                break;
           case IDS_CANCEL:
            PostQuitMessage(WM_QUIT);
            break;
           case IDR_CANCEL:
            EndDialog(hWndDlg, TRUE);
            break;
           }
        }
     case WM_CLOSE:
        break;
     }
     return FALSE;
}
ashishabc5
 
Posts: 1
Joined: Wed Mar 16, 2011 5:02 am

Re: child window is not displaying

Postby exomo » Thu Mar 17, 2011 4:33 pm

The "registration failed" is your own error message, so it's easy to find where the problem is.
You try to register the same class (with the same name "WC") everytime you open a new chat window. To get a more specific error message, you could use getlasterror() to get an error code, you can look up this code here.
To solve your problem, you could just ignore the error (assuming you registered the class earlier) and conitnue creating your window.
The proper solution would be to put the code for window class registration in a new function init() (or registerClasses() or whatever you like) and call that function once at program startup.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden


Return to Windows Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron