Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Struggling to read text from Edit Control (box)

Sonx_hvn7Sonx_hvn7 Member Posts: 54
Tried few attempts but not winning.. My code creates an edit box and a button. I expect to type some text on the edit box then after pressing the button the text should be displayed on the frame to show that it was read successfully... This is with no avail...

here's my code

[code]

// Creating Controls...
case WM_CREATE:
{
editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));

CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
IDBC_DEFPUSHBUTTON,_T("DISPLAY"));

return 0;
}

//after button press
case WM_COMMAND:
if (LOWORD(wParam) == IDBC_DEFPUSHBUTTON)
{
GetDlgItemText(hwnd, IDCE_DLG, szString, 80);
printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
}

// Function to display text
void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
{
HDC hdc = GetDC(NULL);
long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc);

HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
SelectObject(hDC, font);
SetBkColor(hDC, g_rgbBackground);
TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));
DeleteObject(font);
}
[/code]
I suspect it may have to do with the first argument of GetDlgItemText()... the handle

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Tried few attempts but not winning.. My code creates an edit box and
    : a button. I expect to type some text on the edit box then after
    : pressing the button the text should be displayed on the frame to
    : show that it was read successfully... This is with no avail...
    :
    : here's my code
    :
    : [code]:
    :
    : // Creating Controls...
    : case WM_CREATE:
    : {
    : editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    :
    : CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
    : IDBC_DEFPUSHBUTTON,_T("DISPLAY"));
    :
    : return 0;
    : }
    :
    : //after button press
    : case WM_COMMAND:
    : if (LOWORD(wParam) == IDBC_DEFPUSHBUTTON)
    : {
    : GetDlgItemText(hwnd, IDCE_DLG, szString, 80);
    : printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    : }
    :
    : // Function to display text
    : void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
    : {
    : HDC hdc = GetDC(NULL);
    : long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    : ReleaseDC(NULL, hdc);
    :
    : HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
    : SelectObject(hDC, font);
    : SetBkColor(hDC, g_rgbBackground);
    : TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));
    : DeleteObject(font);
    : }
    : [/code]:
    : I suspect it may have to do with the first argument of
    : GetDlgItemText()... the handle
    :
    [color=Blue]Same conceptual problem as you had before. When you need to draw something you need to have a matching data for that drawing. You need to reserve somewhere the area of memory accessible from a few places. Copy your text there and call InvalidateRect() to force drawing.

    The main concept of Windows is to prepare data and then refresh the area where that data is supposed to be painted. The painting should be done in a few places, like:

    - WM_PAINT (dialog may not have this message sent to it)
    - WM_ERASEBKGND (dialog has this one)

    There is also other (easier) way to "paint" it - create a static text control in your dialog and send it any text - it will be visible automatically.

    If you still want to draw manually on a surface of a dialog - I suggest WM_ERASEBKGND message. However, then you need to draw also a background of a dialog first and then your text. Also, if you have more controls on a dialog - you should respond to WM_CTLCOLOR messages from these controls.

    P.S. In your drawing code - you did not deselect back the font. Also you paint on a screen DC (GetDC(NULL) returns screen DC) - this may not be good, if you painting the dialog - get its DC (GetDC ())
    [/color]
  • Sonx_hvn7Sonx_hvn7 Member Posts: 54
    : : Tried few attempts but not winning.. My code creates an edit box and
    : : a button. I expect to type some text on the edit box then after
    : : pressing the button the text should be displayed on the frame to
    : : show that it was read successfully... This is with no avail...
    : :
    : : here's my code
    : :
    : : [code]: :
    : :
    : : // Creating Controls...
    : : case WM_CREATE:
    : : {
    : : editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    : :
    : : CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
    : : IDBC_DEFPUSHBUTTON,_T("DISPLAY"));
    : :
    : : return 0;
    : : }
    : :
    : : //after button press
    : : case WM_COMMAND:
    : : if (LOWORD(wParam) == IDBC_DEFPUSHBUTTON)
    : : {
    : : GetDlgItemText(hwnd, IDCE_DLG, szString, 80);
    : : printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    : : }
    : :
    : : // Function to display text
    : : void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
    : : {
    : : HDC hdc = GetDC(NULL);
    : : long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    : : ReleaseDC(NULL, hdc);
    : :
    : : HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
    : : SelectObject(hDC, font);
    : : SetBkColor(hDC, g_rgbBackground);
    : : TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));
    : : DeleteObject(font);
    : : }
    : : [/code]: :
    : : I suspect it may have to do with the first argument of
    : : GetDlgItemText()... the handle
    : :
    : [color=Blue]Same conceptual problem as you had before. When you need
    : to draw something you need to have a matching data for that drawing.
    : You need to reserve somewhere the area of memory accessible from a
    : few places. Copy your text there and call InvalidateRect() to force
    : drawing.
    :
    : The main concept of Windows is to prepare data and then refresh the
    : area where that data is supposed to be painted. The painting should
    : be done in a few places, like:
    :
    : - WM_PAINT (dialog may not have this message sent to it)
    : - WM_ERASEBKGND (dialog has this one)
    :
    : There is also other (easier) way to "paint" it - create a static
    : text control in your dialog and send it any text - it will be
    : visible automatically.
    :
    : If you still want to draw manually on a surface of a dialog - I
    : suggest WM_ERASEBKGND message. However, then you need to draw also a
    : background of a dialog first and then your text. Also, if you have
    : more controls on a dialog - you should respond to WM_CTLCOLOR
    : messages from these controls.
    :
    : P.S. In your drawing code - you did not deselect back the font. Also
    : you paint on a screen DC (GetDC(NULL) returns screen DC) - this may
    : not be good, if you painting the dialog - get its DC (GetDC ())
    : [/color]

    Thought i should show you my entire code

    [code]
    #include "stdafx.h"
    #include //include all the basics
    #include //string and other mapping macros
    #include

    // Define structure for colors
    typedef struct rgbColors {
    int R,G,B;
    } RGBColor;

    COLORREF g_rgbBackground = RGB(0xD3,0xD3,0xD3); // Set to grey

    #define MAXCOLOR 10

    //define an unicode string type alias
    typedef std::basic_string ustring;
    //=============================================================================
    //message processsing function declarations
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

    // function declarations
    inline int ErrMsg(const ustring&);
    HWND OnCreate(const HWND,CREATESTRUCT*, RECT& rc);
    RECT initRecArea(int left, int top, int width, int height);
    void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC);
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index);
    void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc);
    HWND CreateEdit(const HWND,const HINSTANCE,DWORD,const RECT&,const int, const ustring&);
    HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    const RECT& rc,const int id,const ustring& caption);

    //setup some edit control id's
    enum {
    IDCE_SINGLELINE=200,
    IDCE_MULTILINE,
    IDCE_DLG,
    IDBC_DEFPUSHBUTTON,
    IDBC_PUSHBUTTON,
    IDBC_AUTOCHECKBOX,
    IDBC_AUTORADIOBUTTON,
    IDBC_GROUPBOX,
    IDBC_ICON,
    IDBC_BITMAP
    };

    // Globals
    PAINTSTRUCT pntS;
    HWND editCntrl;

    //=============================================================================
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
    {
    ustring classname=_T("RFS Range");
    WNDCLASSEX wcx={0}; //used for storing information about the wnd 'class'

    wcx.cbSize = sizeof(WNDCLASSEX);
    wcx.lpfnWndProc = WndProc;
    wcx.hInstance = hInst;
    wcx.hIcon = (HICON)LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED);
    wcx.hCursor = (HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED);
    wcx.hbrBackground = reinterpret_cast(COLOR_BTNFACE+1);
    wcx.lpszClassName = classname.c_str();

    if (!RegisterClassEx(&wcx))
    {
    ErrMsg(_T("Failed to register wnd class"));
    return -1;
    }

    int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
    int desktopheight=GetSystemMetrics(SM_CYSCREEN);

    HWND hwnd=CreateWindowEx(0,
    classname.c_str(),
    _T("RFS Range"),
    WS_OVERLAPPEDWINDOW,
    desktopwidth/4,
    desktopheight/4,
    desktopwidth/2,
    desktopheight/2,
    0,
    0,
    hInst,
    0);

    if (!hwnd)
    {
    ErrMsg(_T("Failed to create wnd"));
    return -1;
    }

    ShowWindow(hwnd,nCmd);
    UpdateWindow(hwnd);
    MSG msg;

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

    //=============================================================================
    LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    int pos=0;
    HDC hdc;
    char szString[100];
    int maxChars = 20;
    HWND hWndDLG;
    RGBColor colors[MAXCOLOR] = {{0xD3,0xD3,0xD3},{0x00,0xFF,0x00},
    {0xFF,0xFF,0xFF},{0x00,0x00,0x00}};

    CREATESTRUCT *cs = reinterpret_cast(lParam);

    switch (uMsg)
    {
    case WM_CREATE:
    editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
    IDBC_DEFPUSHBUTTON,_T("DISPLAY"));
    return 0;

    case WM_PAINT:
    hdc = BeginPaint(hwnd, &pntS);

    // Draw unto the client area
    PaintObject(hwnd,initRecArea(10,10,300,200),colors, hdc, pos);
    printText(hwnd, "Hello there", initRecArea(15,15,0,0), hdc);
    PaintObject(hwnd,initRecArea(305,10,605,250),colors, hdc, pos);
    PaintObject(hwnd,initRecArea(10,205,300,315),colors, hdc, pos);
    PaintObject(hwnd,initRecArea(10,320,300,420),colors, hdc, pos);

    EndPaint(hwnd, &pntS);

    case WM_COMMAND:
    if (LOWORD(wParam) == IDBC_DEFPUSHBUTTON)
    {
    GetDlgItemText(hwnd, IDCE_DLG, szString, 80);
    printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    }

    return 0;

    case WM_TIMER:
    /* Force the WM_PAINT */
    //SetTimer(hwnd, ID_TIMER, 500, NULL);
    //InvalidateRect (hwnd, NULL, TRUE);
    //UpdateWindow(hwnd);
    break;

    case WM_DESTROY:
    PostQuitMessage(0); //signal end of application
    return 0;
    default:
    //let system deal with msg
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    }

    //=============================================================================
    inline int ErrMsg(const ustring& s)
    {
    return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
    }

    //=============================================================================
    // EDIT control
    HWND CreateEdit(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    const RECT& rc,const int id,const ustring& caption)
    {
    dwStyle|=WS_CHILD|WS_VISIBLE;
    return CreateWindowEx(WS_EX_CLIENTEDGE,
    _T("edit"),
    caption.c_str(),
    dwStyle,
    rc.left,
    rc.top,
    rc.right,
    rc.bottom,
    hParent,
    reinterpret_cast(static_cast(id)),
    hInst,
    0);
    }
    //=============================================================================
    // BUTTON Control
    HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    const RECT& rc,const int id,const ustring& caption)
    {
    dwStyle|=WS_CHILD|WS_VISIBLE;
    return CreateWindowEx(0,
    _T("button"),
    caption.c_str(),
    dwStyle,
    rc.left,
    rc.top,
    rc.right,
    rc.bottom,
    hParent,
    reinterpret_cast(static_cast(id)),
    hInst,
    0);
    }

    // Purpose: Draws an object on the window
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index)
    {
    HPEN hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
    HBRUSH hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));
    DrawARectangle(hwnd, hpen, hdc, hbrush, rc);
    }

    // Purpose: Draws a rectangle which will have text boxes for input data
    void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc)
    {
    // Select the new pen and brush, and then draw.
    HPEN hpenOld = (HPEN)SelectObject(hdc, hpen);
    HBRUSH hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
    Rectangle(hdc,rc.left,rc.top,rc.right,rc.bottom);

    // Do not forget to clean up.
    SelectObject(hdc, hpenOld);
    DeleteObject(hpen);
    SelectObject(hdc, hbrushOld);
    DeleteObject(hbrush);
    }

    // Initialize rectangle area
    RECT initRecArea(int left, int top, int width, int height)
    {
    RECT rec = {left, top, width, height};
    return rec;
    }

    //handles the WM_CREATE message of the main, parent window; return -1 to fail
    HWND OnCreate(const HWND hwnd,CREATESTRUCT *cs, RECT& rc)
    {
    return CreateEdit(hwnd,cs->hInstance,0,rc,IDCE_SINGLELINE,_T(""));
    }

    // Display text on screen
    void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
    {
    HDC hdc = GetDC(NULL);
    long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    ReleaseDC(NULL, hdc);

    HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
    SelectObject(hDC, font);
    SetBkColor(hDC, g_rgbBackground);
    TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));
    DeleteObject(font);
    }
    [/code]
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]Please, see my modifications:[/color]
    [code]
    [color=Red]// #include "stdafx.h"
    // [AK]: removed this ^^^ - was not compiling[/color]
    #include //include all the basics
    #include //string and other mapping macros
    #include

    // Define structure for colors
    typedef struct rgbColors {
    int R,G,B;
    } RGBColor;

    COLORREF g_rgbBackground = RGB(0xD3,0xD3,0xD3); // Set to grey

    #define MAXCOLOR 10

    //define an unicode string type alias
    typedef std::basic_string ustring;
    //================================================
    //message processsing function declarations
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

    // function declarations
    inline int ErrMsg(const ustring&);
    HWND OnCreate(const HWND,CREATESTRUCT*, RECT& rc);

    [color=Red]// [AK]: changed prototype to return a reference[/color]
    RECT& initRecArea(int left, int top, int width, int height);

    void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC);
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index);
    void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc);
    HWND CreateEdit(const HWND,const HINSTANCE,DWORD,const RECT&,const int, const ustring&);
    HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    const RECT& rc,const int id,const ustring& caption);

    //setup some edit control id's
    enum {
    IDCE_SINGLELINE=200,
    IDCE_MULTILINE,
    IDCE_DLG,
    IDBC_DEFPUSHBUTTON,
    IDBC_PUSHBUTTON,
    IDBC_AUTOCHECKBOX,
    IDBC_AUTORADIOBUTTON,
    IDBC_GROUPBOX,
    IDBC_ICON,
    IDBC_BITMAP
    };

    // Globals
    PAINTSTRUCT pntS;
    HWND editCntrl;

    //================================================
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
    {
    ustring classname=_T("RFS Range");
    WNDCLASSEX wcx={0}; //used for storing information about the wnd 'class'

    wcx.cbSize = sizeof(WNDCLASSEX);
    wcx.lpfnWndProc = WndProc;
    wcx.hInstance = hInst;
    wcx.hIcon = (HICON)LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED);
    wcx.hCursor = (HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED);
    wcx.hbrBackground = reinterpret_cast(COLOR_BTNFACE+1);
    wcx.lpszClassName = classname.c_str();

    if (!RegisterClassEx(&wcx))
    {
    ErrMsg(_T("Failed to register wnd class"));
    return -1;
    }

    int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
    int desktopheight=GetSystemMetrics(SM_CYSCREEN);

    int X = desktopwidth/4;
    int Y = desktopheight/4;

    int W = desktopwidth/2;
    int H = desktopheight/2;

    HWND hwnd=CreateWindowEx(0,
    classname.c_str(),
    _T("RFS Range"),
    WS_OVERLAPPEDWINDOW,
    X, //desktopwidth/4,
    Y, //desktopheight/4,
    W, //desktopwidth/2,
    H, //desktopheight/2,
    0,
    0,
    hInst,
    0);

    if (!hwnd)
    {
    ErrMsg(_T("Failed to create wnd"));
    return -1;
    }

    ShowWindow(hwnd,nCmd);
    UpdateWindow(hwnd);
    MSG msg;

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

    //================================================
    LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    int pos=0;
    HDC hdc;

    [color=Red]// [AK]: making it static, so the value in it will be preserved between calls.[/color]
    static TCHAR szString[100]; [color=Red]// [AK]: added proper TCHAR declaration[/color]

    int maxChars = 20;
    HWND hWndDLG;
    RGBColor colors[MAXCOLOR] = {{0xD3,0xD3,0xD3},{0x00,0xFF,0x00},
    {0xFF,0xFF,0xFF},{0x00,0x00,0x00}};

    CREATESTRUCT *cs = reinterpret_cast(lParam);

    switch (uMsg)
    {
    case WM_CREATE:
    editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
    IDBC_DEFPUSHBUTTON,_T("DISPLAY"));
    return 0;

    case WM_PAINT:
    hdc = BeginPaint(hwnd, &pntS);

    // Draw unto the client area
    PaintObject(hwnd,initRecArea(10,10,300,200),colors, hdc,pos);
    printText(hwnd, _T("Hello there"), initRecArea(15,15,0,0), hdc); [color=Red]// [AK]: added _T() macro[/color]
    PaintObject(hwnd,initRecArea(305,10,605,250),colors, hdc, pos);
    PaintObject(hwnd,initRecArea(10,205,300,315),colors, hdc, pos);
    PaintObject(hwnd,initRecArea(10,320,300,420),colors, hdc, pos);

    [color=Red]// [AK]: also draw what's in text buffer
    if (szString [0])
    {
    printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    }[/color]

    EndPaint(hwnd, &pntS);
    return 0; [color=Red]// [AK]: added return because code drops down to WM_COMMAND case[/color]

    case WM_COMMAND:
    [color=Red]// [AK]: issue in below statement. WM_COMMAND message packs into
    // WPARAM two values: ID is in LOW word and Notification code in HIGH word.
    // There may be a case when this button sends other messages beside clicks.
    // Take a look at this link: "http://msdn.microsoft.com/en-us/library/bb776944(VS.85).aspx"
    // You need only BN_CLICKED to process, so you have to check for both HIGH and LOW words.[/color]
    if ((LOWORD(wParam) == IDBC_DEFPUSHBUTTON) && (HIWORD (wParam) == BN_CLICKED))
    {
    [color=Red]// [AK]: IDCE_DLG control is never created. What's it for?
    // Replacing it with IDCE_SINGLELINE just to see the edit contents
    // painted on a dialog.
    //[/color]
    GetDlgItemText(hwnd, [color=Red]/*IDCE_DLG*/[/color] IDCE_SINGLELINE, szString, 80);
    [color=Red]// printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    // [AK]: ^^^ moved into WM_PAINT case

    // [AK]: once text is copied: force WM_PAINT
    InvalidateRect (hwnd, NULL, TRUE);[/color]
    }
    return 0;

    case WM_TIMER:
    /* Force the WM_PAINT */
    //SetTimer(hwnd, ID_TIMER, 500, NULL);
    //InvalidateRect (hwnd, NULL, TRUE);
    //UpdateWindow(hwnd);
    break; [color=Red]// [AK]: compiler warning here,
    // because there is no return after switch()[/color]

    case WM_DESTROY:
    PostQuitMessage(0); //signal end of application
    return 0;
    default:
    //let system deal with msg
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0; [color=Red]// [AK]: added to eliminate warnings[/color]
    }

    //================================================
    inline int ErrMsg(const ustring& s)
    {
    return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
    }

    //================================================
    // EDIT control
    HWND CreateEdit(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    const RECT& rc,const int id,const ustring& caption)
    {
    dwStyle|=WS_CHILD|WS_VISIBLE;
    return CreateWindowEx(WS_EX_CLIENTEDGE,
    _T("edit"),
    caption.c_str(),
    dwStyle,
    rc.left,
    rc.top,
    [color=Red]rc.right,
    rc.bottom,
    // ^^^ Possibly same fix as below is needed here too?[/color]
    hParent,
    reinterpret_cast(static_cast(id)),
    hInst,
    0);
    }
    //================================================
    // BUTTON Control
    HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    const RECT& rc,const int id,const ustring& caption)
    {
    dwStyle|=WS_CHILD|WS_VISIBLE;
    return CreateWindowEx(0,
    _T("button"),
    caption.c_str(),
    dwStyle,
    rc.left,
    rc.top,
    rc.right - rc.left, [color=Red]// [AK]: fixed the width[/color]
    rc.bottom - rc.top, [color=Red]// [AK]: fixed the height[/color]
    hParent,
    reinterpret_cast(static_cast(id)),
    hInst,
    0);
    }

    // Purpose: Draws an object on the window
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index)
    {
    HPEN hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
    HBRUSH hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));

    DrawARectangle(hwnd, hpen, hdc, hbrush, rc);

    [color=Red]// [AK]: moved it here to provide better maintenance.
    // Creating objects in one function and deleting them in
    // another function is a way for bad code in the long run.
    DeleteObject(hpen);
    DeleteObject(hbrush);[/color]
    }

    // Purpose: Draws a rectangle which will have text boxes for input data
    void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc)
    {
    // Select the new pen and brush, and then draw.
    HPEN hpenOld = (HPEN)SelectObject(hdc, hpen);
    HBRUSH hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
    Rectangle(hdc,rc.left,rc.top,rc.right,rc.bottom);

    // Do not forget to clean up.
    SelectObject(hdc, hpenOld);
    [color=Red]//DeleteObject(hpen); // [AK]: moved out[/color]
    SelectObject(hdc, hbrushOld);
    [color=Red]//DeleteObject(hbrush); // [AK]: moved out[/color]
    }

    // Initialize rectangle area
    [color=Red]// [AK]: refactored it to provide a reference, because of warnings
    RECT& initRecArea(int left, int top, int width, int height)
    {
    static RECT rec;

    rec.left = left;
    rec.top = top;

    // [AK]: not sure is these two are correct.
    // Technically speaking "right" coordinate is "width" only when "left"=0.
    rec.right = width;
    rec.bottom = height;

    // This line proves that you want the edit box starting at X=100 and Width=100!
    // editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    // So, I modified the code below to initialize RECT properly
    rec.right = width + left;
    rec.bottom = height + top;

    return rec;
    }
    // [AK]: end of refactoring[/color]

    //handles the WM_CREATE message of the main, parent window; return -1 to fail
    HWND OnCreate(const HWND hwnd,CREATESTRUCT *cs, RECT& rc)
    {
    return CreateEdit(hwnd,cs->hInstance,0,rc,IDCE_SINGLELINE,_T(""));
    }

    // Display text on screen
    void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
    {
    [color=Red]//HDC hdc = GetDC(NULL); // [AK]: not used[/color]
    long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    [color=Red]//ReleaseDC(NULL, hdc); // [AK]: not used[/color]

    HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE,
    0, 0, 0, 0, 0, 0, 0, _T("Times New Roman")); [color=Red]// [AK]: Added _T() macro[/color]

    [color=Red]HGDIOBJ hDefFont=SelectObject(hDC, font); // [AK]: remember the default font handle[/color]
    SetBkColor(hDC, g_rgbBackground);
    TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));

    [color=Red]// [AK]: pop the created font back from DC,
    // so it can be deleted properly
    SelectObject (hDC, hDefFont);[/color]

    DeleteObject(font);
    }
    [/code]
  • Sonx_hvn7Sonx_hvn7 Member Posts: 54
    : [color=Blue]Please, see my modifications:[/color]
    : [code]:
    : [color=Red]// #include "stdafx.h"
    : // [AK]: removed this ^^^ - was not compiling[/color]
    : #include //include all the basics
    : #include //string and other mapping macros
    : #include
    :
    : // Define structure for colors
    : typedef struct rgbColors {
    : int R,G,B;
    : } RGBColor;
    :
    : COLORREF g_rgbBackground = RGB(0xD3,0xD3,0xD3); // Set to grey
    :
    : #define MAXCOLOR 10
    :
    : //define an unicode string type alias
    : typedef std::basic_string ustring;
    : //================================================
    : //message processsing function declarations
    : LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    :
    : // function declarations
    : inline int ErrMsg(const ustring&);
    : HWND OnCreate(const HWND,CREATESTRUCT*, RECT& rc);
    :
    : [color=Red]// [AK]: changed prototype to return a reference[/color]
    : RECT& initRecArea(int left, int top, int width, int height);
    :
    : void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC);
    : void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index);
    : void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc);
    : HWND CreateEdit(const HWND,const HINSTANCE,DWORD,const RECT&,const int, const ustring&);
    : HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    : const RECT& rc,const int id,const ustring& caption);
    :
    : //setup some edit control id's
    : enum {
    : IDCE_SINGLELINE=200,
    : IDCE_MULTILINE,
    : IDCE_DLG,
    : IDBC_DEFPUSHBUTTON,
    : IDBC_PUSHBUTTON,
    : IDBC_AUTOCHECKBOX,
    : IDBC_AUTORADIOBUTTON,
    : IDBC_GROUPBOX,
    : IDBC_ICON,
    : IDBC_BITMAP
    : };
    :
    : // Globals
    : PAINTSTRUCT pntS;
    : HWND editCntrl;
    :
    : //================================================
    : int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
    : {
    : ustring classname=_T("RFS Range");
    : WNDCLASSEX wcx={0}; //used for storing information about the wnd 'class'
    :
    : wcx.cbSize = sizeof(WNDCLASSEX);
    : wcx.lpfnWndProc = WndProc;
    : wcx.hInstance = hInst;
    : wcx.hIcon = (HICON)LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED);
    : wcx.hCursor = (HCURSOR)LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED);
    : wcx.hbrBackground = reinterpret_cast(COLOR_BTNFACE+1);
    : wcx.lpszClassName = classname.c_str();
    :
    : if (!RegisterClassEx(&wcx))
    : {
    : ErrMsg(_T("Failed to register wnd class"));
    : return -1;
    : }
    :
    : int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
    : int desktopheight=GetSystemMetrics(SM_CYSCREEN);
    :
    : int X = desktopwidth/4;
    : int Y = desktopheight/4;
    :
    : int W = desktopwidth/2;
    : int H = desktopheight/2;
    :
    : HWND hwnd=CreateWindowEx(0,
    : classname.c_str(),
    : _T("RFS Range"),
    : WS_OVERLAPPEDWINDOW,
    : X, //desktopwidth/4,
    : Y, //desktopheight/4,
    : W, //desktopwidth/2,
    : H, //desktopheight/2,
    : 0,
    : 0,
    : hInst,
    : 0);
    :
    : if (!hwnd)
    : {
    : ErrMsg(_T("Failed to create wnd"));
    : return -1;
    : }
    :
    : ShowWindow(hwnd,nCmd);
    : UpdateWindow(hwnd);
    : MSG msg;
    :
    : while (GetMessage(&msg,0,0,0)>0)
    : {
    : TranslateMessage(&msg);
    : DispatchMessage(&msg);
    : }
    : return static_cast(msg.wParam);
    : }
    :
    : //================================================
    : LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    : {
    : int pos=0;
    : HDC hdc;
    :
    : [color=Red]// [AK]: making it static, so the value in it will be preserved between calls.[/color]
    : static TCHAR szString[100]; [color=Red]// [AK]: added proper TCHAR declaration[/color]
    :
    : int maxChars = 20;
    : HWND hWndDLG;
    : RGBColor colors[MAXCOLOR] = {{0xD3,0xD3,0xD3},{0x00,0xFF,0x00},
    : {0xFF,0xFF,0xFF},{0x00,0x00,0x00}};
    :
    : CREATESTRUCT *cs = reinterpret_cast(lParam);
    :
    : switch (uMsg)
    : {
    : case WM_CREATE:
    : editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    : CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,initRecArea(305,320,150,30),
    : IDBC_DEFPUSHBUTTON,_T("DISPLAY"));
    : return 0;
    :
    : case WM_PAINT:
    : hdc = BeginPaint(hwnd, &pntS);
    :
    : // Draw unto the client area
    : PaintObject(hwnd,initRecArea(10,10,300,200),colors, hdc,pos);
    : printText(hwnd, _T("Hello there"), initRecArea(15,15,0,0), hdc); [color=Red]// [AK]: added _T() macro[/color]
    : PaintObject(hwnd,initRecArea(305,10,605,250),colors, hdc, pos);
    : PaintObject(hwnd,initRecArea(10,205,300,315),colors, hdc, pos);
    : PaintObject(hwnd,initRecArea(10,320,300,420),colors, hdc, pos);
    :
    : [color=Red]// [AK]: also draw what's in text buffer
    : if (szString [0])
    : {
    : printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    : }[/color]
    :
    : EndPaint(hwnd, &pntS);
    : return 0; [color=Red]// [AK]: added return because code drops down to WM_COMMAND case[/color]
    :
    : case WM_COMMAND:
    : [color=Red]// [AK]: issue in below statement. WM_COMMAND message packs into
    : // WPARAM two values: ID is in LOW word and Notification code in HIGH word.
    : // There may be a case when this button sends other messages beside clicks.
    : // Take a look at this link: "http://msdn.microsoft.com/en-us/library/bb776944(VS.85).aspx"
    : // You need only BN_CLICKED to process, so you have to check for both HIGH and LOW words.[/color]
    : if ((LOWORD(wParam) == IDBC_DEFPUSHBUTTON) && (HIWORD (wParam) == BN_CLICKED))
    : {
    : [color=Red]// [AK]: IDCE_DLG control is never created. What's it for?
    : // Replacing it with IDCE_SINGLELINE just to see the edit contents
    : // painted on a dialog.
    : //[/color]
    : GetDlgItemText(hwnd, [color=Red]/*IDCE_DLG*/[/color] IDCE_SINGLELINE, szString, 80);
    : [color=Red]// printText(hwnd, szString, initRecArea(15,25,0,0), hdc);
    : // [AK]: ^^^ moved into WM_PAINT case
    :
    : // [AK]: once text is copied: force WM_PAINT
    : InvalidateRect (hwnd, NULL, TRUE);[/color]
    : }
    : return 0;
    :
    : case WM_TIMER:
    : /* Force the WM_PAINT */
    : //SetTimer(hwnd, ID_TIMER, 500, NULL);
    : //InvalidateRect (hwnd, NULL, TRUE);
    : //UpdateWindow(hwnd);
    : break; [color=Red]// [AK]: compiler warning here,
    : // because there is no return after switch()[/color]
    :
    : case WM_DESTROY:
    : PostQuitMessage(0); //signal end of application
    : return 0;
    : default:
    : //let system deal with msg
    : return DefWindowProc(hwnd,uMsg,wParam,lParam);
    : }
    : return 0; [color=Red]// [AK]: added to eliminate warnings[/color]
    : }
    :
    : //================================================
    : inline int ErrMsg(const ustring& s)
    : {
    : return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
    : }
    :
    : //================================================
    : // EDIT control
    : HWND CreateEdit(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    : const RECT& rc,const int id,const ustring& caption)
    : {
    : dwStyle|=WS_CHILD|WS_VISIBLE;
    : return CreateWindowEx(WS_EX_CLIENTEDGE,
    : _T("edit"),
    : caption.c_str(),
    : dwStyle,
    : rc.left,
    : rc.top,
    : [color=Red]rc.right,
    : rc.bottom,
    : // ^^^ Possibly same fix as below is needed here too?[/color]
    : hParent,
    : reinterpret_cast(static_cast(id)),
    : hInst,
    : 0);
    : }
    : //================================================
    : // BUTTON Control
    : HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    : const RECT& rc,const int id,const ustring& caption)
    : {
    : dwStyle|=WS_CHILD|WS_VISIBLE;
    : return CreateWindowEx(0,
    : _T("button"),
    : caption.c_str(),
    : dwStyle,
    : rc.left,
    : rc.top,
    : rc.right - rc.left, [color=Red]// [AK]: fixed the width[/color]
    : rc.bottom - rc.top, [color=Red]// [AK]: fixed the height[/color]
    : hParent,
    : reinterpret_cast(static_cast(id)),
    : hInst,
    : 0);
    : }
    :
    : // Purpose: Draws an object on the window
    : void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, HDC hdc, int index)
    : {
    : HPEN hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
    : HBRUSH hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));
    :
    : DrawARectangle(hwnd, hpen, hdc, hbrush, rc);
    :
    : [color=Red]// [AK]: moved it here to provide better maintenance.
    : // Creating objects in one function and deleting them in
    : // another function is a way for bad code in the long run.
    : DeleteObject(hpen);
    : DeleteObject(hbrush);[/color]
    : }
    :
    : // Purpose: Draws a rectangle which will have text boxes for input data
    : void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, HBRUSH hbrush, const RECT& rc)
    : {
    : // Select the new pen and brush, and then draw.
    : HPEN hpenOld = (HPEN)SelectObject(hdc, hpen);
    : HBRUSH hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
    : Rectangle(hdc,rc.left,rc.top,rc.right,rc.bottom);
    :
    : // Do not forget to clean up.
    : SelectObject(hdc, hpenOld);
    : [color=Red]//DeleteObject(hpen); // [AK]: moved out[/color]
    : SelectObject(hdc, hbrushOld);
    : [color=Red]//DeleteObject(hbrush); // [AK]: moved out[/color]
    : }
    :
    : // Initialize rectangle area
    : [color=Red]// [AK]: refactored it to provide a reference, because of warnings
    : RECT& initRecArea(int left, int top, int width, int height)
    : {
    : static RECT rec;
    :
    : rec.left = left;
    : rec.top = top;
    :
    : // [AK]: not sure is these two are correct.
    : // Technically speaking "right" coordinate is "width" only when "left"=0.
    : rec.right = width;
    : rec.bottom = height;
    :
    : // This line proves that you want the edit box starting at X=100 and Width=100!
    : // editCntrl = OnCreate(hwnd,cs,initRecArea(100,15,100,20));
    : // So, I modified the code below to initialize RECT properly
    : rec.right = width + left;
    : rec.bottom = height + top;
    :
    : return rec;
    : }
    : // [AK]: end of refactoring[/color]
    :
    : //handles the WM_CREATE message of the main, parent window; return -1 to fail
    : HWND OnCreate(const HWND hwnd,CREATESTRUCT *cs, RECT& rc)
    : {
    : return CreateEdit(hwnd,cs->hInstance,0,rc,IDCE_SINGLELINE,_T(""));
    : }
    :
    : // Display text on screen
    : void printText(HWND hWnd, TCHAR txt[], RECT& rc, HDC hDC)
    : {
    : [color=Red]//HDC hdc = GetDC(NULL); // [AK]: not used[/color]
    : long lfHeight = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    : [color=Red]//ReleaseDC(NULL, hdc); // [AK]: not used[/color]
    :
    : HFONT font = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE,
    : 0, 0, 0, 0, 0, 0, 0, _T("Times New Roman")); [color=Red]// [AK]: Added _T() macro[/color]
    :
    : [color=Red]HGDIOBJ hDefFont=SelectObject(hDC, font); // [AK]: remember the default font handle[/color]
    : SetBkColor(hDC, g_rgbBackground);
    : TextOut(hDC, rc.left, rc.top, txt, (int)_tcslen(txt));
    :
    : [color=Red]// [AK]: pop the created font back from DC,
    : // so it can be deleted properly
    : SelectObject (hDC, hDefFont);[/color]
    :
    : DeleteObject(font);
    : }
    : [/code]:

    Forums exist because of people like you.. Thanx a million! God bless...
Sign In or Register to comment.