Re: Serial data + DialogBox

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Frank Vicious (dontspame_at_lame.com)
Date: 11/22/04


Date: Mon, 22 Nov 2004 10:52:12 -0800


> "Bruce Eitman (eMVP)" <beitmannospam@NOSPAM_applieddata.NOSPAM_net> wrote
in message
>
> You are going to have to show us some code for that thread.

Bruce!

I put the thread creation in the WM_INITDIALOG case for my DialogBox. Once
this dialogbox is setup I want to have a thread created, reading serial
data, and updating the text fields of this dialogbox. I've taken out parts
of the code to simplify this post. Thanks for any info!

// Global Variables:
HINSTANCE g_hInst; // The current instance
HWND g_hwndCB; // The command bar handle
HWND global_handle;
HANDLE serial_threadHDL; // Handle to the serial thread.

int WINAPI WinMain( HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPTSTR lpCmdLine,
     int nCmdShow)
{

 if (!InitInstance (hInstance, nCmdShow))
 {
  return FALSE;
 }

 // Key combos
 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BAREBONES4);

 // Main message loop:
 while (GetMessage(&msg, NULL, 0, 0))
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }

 return msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
 WNDCLASS wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC) WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BAREBONES4));
    wc.hCursor = 0;
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = 0;
    wc.lpszClassName = szWindowClass;

 return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
 HWND hWnd = NULL;
 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
 TCHAR szWindowClass[MAX_LOADSTRING]; // The window class name

 g_hInst = hInstance; // Store instance handle in our global variable
 // Initialize global strings
 LoadString(hInstance, IDC_BAREBONES4, szWindowClass, MAX_LOADSTRING);
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

 //If it is already running, then focus on the window
 hWnd = FindWindow(szWindowClass, szTitle);
 if (hWnd)
 {
  // set focus to foremost child window
  // The "| 0x01" is used to bring any owned windows to the foreground and
  // activate them.
  SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
  return 0;
 }

 MyRegisterClass(hInstance, szWindowClass);

 RECT rect;
 GetClientRect(hWnd, &rect);

 hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
  CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
hInstance, NULL);

 global_handle = hWnd;

 if (!hWnd)
 {
  return FALSE;
 }
 //When the main window is created using CW_USEDEFAULT the height of the
menubar (if one
 // is created is not taken into account). So we resize the window after
creating it
 // if a menubar is present
 {
  RECT rc;
  GetWindowRect(hWnd, &rc);
  rc.bottom -= MENU_HEIGHT;
  if (g_hwndCB)
   MoveWindow(hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
 }

 ShowWindow(hWnd, nCmdShow);
 UpdateWindow(hWnd);

 return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
 HDC hdc;
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 TCHAR szHello[MAX_LOADSTRING];

 switch (message)
 {
  case WM_COMMAND:
  wmId = LOWORD(wParam);
  wmEvent = HIWORD(wParam);

   // Parse the menu selections:
   switch (wmId)
   {
    case IDM_HELP_ABOUT:

     // Once the "ABOUT" field is clicked on in the menu then the id_sample
DialogBox will show up -- calling with Sample()
     // Create the thread in the Sample CALL BACK routine.
     DialogBox(g_hInst, (LPCTSTR)id_sample, hWnd, (DLGPROC)Sample);

        break;

    case IDOK:
     SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0),
(LPARAM)hWnd);
     SendMessage (hWnd, WM_CLOSE, 0, 0);
     break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
   }
   break;

  case WM_CREATE:
   g_hwndCB = CreateRpCommandBar(hWnd);
            // Initialize the shell activate info structure
            memset (&s_sai, 0, sizeof (s_sai));
            s_sai.cbSize = sizeof (s_sai);
   break;
  case WM_PAINT:
   RECT rt;
   hdc = BeginPaint(hWnd, &ps);
   GetClientRect(hWnd, &rt);
   LoadString(g_hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

   DrawText(hdc, szHello, _tcslen(szHello), &rt,
    DT_SINGLELINE | DT_VCENTER | DT_CENTER);
   EndPaint(hWnd, &ps);
   break;
  case WM_DESTROY:
   CommandBar_Destroy(g_hwndCB);
   PostQuitMessage(0);
   break;
  case WM_ACTIVATE:
            // Notify shell of our activate message
   SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
       break;
  case WM_SETTINGCHANGE:
   SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
       break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

HWND CreateRpCommandBar(HWND hwnd)
{
 SHMENUBARINFO mbi;

 memset(&mbi, 0, sizeof(SHMENUBARINFO));
 mbi.cbSize = sizeof(SHMENUBARINFO);
 mbi.hwndParent = hwnd;
 mbi.nToolBarId = IDM_MENU;
 mbi.hInstRes = g_hInst;
 mbi.nBmpId = 0;
 mbi.cBmpImages = 0;

 if (!SHCreateMenuBar(&mbi))
  return NULL;

 return mbi.hwndMB;

}

// This is the callback fun citon for my dialogbox. I want to create a
thread that will read in serial data and update
// the dialogbox!

LRESULT CALLBACK Sample(HWND hDlg, UINT message, WPARAM wParam, LPARAM
lParam)
{
 SHINITDLGINFO shidi;

 switch (message)
 {

case WM_INITDIALOG:

   // Create a Done button and size it.
   shidi.dwMask = SHIDIM_FLAGS;
   shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN |
SHIDIF_SIZEDLGFULLSCREEN;
   shidi.hDlg = hDlg;
   SHInitDialog(&shidi);

   // start thread when dialog is first created! Should I be doing it here?
   // I would like ti have it under WM_COMMAND under the IDOK case but it
didn't seem to be
   // working properly there

   serial_threadHDL=
CreateThread(NULL,NULL,sample_action,lpParam1,0,lpThreadId);

   return TRUE;

  case WM_COMMAND:
   if (LOWORD(wParam) == IDOK)
   {
    return TRUE;
   }

   break;
 }
    return FALSE;
}

DWORD sample_action(LPVOID lpParameter)
{
  static int countie = 0;

  // All I see is this message! I would expect to eventually to see the next
message! I never seem to jump into the while() loop!

  wsprintf(Message,L"Thread for serial data should have started
here!",szPortNamie, sizebaby);
  MessageBox(NULL,Message,L"Triage Wireless",MB_OK);

   wsprintf(Message,L" hahahahah Thread for serial data should have started
here!",szPortNamie, sizebaby);
  MessageBox(NULL,Message,L"Triage Wireless",MB_OK);

  while (1)
    {
              // Continuously reading in serial data updating the dialog box
and displaying MessageBoxes

      }

}


Quantcast