john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Win CE from file update window main

/* 2010-02-20 jpfeiffer
When Next button is pressed get data from
File and update Window display , can convert char[] to wchar[]  BUT zero wchar before pass by ref!
*/

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <aygshell.h>
#include "stringToWchar.txt"
#include "drawText.txt"
#include "handleError.txt"
#include "getFileLine.txt"
#define MAXCHARSPERLINE 34 
#define LINEHEIGHT 14

#define  IDC_ExitButton      40099
#define  IDC_clearScreen    40098
#define  IDC_openFile         40097
#define  IDC_nextButton    40096

LRESULT CALLBACK MenuWndProc(                                    HWND hwnd,   UINT message, WPARAM wParam,   LPARAM lParam);

FILE* fp=NULL;

int APIENTRY WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine, int ncmdshow )
{ 
    HWND hwnd = NULL;  
    HWND ExitButton = NULL;  
    HWND clearScreenButton = NULL; 
    HWND openFileButton = NULL; 
    HWND nextButton = NULL;

    MSG msg;   
    WNDCLASS wc;
    HMENU hMenu;
    RECT rc;

    wc.style = CS_HREDRAW | CS_VREDRAW;         
    wc.lpfnWndProc = (WNDPROC)MenuWndProc; 
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = 0;
    wc.hCursor = 0;
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = (LPTSTR) L"Menu App";

  if(!  RegisterClass (&wc))
{     MessageBox(NULL, TEXT("errors "),
  L"IMPORTANT", MB_OK); 
    return 0;
 }

    hwnd = CreateWindow (L"Menu App", L"ce read", WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, 
 CW_USEDEFAULT, CW_USEDEFAULT,        (HWND)NULL, NULL, hInstance, (LPSTR)NULL);

/* ----------- ----------- ----------- ----------- */
 GetWindowRect(hwnd, &rc);

ExitButton = CreateWindow( 
L"BUTTON", L"Quit", WS_CHILD | 
WS_VISIBLE | BS_PUSHBUTTON, 
0, (rc.bottom - (rc.top + (rc.right/6) )),
/* button top left corner x,y */
rc.right/6 , rc.right/6 , /* width & height */
hwnd, (HMENU)IDC_ExitButton, 
hInstance, NULL);

clearScreenButton = CreateWindow( 
L"BUTTON", L"Clear", WS_CHILD | 
WS_VISIBLE | BS_PUSHBUTTON, 
/* button top left corner x,y */
50, (rc.bottom - (rc.top + (rc.right/6) )),
/* width & height */
 rc.right/6 , rc.right/6 ,
hwnd, (HMENU) IDC_clearScreen,
hInstance, NULL);

openFileButton = CreateWindow( 
L"BUTTON", L"Open", WS_CHILD | 
WS_VISIBLE | BS_PUSHBUTTON, 
/* button top left corner x,y */
100, (rc.bottom - (rc.top + (rc.right/6) )),
/* width & height */
 rc.right/6 , rc.right/6 ,hwnd,
 (HMENU) IDC_openFile , hInstance, NULL);

   nextButton = CreateWindow( 
L"BUTTON", L"Next", WS_CHILD | 
WS_VISIBLE | BS_PUSHBUTTON, 
/* button top left corner x,y */
150, (rc.bottom - (rc.top + (rc.right/6) )),
/* width & height */
 rc.right/6 , rc.right/6 , hwnd, (HMENU)IDC_nextButton, hInstance, NULL);

ShowWindow(hwnd, ncmdshow); 
UpdateWindow(hwnd);

 while(GetMessage(&msg, NULL, 0, 0))  {       
    TranslateMessage(&msg);                  
    DispatchMessage(&msg); 
   }   
    return msg.wParam;
} /* end WinMain */

/* ----------- ----------- ----------- ----------- */

LRESULT CALLBACK MenuWndProc(                                    HWND hwnd,  UINT message, WPARAM wParam,  LPARAM lParam) 
{       wchar_t text[ MAXCHARSPERLINE];
    char buffer[ MAXCHARSPERLINE];

    int textpointX=0;
    int textpointY=100;
    int mousepointX =0;
    int mousepointY =0; 
    char c;
    RECT rc;
    int errorCode;
    char filename[ 128 ];

    switch (message) 
    {   
        case WM_CREATE:
     initializeBackground( hwnd );
            break;

        case WM_DESTROY: 
    if( fp != NULL ) {    fclose( fp );    }
               PostQuitMessage(0);    
        break;   
    case WM_LBUTTONDOWN:    
    mousepointX = LOWORD(lParam);
    mousepointY =  HIWORD(lParam);
      break;

     case WM_COMMAND:         
            switch(LOWORD(wParam))    
               {       
            case IDC_ExitButton: 
    if( fp != NULL ) {   fclose( fp );    }      
                     PostQuitMessage(0);    
                     break;
             case IDC_clearScreen:     
    InvalidateRect( hwnd, NULL, TRUE);
                 break;
             case  IDC_openFile: 
/* close any handle to previous file */
if( fp != NULL ) {   fclose( fp );  
           fp = NULL;  }

    memset( filename , 0, sizeof( filename ));
    strcpy( filename ,  "test.txt" );

    /* opens in root directory */
    fp = fopen( filename ,"r");

if( fp == NULL )
{
       drawText( hwnd, "error opening file" , 0, 0 );

}else{
      strcat( filename , " opened");     drawText( hwnd,  filename , 0 , 0 );

}
                         break;

             case IDC_nextButton: 
        if( fp == NULL ){
  drawText( hwnd, "error " , 
textpointX, textpointY );   
    }else{  
    memset( buffer , 0, sizeof( buffer ));
    errorCode = getFileLine( fp, buffer, MAXCHARSPERLINE);
if( errorCode == 1 )
{   drawText( hwnd, "end of file" , textpointX, 
textpointY + LINEHEIGHT + LINEHEIGHT );

}else
{
}
    drawText( hwnd, buffer , textpointX, textpointY );
drawText( hwnd, "12345678901234567890123456789012345" , textpointX, textpointY + LINEHEIGHT );

    }/* end if file next button */

             break;

                    default:
                    break;          
     }/* end case command */      
    break;

               case WM_PAINT:
        initializeBackground( hwnd );
     break;

                default:      
                   return DefWindowProc(hwnd, message, wParam, lParam);
       }/*  end case message */
    return 0;
}

  • « win ce from file mouse get file line
  • Win CE keys chars draw text »

Published

Feb 26, 2010

Category

c

~472 words

Tags

  • file 92
  • main 10
  • openfile-button 3
  • update 4
  • win 19
  • win-ce 40
  • window 6