Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Win32 API Chat] Defpushbutton and EditControl  (Read 2465 times)

0 Members and 1 Guest are viewing this topic.

SEnergy

  • Newbie
  • *
  • Posts: 20
    • View Profile
[Win32 API Chat] Defpushbutton and EditControl
« on: June 09, 2012, 08:41:38 pm »
Hello,

I wanted to include chat in my game, I've did a similiar chat to the one that's already working, however when I press enter it wont "simulate" press of the "send" button like it is in my chat, I've did everything like in my chat, even resources, but I have no idea why is this happening, when I press enter it will do a "beep" and nothing will happen, I have to manually press "Send" button, I'm not sure if this is caused because of SFML or there's something wrong in my code

My Game so far:

Source.cpp
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <Windows.h>
#include "Animation.h"
#include "resource.h"
using namespace sf;

/*
default:
right: 0
left : 1
down : 2
up   : 3

Moving:
right: 4
Left : 5
Down : 6
Up   : 7
*/


bool visible = false;

#pragma comment (lib, "sfml-window.lib")
#pragma comment (lib, "sfml-graphics.lib")
#pragma comment (lib, "sfml-system.lib")

BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

#define MY_WM_INITDIALOG (WM_USER + 1)


int wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, INT nShowCmd)
{
        UNREFERENCED_PARAMETER(hPrevInst);
        UNREFERENCED_PARAMETER(lpCmdLine);

        WNDCLASSEX wClass;
        ZeroMemory(&wClass,sizeof(WNDCLASSEX));
        wClass.cbClsExtra=0;
        wClass.cbSize=sizeof(WNDCLASSEX);
        wClass.cbWndExtra=DLGWINDOWEXTRA;
        wClass.hbrBackground=(HBRUSH)(COLOR_BTNFACE + 1);
        wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
        wClass.hIcon=NULL;
        wClass.hIconSm=NULL;
        wClass.hInstance=hInst;
        wClass.lpfnWndProc=(WNDPROC)MainDlgProc;
        wClass.lpszClassName="LD Class";
        wClass.lpszMenuName=NULL;
        wClass.style=CS_HREDRAW | CS_VREDRAW;

        if(!RegisterClassEx(&wClass))
        {
                int nResult = GetLastError();
                MessageBox(NULL, "Window Class Creation Failed!", NULL, MB_ICONERROR | MB_OK);
                return 0;
        }

        HWND hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_MAIN1), NULL, NULL);

        if(!hWnd)
        {
                int nResult = GetLastError();
                MessageBox(NULL, "Dialog Creation Failed!", NULL, MB_ICONERROR | MB_OK);
                return 0;
        }

        DWORD Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;
        HWND View = CreateWindow("STATIC", NULL, Style, 0, 0, 800, 600, hWnd, NULL, hInst, NULL);
        RenderWindow MainWindow(View);

        MainWindow.create(View);

        bool left = false, right = false, up = false, down = false;

        Texture tBackground;
        tBackground.loadFromFile("Background.png");
        Sprite sBackground(tBackground);

        Animation anim;
        anim.Load("Animation/Right Stand.png", 0);
        anim.Load("Animation/Right Move.png", 4);
        anim.Swap(0,0);

        MSG msg;
        ZeroMemory(&msg,sizeof(MSG));

        msg.message = ~WM_QUIT;

        while (msg.message != WM_QUIT)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
                        if(Keyboard::isKeyPressed(Keyboard::Left))
                        {
                                left = true;
                                right = false;
                                up = false;
                                down = false;
                        }
                        if(Keyboard::isKeyPressed(Keyboard::Right))
                        {
                                right = true;
                                left = false;
                                up = false;
                                down = false;
                        }
                        if(Keyboard::isKeyPressed(Keyboard::Up))
                        {
                                up = true;
                                right = false;
                                left = false;
                                down = false;
                        }
                        if(Keyboard::isKeyPressed(Keyboard::Down))
                        {
                                down = true;
                                right = false;
                                left = false;
                                up = false;
                        }

                        if(left)
                        {
                                anim.Move(Vector2f(-0.2f, 0), 'l');
                        }
                        if(right)
                        {
                                anim.Move(Vector2f(0.2f, 0), 'r');
                        }
                        if(up)
                        {
                                anim.Move(Vector2f(0, -0.2f), 'u');
                        }
                        if(down)
                        {
                                anim.Move(Vector2f(0, 0.2f), 'd');
                        }
               
                        left = false;
                        right = false;
                        up = false;
                        down = false;
               
                        MainWindow.clear();

                        MainWindow.draw(sBackground);

                        anim.Draw(MainWindow);

                        MainWindow.display();

                        anim.ResetFrame();
        }
    }
        return msg.wParam;
}

BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        switch(msg)
        {
        case MY_WM_INITDIALOG:
                {
                }
                break;
        case WM_CREATE:
                {
                        PostMessage(hWnd, MY_WM_INITDIALOG, 0, 0);
                }
                break;
        case WM_DESTROY:
                {
                        PostQuitMessage(0);
                        return 0;
                }
                break;
        case WM_COMMAND:
                {
                        switch(LOWORD(wParam))
                        {
                        case IDC_CHAT1:
                                {
                                        if(visible)
                                        {
                                                visible = false;
                                                HWND hOutput = GetDlgItem(hWnd, IDC_OUTPUT1);
                                                HWND hMessage = GetDlgItem(hWnd, IDC_MESSAGE1);
                                                HWND hSend = GetDlgItem(hWnd, IDC_SEND1);
                                                ShowWindow(hOutput, SW_HIDE);
                                                ShowWindow(hMessage, SW_HIDE);
                                                ShowWindow(hSend, SW_HIDE);
                                        }
                                        else
                                        {
                                                visible = true;
                                                HWND hOutput = GetDlgItem(hWnd, IDC_OUTPUT1);
                                                HWND hMessage = GetDlgItem(hWnd, IDC_MESSAGE1);
                                                HWND hSend = GetDlgItem(hWnd, IDC_SEND1);
                                                ShowWindow(hOutput, SW_SHOW);
                                                ShowWindow(hMessage, SW_SHOW);
                                                ShowWindow(hSend, SW_SHOW);
                                                SetFocus(hSend);
                                        }
                                }
                        case IDC_SEND1:
                                {
                                        char buffer[128];
                                        GetDlgItemText(hWnd, IDC_MESSAGE1, buffer, sizeof(buffer)/sizeof(buffer[0]));
                                        if(strcmp(buffer, "") != 0)
                                        {
                                                bool cansend = false;
                                                for(unsigned int i = 0; i < strlen(buffer); i++)
                                                {
                                                        if(buffer[i] != ' ')
                                                        {
                                                                cansend = true;
                                                                break;
                                                        }
                                                }
                                                if(cansend == true)
                                                {
                                                        HWND hOutput = GetDlgItem(hWnd, IDC_OUTPUT1);
                                                        HWND hMessage = GetDlgItem(hWnd, IDC_MESSAGE1);
                                                        int Textlen = SendMessage(hOutput, WM_GETTEXTLENGTH, 0, 0);
                                                        SendMessage(hOutput, EM_SETSEL, Textlen, Textlen);
                                                        SendMessage(hOutput, EM_REPLACESEL, TRUE, (LPARAM)buffer);
                                                        SendMessage(hOutput, EM_REPLACESEL, TRUE, (LPARAM)"\n");
                                                        Textlen = SendMessage(hMessage, WM_GETTEXTLENGTH, 0, 0);
                                                        SendMessage(hMessage, EM_SETSEL, 0, Textlen);
                                                        SendMessage(hMessage, EM_REPLACESEL, TRUE, (LPARAM)"");
                                                }
                                        }
                                        break;
                                }
                        }
                        break;
                }
        }
        return DefWindowProc(hWnd, msg, wParam, lParam);
}

Resource.rc
// Generated by ResEdit 1.5.11
// Copyright (C) 2006-2012
// http://www.resedit.net

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"




