Jump to content

About to finish it just need a push ! (Win32 + C )

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
#include <windows.h>
#include <stdio.h>
#include <string.h>
#define ID_EDIT 1
#define ID_BUTTON 2
#define ID_TEXT 3
#define ARRAY_SIZE 10000;
static int len;
char str[900];
int i;
char temp[1000000];
char Arry[20];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
            LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Static Control" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0,IDC_ARROW);


  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Change Text"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                100, 100, 500, 500, 0, 0, hInstance, 0);

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

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  static HWND hwndEdit;
  static HWND hwndEdit2;
  static HWND hwndButton;
  switch(msg)
  {
    case WM_CREATE:
    {

        hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
                10, 30, 465, 100, hwnd, (HMENU) ID_EDIT,
                NULL, NULL);

        hwndButton = CreateWindow( TEXT("button"), TEXT("Color Me !"),
            WS_VISIBLE | WS_CHILD,
            10,140,465,25,
            hwnd, (HMENU) ID_BUTTON, NULL, NULL);

        hwndEdit2 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
                10, 178,465, 270, hwnd, (HMENU) ID_TEXT,
                NULL, NULL);

        break;
    }
    case WM_COMMAND:
        if(HIWORD(wParam) == BN_CLICKED){
            len = GetWindowTextLength(hwndEdit) + 1;
            GetWindowText(hwndEdit, (LPWSTR)str, len);
            for(i=0; i<len; i++)
            {
[COLOR=Black]               [/COLOR][COLOR=Black] strcpy(temp, "[color=blue");
                strcpy(temp, "]");
[COLOR=Magenta]                strcpy(temp, (const char *)str[i]);[/COLOR]
                strcpy(temp, "[ / color]");[/COLOR]
            }
            SetWindowText(hwndEdit2, (LPCWSTR)temp);
        }
                break;
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
SO you can see that the problem is in the str[i];

i wonder why ! I've tried to cast it to (const char *) but it didn't work. so any idea's ?

Oh and i know i didn't use ARRAY_SIZE i'm using it later

#2
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
Thats because str[i] is a character. What exactly are you trying to do there? Im asking because I didn't really read your code, its a type problem. If you want to pass the part of the string starting at index i - you should call
strcpy(temp, &str[i]);
or
strcpy(temp, str+i); 
, don' know what's more readable.

#3
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
The point of this program is to :
1 Read the input from first texbox
2- pass it to a string and take every character and colorize it with BB codes and save every step in temp.
3- print it in second texbox.

Well with the code you supplied it doesn't crash anymore, but the output is chinese stuff

#4
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
Guys i finished it, i can't believe how happy i am D:
my first GUI'D PROGRAM.
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include<time.h>
#include<stdlib.h>
#define ID_EDIT 1
#define ID_BUTTON 2
#define ID_TEXT 3
#define ARRAY_SIZE 10000;
static int len;
char str[900];
int i;
char temp[10000000];
char *Arry[20][20];
int r;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
            LPSTR lpCmdLine, int nCmdShow )
{
    strcpy(Arry[1], "Antiquewhite");
    strcpy(Arry[2], "Aqua");
    strcpy(Arry[3], "Aquamarine");
    strcpy(Arry[4], "Azure");
    strcpy(Arry[5], "Beige");
    strcpy(Arry[6], "Bisque");
    strcpy(Arry[7], "Black");
    strcpy(Arry[8], "Blanchedalmond");
    strcpy(Arry[9], "Blue");
    strcpy(Arry[10], "Blueviolet");
    strcpy(Arry[11], "Brown");
    strcpy(Arry[12], "Burlywood");
    strcpy(Arry[13], "Chartreuse");
    strcpy(Arry[14], "Chocolate");
    strcpy(Arry[15], "Coral");
    strcpy(Arry[16], "Cornflowerblue");
    strcpy(Arry[17], "Cornsilk");
    strcpy(Arry[18], "Crimson");
    strcpy(Arry[19], "Darkcyan");
  MSG  msg ;
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Static Control" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0,IDC_ARROW);


  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Change Text"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                100, 100, 500, 500, 0, 0, hInstance, 0);

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

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  static HWND hwndEdit;
  static HWND hwndEdit2;
  static HWND hwndButton;
  switch(msg)
  {
    case WM_CREATE:
    {

        hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE |ES_MULTILINE|ES_AUTOHSCROLL|WS_HSCROLL|ES_AUTOHSCROLL|WS_BORDER,
                10, 30, 465, 100, hwnd, (HMENU) ID_EDIT,
                NULL, NULL);

        hwndButton = CreateWindow( TEXT("button"), TEXT("Color Me !"),
            WS_VISIBLE | WS_CHILD,
            10,140,465,25,
            hwnd, (HMENU) ID_BUTTON, NULL, NULL);

        hwndEdit2 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE |ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOHSCROLL|WS_HSCROLL|WS_BORDER,
                10, 178,465, 270, hwnd, (HMENU) ID_TEXT,
                NULL, NULL);

        break;
    }
    case WM_COMMAND:
        if(HIWORD(wParam) == BN_CLICKED){
            len = GetWindowTextLength(hwndEdit) + 1;
            GetWindowText(hwndEdit, str, len);
            for(i=0; i<len-1; i++)
            {
                srand(time(NULL));
                r= rand() % 19;
                strcat(temp,"[color=");
                strcat(temp, Arry[r]);
                strcat(temp, "]");
                strncat(temp, &str[i],1);
                strcat(temp, "[/color]");
            }
                SetWindowText(hwndEdit2, temp);

        }
                break;
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

All i need to do is work out a dynamic rand ...

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Your problem probably comes from using a char array when you should be using a wchar_t array. If you notice, you typecast temp as a LPCWSTR which stands for Long Pointer to Constant Wide-character STRing.This means you need to declare str and temp as arrays of wchar_t instead of char, and use the wide-character string functions wcscmp, wcscpy, and so on. Look here for more information about wide-character stuff.

EDIT: Oops - wrong and late. Well, I tried. In any case, I do think you should be using the wchar_t stuff because-- I think--things could go wrong with a char array if you're not careful. Although, if you're using the TEXT() macro then you should be using the TCHAR functions instead. Technically more correct, but I guess it's not necessary? Just my two cents.
sudo rm -rf /

#6
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
heheheh thanks anyway mate :)

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Not doing my reputation any good... :D
sudo rm -rf /

#8
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
You must spread some Reputation around before giving it to dargueta again.

Don't worry i already repped you like 2 days ago :P

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Thanks!
sudo rm -rf /