Adding buttons, edit boxes, etc to the window

This is a discussion on Adding buttons, edit boxes, etc to the window within the Windows Programming forums, part of the Platform Specific Boards category; I am new to windows programming and I am trying to turn a console program I made (an IRC bot) ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    29

    Adding buttons, edit boxes, etc to the window

    I am new to windows programming and I am trying to turn a console program I made (an IRC bot) into a windows32 program so I can have it minimize to the system tray and take advantage of asynchronous sockets. This is the first windows program I have attempted to make so sorry if there are a lot of things that you see wrong with it. So far this is what I have:

    Main.cpp
    Code:
    #include <windows.h>
    #include <shellapi.h>
    
    #include "Resource.h"
    
    const char g_szClassName[] = "Windows App";
    NOTIFYICONDATA minimized;
    
    LRESULT CALLBACK WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
    {
        POINT pt;
        HMENU hPopupMenu = CreatePopupMenu();
        AppendMenu(hPopupMenu, MF_STRING, IDMI_RESTORE, "Restore");
        AppendMenu(hPopupMenu, MF_STRING, IDMI_CLOSE, "Exit");
    
        switch(message)
        {
            case PUGBOT_SOCKET_MSG:
            {
                if (WSAGETSELECTERROR(lparam))
                {
                    // ERROR
                }
    
                switch(WSAGETSELECTEVENT(lparam))
                {
                    case FD_READ:
                    {
                    }
                    break;
    
                    case FD_WRITE:
                    {
                    }
                    break;
    
                    case FD_CONNECT:
                    {
                    }
                    break;
    
                    case FD_ACCEPT:
                    {
                    }
                    break;
                }
            }
            break;
    
            case WM_CREATE:
            {	/*
                HWND hServerName;
                hServerName = CreateWindowEx(0, "Static", "Server:", WS_CHILD | WS_VISIBLE,
                                             5, 5, 50, 25, window, (HMENU)IDD_NAME,
                                             GetModuleHandle(NULL), NULL);
    
    
                HWND hServerBox;
                hServerBox = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "",
                                            WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
                                            55, 5, 175, 25,
                                            window, (HMENU)IDD_SERVERBOX, GetModuleHandle(NULL), NULL);
    
                HWND hPortName;
                hPortName = CreateWindowEx(0, "Static", "Port:", WS_CHILD | WS_VISIBLE,
                                           235, 5, 35, 25, window, (HMENU)IDD_NAME,
                                           GetModuleHandle(NULL), NULL);
    
    
                HWND hPortBox;
                hPortBox = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "",
                                          WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
                                          270, 5, 55, 25,
                                          window, (HMENU)IDD_PORTBOX, GetModuleHandle(NULL), NULL);
    
                HWND hChannelName;
                hPortName = CreateWindowEx(0, "Static", "Channel:", WS_CHILD | WS_VISIBLE,
                                           330, 5, 60, 25, window, (HMENU)IDD_NAME,
                                           GetModuleHandle(NULL), NULL);
    
                HWND hChannelBox;
                hChannelBox = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "",
                                             WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
                                             395, 5, 125, 25,
                                             window, (HMENU)IDD_RECVBOX, GetModuleHandle(NULL), NULL);
    
    
                HWND hConnectButton;
                hConnectButton = CreateWindowEx(WS_EX_STATICEDGE,"BUTTON", "Connect",
                                                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                                525, 5, 105, 60,
                                                window, (HMENU)IDD_CONNECT, GetModuleHandle(NULL), NULL);
    
                HWND hRecvBox;
                hRecvBox = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "",
                                          WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |
                                          ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
                                          5, WINDOW_HIEGHT-400, WINDOW_WIDTH-15, WINDOW_HIEGHT-45-65-65,
                                          window, (HMENU)IDD_RECVBOX, GetModuleHandle(NULL), NULL);
    
                HWND hSendBox;
                hSendBox = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "",
                                          WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
                                          5, WINDOW_HIEGHT-75, WINDOW_WIDTH-120, 25,
                                          window, (HMENU)IDD_SENDBOX, GetModuleHandle(NULL), NULL);
                HWND hSendButton;
                hSendButton = CreateWindowEx(WS_EX_STATICEDGE,"BUTTON", "Send",
                                             WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                             WINDOW_WIDTH-110, WINDOW_HIEGHT-75, 100, 25,
                                             window, (HMENU)IDD_SENDBUTTON, GetModuleHandle(NULL), NULL);
    			*/
            }
            break;
    
            case WM_COMMAND:
            {
                switch(lparam)
                {
                    case IDI_TRAY:
                    {
                        Shell_NotifyIcon(NIM_DELETE, &minimized);
                        if (LOWORD (wparam) == IDMI_RESTORE)
                        {
                            ShowWindow(window, SW_RESTORE);
                            SetForegroundWindow(window);
                        }
                        else if (LOWORD (wparam) == IDMI_CLOSE)
                        {
                            PostMessage(window, WM_CLOSE, 0, 0);
                        }
                    }
                    break;
                }
    
                switch(LOWORD(wparam))
                {
                    case ID_FILE_EXIT:
                    {
                        PostMessage(window, WM_CLOSE, 0, 0);
                    }
                    break;
                }
            }
            break;
    
            case WM_SIZE:
            {
                switch (wparam)
                {
                    case SIZE_MINIMIZED:
                    {
                        minimized.cbSize = sizeof(NOTIFYICONDATA);
                        minimized.hWnd = window;
                        minimized.uID = IDI_TRAY;
                        minimized.uFlags = NIF_ICON+NIF_MESSAGE+NIF_TIP;
                        minimized.uCallbackMessage = WM_SHELLNOTIFY;
                        minimized.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
                        lstrcpy(&minimized.szTip[0], "ADVERSARY");
                        ShowWindow(window, SW_HIDE);
                        Shell_NotifyIcon(NIM_ADD, &minimized);
                        return true;
                    }
                    break;
                }
            }
    
            case WM_SHELLNOTIFY:
            {
                switch (wparam)
                {
                    case IDI_TRAY:
                    {
                        if (lparam == WM_RBUTTONDOWN)
                        {
                            GetCursorPos(&pt);
                            TrackPopupMenu(hPopupMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, window, NULL);
                            return true;
                        }
                        else if (lparam == WM_LBUTTONDOWN)
                        {
                            SendMessage(window, WM_COMMAND, IDMI_RESTORE, 0);
                            return true;
                        }
                    }
                    default:
                    {
                        DefWindowProc(window, message, wparam, lparam);
                        return true;
                    }
                }
            }
            break;
    
            case WM_CLOSE:
                DestroyWindow(window);
            break;
    
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
    
            default:
            return DefWindowProc(window, message ,wparam, lparam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX info;
        MSG message;
        HWND window;
    
        info.cbSize = sizeof(WNDCLASSEX);
        info.style = 0;
        info.lpfnWndProc = WndProc;
        info.cbClsExtra = 0;
        info.cbWndExtra = 0;
        info.hInstance = hInstance;
        info.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
        info.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
        info.hCursor = LoadCursor(NULL, IDC_ARROW);
        info.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
        info.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
        info.lpszClassName = g_szClassName;
    
        if (!RegisterClassEx(&info))
        {
            MessageBox(NULL,"Window Registration Failed!","ERROR", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        window = CreateWindowEx(0, g_szClassName, WINDOW_TITLE,
                                WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
                                CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HIEGHT,
                                NULL, NULL, hInstance, NULL);
    
    
        if (window == NULL)
        {
            MessageBox(NULL,"Window Creation Failed!","ERROR", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(window,nCmdShow);
        UpdateWindow(window);
    
        while (GetMessage(&message,NULL,0,0) > 0)
        {
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
    
        return message.wParam;
    }

    Resource.h
    Code:
    #ifndef RESOURCE_H
    #define RESOURCE_H
    
    #define IDC_STATIC -1
    
    // Main Window Properties
    
    #define WINDOW_TITLE "ADVERSARY PUG-BOT"
    #define WINDOW_HIEGHT 480
    #define WINDOW_WIDTH 640
    
    //Icon System Tray Options
    
    #define IDI_TRAY 0
    #define IDMI_RESTORE 1001
    #define IDMI_CLOSE 1002
    #define WM_SHELLNOTIFY WM_USER+1
    
    #define IDI_MYICON 201
    
    // Menu 
    #define IDR_MYMENU 4000
    	#define ID_FILE_EXIT	4001
    	#define ID_FILE_LOAD 4002
    	#define ID_FILE_SAVE 4003
    	
    	#define ID_IRC_CONNECT 4010
    	#define ID_IRC_DISCONNECT 4011
    	#define ID_IRC_CONFIGURE 4012
    	#define ID_IRC_DEFAULT_THEME 4013
    		
    	#define ID_HELP_ABOUT 4030
    	#define ID_HELP_HELP 4031
    	
    // Widgets and Stuff
    #define IDD_NAME			4999
    
    #define IDD_SERVERBOX 	5001
    #define IDD_PORTBOX   	5002
    #define IDD_CHANNELBOX	5003
    #define IDD_NICKBOX   	5004
    #define IDD_IDENTBOX   	5005
    
    #define IDD_RECVBOX 		5010
    
    #define IDD_SENDBOX	  	5020
    #define IDD_SENDBUTTON 	5021
    
    #define IDD_CONNECT 		5050
    
    
    
    //User Defined
    
    #define PUGBOT_SOCKET_MSG (WM_USER + 100)
    
    
    #endif

    Resource.rc
    Code:
    #include <windows.h>
    
    #include "Resource.h"
    
    #if !defined IDC_STATIC
    	#define IDC_STATIC -1
    #endif
    
    // ICON
    IDI_MYICON ICON "OptimusPrime2.ico"
    
    
    //Top Menu
    IDR_MYMENU MENU
    BEGIN
    	POPUP "&File"
        	BEGIN
            	MENUITEM "&Load", ID_FILE_LOAD
            	MENUITEM "&Save", ID_FILE_SAVE
            	MENUITEM SEPARATOR
            	MENUITEM "E&xit", ID_FILE_EXIT
        	END
       POPUP "&IRC"
       	BEGIN
       		MENUITEM "&Connect", ID_IRC_CONNECT 
       		MENUITEM "&Disconnect", ID_IRC_DISCONNECT
       		MENUITEM SEPARATOR
       		MENUITEM "Con&figure", ID_IRC_CONFIGURE
       		MENUITEM SEPARATOR
       		MENUITEM "&Default", ID_IRC_DEFAULT_THEME
       		MENUITEM "Theme 1", ID_IRC_DEFAULT_THEME
       		MENUITEM "Theme 2", ID_IRC_DEFAULT_THEME
       		MENUITEM "Theme 3", ID_IRC_DEFAULT_THEME
        	END
       POPUP "&Help"
        	BEGIN
        		MENUITEM "&Help", ID_HELP_HELP
        		MENUITEM SEPARATOR
        		MENUITEM "&About Adversary . . .", ID_HELP_ABOUT
        	END
    END
    My first questions is how does one use a resource file to add buttons, edit boxes, static text to the main window? I can create them in the "case WM_CREATE:" section of the code but is it possible to do it in the resource.rc file to make the code look cleaner and how would I do this?

    I have been using http://www.winprog.org/tutorial/ tutorials but it only goes over how to create dialog boxes. I do not want to create the dialogs (buttons/text edits) in a whole new seperate window I want to create it on the main window.

    Thank you for your help.
    Last edited by rainmanddw; 04-10-2006 at 12:17 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>how does one use a resource file to add buttons, edit boxes, static text to the main window?<<

    As you have done, in your WM_CREATE handler using CreateWindowEx. If you want to create controls as part of a resource script then they must be created as part of a dialog resource - see the DIALOGEX resource definition statement on msdn for details.

    >>I do not want to create the dialogs (buttons/text edits) in a whole new seperate window I want to create it on the main window.<<

    One possibility is to create the dialog without any border styles and then simply attach it to the client of your main window but that still requires that you create the dialog and effectively treat that aspect of your application as a dialog box. Unless your main window has some special attributes, which from a cursory glance at your code doesn't appear to be the case, you could always just dump the main parent window and run with the resource generated dialog box.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buttons + Edit Control
    By jay kay in forum Windows Programming
    Replies: 6
    Last Post: 03-09-2005, 05:36 PM
  2. confused about adding controls to main window
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2004, 12:35 PM
  3. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 06:55 PM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM
  5. Password Edit Boxes
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 08-27-2001, 03:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21