//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_MAIN1 DIALOG 0, 0, 533, 369
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Lucid Dreaming"
CLASS "LD Class"
FONT 8, "Ms Shell Dlg"
{
    EDITTEXT        IDC_MESSAGE1, 17, 353, 163, 14, NOT WS_VISIBLE | ES_AUTOHSCROLL
    DEFPUSHBUTTON   "Send", IDC_SEND1, 181, 353, 22, 14, NOT WS_VISIBLE
    EDITTEXT        IDC_OUTPUT1, 17, 268, 163, 83, NOT WS_VISIBLE | ES_AUTOHSCROLL | ES_MULTILINE | ES_READONLY
    PUSHBUTTON      "C", IDC_CHAT1, 1, 268, 15, 14
}
 

My Chat:

main.cpp
#include <winsock2.h>
#include <Windows.h>
#include "resource.h"
#include <string>
#include <mysql++.h>
#include <manip.h>
#include <sstream>
#include <fstream>
#include <winsock2.h>
#include <Ws2tcpip.h>

using namespace std;

//DB

//deleted now

// Server

#define DN "localhost"

#define WM_DNS (WM_USER + 2)
#define WM_DATA (WM_USER + 3)

#define TCP_PORT 5000
#define BUFSIZE 10240
#define TEXT_SIZE 102400

HANDLE idTask;
SOCKET sock = INVALID_SOCKET;

char buf[MAXGETHOSTSTRUCT];
char textBuffer[TEXT_SIZE];

HWND mainhWnd;

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")


char Username[16];
char Password[256];

char DBPassword[256];

char cUsername[16];
char cPassword[256];
char cPasswordc[256];
char cEmail[32];

int Perm = 0;
int iRet;

string sPassword;
char scPassword[256];

string wholeMessage;

void AppendText(HWND hDlg, char buffer[TEXT_SIZE], int size);
string MakeText(char buffer[TEXT_SIZE]);
int CreateConnectDlg(HINSTANCE hInst);
void CreateRegDlg(HINSTANCE hInst);
int CreateAccount(HWND hWnd, char cUsername[16], char cPassword[256], char cEmail[32]);
int Login(HWND hWnd, char Username[16], char Password[256]);
void Encode(char Message[1024]);
void Decode(char Message[1024]);
void On_DrawItem(LPDRAWITEMSTRUCT lpDIS);

BOOL CALLBACK ConnectDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK MainDlgProc(HWND hMain, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK RegisterDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

HINSTANCE hInst2;

void CreateRegDlg(HINSTANCE hInst)
{
        DialogBox(hInst, MAKEINTRESOURCE(IDD_REGISTER1), NULL, RegisterDlgProc);
        CreateConnectDlg(hInst);
}

int CreateConnectDlg(HINSTANCE hInst)
{
        iRet = 0;
        iRet = DialogBox(hInst, MAKEINTRESOURCE(IDD_CONNECT1), NULL, ConnectDlgProc);
        if(iRet == IDC_REGISTER1)
                CreateRegDlg(hInst);
        return 0;
}

#define MY_WM_INITDIALOG (WM_USER + 1)

INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, INT nShowCmd)
{
        UNREFERENCED_PARAMETER(hPrevInst);
        UNREFERENCED_PARAMETER(lpCmdLine);

        hInst2 = hInst;

        CreateConnectDlg(hInst);
        if(iRet == IDCANCEL)
                return 0;
       
        HMODULE hmodRichEdit = LoadLibrary("Riched20.dll");
        _ASSERTE(NULL != hmodRichEdit);

        WNDCLASSEX wClass;
        ZeroMemory(&wClass,sizeof(WNDCLASSEX));
        wClass.cbClsExtra=0;
        wClass.cbSize=sizeof(WNDCLASSEX);
        wClass.cbWndExtra=DLGWINDOWEXTRA;
        wClass.hbrBackground=(HBRUSH)(COLOR_BTNFACE + 1);
        wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
        wClass.hIcon=(HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTCOLOR);
        wClass.hIconSm=(HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON2), IMAGE_ICON, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTCOLOR);
        wClass.hInstance=hInst;
        wClass.lpfnWndProc=(WNDPROC)MainDlgProc;
        wClass.lpszClassName="Window Class";
        wClass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
        wClass.style=CS_HREDRAW | CS_VREDRAW;

        if(!RegisterClassEx(&wClass))
        {
                int nResult = GetLastError();
                MessageBox(NULL, "Window Class Creation Failed!", NULL, MB_ICONERROR | MB_OK);
                return 0;
        }

        mainhWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_CHAT1), NULL, NULL);

        if(!mainhWnd)
        {
                int nResult = GetLastError();
                MessageBox(NULL, "Dialog Creation Failed!", NULL, MB_ICONERROR | MB_OK);
                return 0;
        }

        MSG msg;
        ZeroMemory(&msg,sizeof(MSG));

        while(GetMessage(&msg, NULL, 0, 0) == TRUE)
        {
                if(!IsDialogMessage(mainhWnd, &msg))
                {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                }
        }
        return msg.wParam;
}

BOOL CALLBACK MainDlgProc(HWND hMain, UINT msg, WPARAM wParam, LPARAM lParam)
{
        switch(msg)
        {
        case MY_WM_INITDIALOG:
                {
                        WORD wVersionRequested = MAKEWORD(2,2);
                        WSAData data;
                        if (WSAStartup(wVersionRequested, &data) != 0){
                                return 0;
                        }
                        memset(buf, 0, MAXGETHOSTSTRUCT);
                        if ((idTask = WSAAsyncGetHostByName(hMain, WM_DNS, DN, buf, MAXGETHOSTSTRUCT)) == 0){          
                                return 0;
                        }
                        HWND TextMessageBox = GetDlgItem(hMain, IDC_MESSAGE1);
                        SetFocus(TextMessageBox);
                        HWND hList = GetDlgItem(hMain, IDC_LIST1);
                        if(Perm == 3)
                        {
                                char AUsername[30];
                                wsprintf(AUsername, "*%s", Username);
                                SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)AUsername);
                        }
                        else
                                SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)Username);
                }
                break;
        case WM_DNS:
                {
                        if(idTask == (HWND)wParam)
                        {
                                struct hostent *host = (struct hostent *) buf;
                                struct sockaddr *addr = NULL;
                                struct sockaddr_in addr_ip4;
                                size_t addrLen = 0;
                                if(WSAGETASYNCERROR(lParam) != 0)
                                {
                                        return 0;
                                }
                                addr_ip4.sin_family = host->h_addrtype;
                                addr_ip4.sin_port = htons(TCP_PORT);
                                memcpy(&(addr_ip4.sin_addr), host->h_addr_list[0], host->h_length);
                                addr = (struct sockaddr *)&addr_ip4;
                                addrLen = sizeof(addr_ip4);
                                if ((sock = socket(host->h_addrtype, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET){
                                        return 0;
                                }
                                if (connect(sock, addr, addrLen) == SOCKET_ERROR){
                                        return 0;
                                }
                                if (WSAAsyncSelect(sock, hMain, WM_DATA, FD_READ) == SOCKET_ERROR){
                                        return 0;
                                }
                        }
                        char buffer[TEXT_SIZE];
                        char CommandConnected[] = "Command.User.Connected";
                        wsprintf(buffer, "%s %s", CommandConnected, Username);
                        if(send(sock, buffer, strlen(buffer)+1, 0) == SOCKET_ERROR)
                        {
                                MessageBox(hMain, "Nejde odoslat data\r\n", "Error", MB_OK);
                                return 0;
                        }
                }
        case WM_DATA:
                {
                        if (WSAGETSELECTEVENT(lParam) == FD_READ)
                        {
                                int size;
                                char buffer[BUFSIZE];
                                if ((size = recv(sock, buffer, BUFSIZE, 0)) == SOCKET_ERROR)
                                {
                                        WSAAsyncSelect(sock, hMain, 0, 0);
                                        strncpy(buffer, "Nejde prijat data\r\n", BUFSIZE);
                                        send(sock, buffer, strlen(buffer), 0);
                                        shutdown(sock, SD_BOTH);
                                        closesocket(sock);
                                }
                                AppendText(hMain, buffer, size);
                }
                        break;
                }
        case WM_CREATE:
                {
                        PostMessage(hMain, MY_WM_INITDIALOG, 0, 0);
                }
                break;
        case WM_DESTROY:
                {
                        if(sock != INVALID_SOCKET)
                        {
                                closesocket(sock);
                        }
                        WSACleanup();
                        PostQuitMessage(0);
                        return 0;
                }
                break;
        case WM_COMMAND:
                {
                        switch(LOWORD(wParam))
                        {
                        case IDOK:
                                {
                                        HWND TextMessageBox = GetDlgItem(hMain, IDC_MESSAGE1);
                                        char buffer[TEXT_SIZE];
                                        char BanCommand[] = "/ban";
                                        GetDlgItemText(hMain, IDC_MESSAGE1, buffer, sizeof(buffer)/sizeof(buffer[0]));
                                        if(strcmp(buffer, "") != 0)
                                        {
                                                bool cansend = false;
                                                for(unsigned int i = 0; i < strlen(buffer); i++)
                                                {
                                                        if(buffer[i] != ' ')
                                                        {
                                                                cansend = true;
                                                                break;
                                                        }
                                                }
                                                if(cansend == true)
                                                {
                                                        string str = MakeText(buffer);
                                                        memset(buffer, 0, sizeof(buffer));
                                                        strncpy(buffer, str.c_str(), _countof(buffer) - 1);
                                                        int size = strlen(buffer);
                                                        if(send(sock, buffer, size+1, 0) == SOCKET_ERROR)
                                                        {
                                                                char error[] = "Nejde odoslat data\r\n";
                                                                strcpy(buffer, error);
                                                                size = strlen(error);
                                                        }
                                                        AppendText(hMain, buffer, size);
                                                }
                                        }
                                        SetDlgItemText(hMain, IDC_MESSAGE1, "");
                                        SetFocus(TextMessageBox);
                                }
                                break;
                        case IDM_BAN1:
                                {
                                        HWND TextMessageBox = GetDlgItem(hMain, IDC_MESSAGE1);
                                        EnableWindow(TextMessageBox, FALSE);
                                }
                                break;
                        case IDM_UNBAN1:
                                {
                                        HWND TextMessageBox = GetDlgItem(hMain, IDC_MESSAGE1);
                                        EnableWindow(TextMessageBox, TRUE);
                                }
                                break;
                        }
                        break;
                }
        }
        return DefWindowProc(hMain, msg, wParam, lParam);
}


Resource.rc
// Generated by ResEdit 1.5.8
// Copyright (C) 2006-2011
// http://www.resedit.net

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"



//
// Menu resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_MENU1 MENU
{
    POPUP "File"
    {
        MENUITEM "Ban", IDM_BAN1
        MENUITEM "Unban", IDM_UNBAN1
    }
}



//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_CHAT1 DIALOG 0, 0, 325, 169
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU | WS_MINIMIZEBOX
CAPTION "Chat"
CLASS "Window Class"
FONT 8, "Ms Shell Dlg"
{
    EDITTEXT        IDC_MESSAGE1, 9, 136, 252, 14, ES_AUTOHSCROLL
    DEFPUSHBUTTON   "Send", IDOK, 267, 136, 50, 14
    LISTBOX         IDC_LIST1, 267, 10, 50, 119, WS_TABSTOP | WS_VSCROLL | LBS_NOINTEGRALHEIGHT | LBS_NOTIFY
    CONTROL         "", IDC_OUTPUT1, RICHEDIT_CLASS, WS_TABSTOP | WS_VSCROLL | WS_BORDER | ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY, 9, 10, 252, 119
}