⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpclien.cpp

📁 早期的WINDOWS编程,适合刚接触WINDOWS编程的初学者
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
//Function and data definition for the main module
//
//Lawrence Harris
//SAMS Publishing
//

#include "stdhdrs.h"
#include "common.h"
#include "resource.h" 
#include "TCPClient.h"
#include "cwclient.h"  
#include "about.h"
#include "..\testinfo.h"

//The following routines are only used with this module
BOOL InitApplication(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);

//Application instance handle
//declared extern common.h
//
HINSTANCE hInstApp;

CTCPClientAppObject TCPClientAppObject;
CWClient    *m_pClient;

TCHAR szAppName[] = TEXT("TCPClient");   // The name of this application
TCHAR szTitle[]   = TEXT("TCPClient Sample Application"); // The title bar text

int CALLBACK WinMain(
		  HINSTANCE hInstance,
		  HINSTANCE hPrevInstance,
		  LPSTR /* lpCmdLine */,
        int nCmdShow)
{

      MSG msg;
      HACCEL hAccelTable;

      m_pClient = NULL;

      //Under Windows 95 and Windows NT you do not need to change the message queue
      //size as the queue will change dynamically.
      //Under Win32s and Win16 the message queue size must be changed to work
      //with many OLE APIs
      //Since changing the queue size under Windows 95 and Windows NT is not harmful
      //the queue size will be changed in all cases.
      //
      int     iMsgQueueSize = 96;
      while   ((SetMessageQueue(iMsgQueueSize) == 0) && (iMsgQueueSize>0))
            iMsgQueueSize -= 8;
      if (iMsgQueueSize <= 0)
            return FALSE;

//In Win32 hPrevInstance is always NULL regardless of whether a previous instance
//is running. If we want to determine if a previous instance is running
//we'll look for a window which we create
//
//If we find a previous instance, we terminate the current instance and
//bring focus to the previous instance
//
      HWND    hWndPreviousInstance;

      if ((hWndPreviousInstance = FindWindow(szAppName,NULL)) != NULL){
            ShowWindow(hWndPreviousInstance,SW_SHOW);
            return (FALSE);
      }

      if (!hPrevInstance) {       // Other instances of app running?
          if (!InitApplication(hInstance)) { // Initialize shared things
              return (FALSE);     // Exits if unable to initialize
          }
      }

      /* Perform initializations that apply to a specific instance */

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


      hAccelTable = LoadAccelerators (hInstance, MAKEINTRESOURCE(IDR_TCPCLIENT));

      /* Acquire and dispatch messages until a WM_QUIT message is received. */

      while (GetMessage(&msg, // message structure
           NULL,   // handle of window receiving the message
           0,      // lowest message to examine
           0)) {   // highest message to examine
                if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg)) {
                        TranslateMessage(&msg);// Translates virtual key codes
                        DispatchMessage(&msg); // Dispatches message to window
                }
        }

      if (m_pClient != NULL)
        delete m_pClient;

      ::OleUninitialize();
      return (msg.wParam); // Returns the value from PostQuitMessage
}

//
//InitApplication
//
BOOL  InitApplication(HINSTANCE hInstance)
{
      WNDCLASS  wc;

      // Fill in window class structure with parameters that describe the
      // main window.

      wc.style         = CS_HREDRAW | CS_VREDRAW;// Class style(s).
      wc.lpfnWndProc   = (WNDPROC)WndProc;       // Window Procedure
      wc.cbClsExtra    = 0;                      // No per-class extra data.
      wc.cbWndExtra    = 0;                      // No per-window extra data.
      wc.hInstance     = hInstance;              // Owner of this class
      wc.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(IDR_TCPCLIENT)); // Icon name from .RC
      wc.hCursor       = LoadCursor(NULL, IDC_ARROW);// Cursor
      wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);// Default color
      wc.lpszMenuName  = MAKEINTRESOURCE(IDR_TCPCLIENT); // Menu from .RC
      wc.lpszClassName = szAppName;              // Name to register as

//Copy the information from wc to wcex, and then add the information about our
//small icon. Note that the wcex structure wants the size of the structure to
//be placed into cbSize.
//
      WNDCLASSEX  wcex;
      wcex.style       = wc.style;
      wcex.lpfnWndProc = wc.lpfnWndProc;
      wcex.cbClsExtra  = wc.cbClsExtra;
      wcex.cbWndExtra  = wc.cbWndExtra;
      wcex.hInstance   = wc.hInstance;
      wcex.hIcon       = wc.hIcon;
      wcex.hCursor     = wc.hCursor;
      wcex.hbrBackground = wc.hbrBackground;
      wcex.lpszMenuName  = wc.lpszMenuName;
      wcex.lpszClassName = wc.lpszClassName;
      wcex.cbSize        = sizeof(WNDCLASSEX);
      wcex.hIconSm       = (HICON)LoadImage(wcex.hInstance,MAKEINTRESOURCE(IDR_TCPCLIENT),
                                       IMAGE_ICON, 16, 16, 0);

      return RegisterClassEx(&wcex);
}

//
// InitInstance
//
BOOL  InitInstance(
      HINSTANCE hInstance,
      int       nCmdShow)
{
      HWND      hWnd; // Main window handle.

      // Save the instance handle in static variable, which will be used in
      // many subsequence calls from this application to Windows.

      hInstApp = TCPClientAppObject.hInstApp = hInstance;

      // Create a main window for this application instance.

      hWnd = CreateWindow(
                szAppName,           // See RegisterClass() call.
                szTitle,             // Text for window title bar.
                WS_OVERLAPPEDWINDOW,// Window style.
                CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, // Use default positioning
                NULL,                // Overlapped windows have no parent.
                NULL,                // Use the window class menu.
                hInstance,           // This instance owns this window.
                NULL                 // We don't use any data in our WM_CREATE
        );

      // If window could not be created, return "failure"
      if (!hWnd) {
                return (FALSE);
        }

      // Make the window visible; update its client area; and return "success"
      ShowWindow(hWnd, nCmdShow); // Show the window
      UpdateWindow(hWnd);         // Sends WM_PAINT message

      return (TRUE);              // We succeeded...
}

//
// WndProc
//
LRESULT CALLBACK WndProc(
                HWND hWnd,         // window handle
                UINT message,      // type of message
                WPARAM wParam,     // additional information
                LPARAM lParam)     // additional information
{
      switch (message) {

      //Message Cracker handlers for switch statement

      HANDLE_MSG(hWnd,WM_CREATE,TCPCLIENT_OnCreate);
      HANDLE_MSG(hWnd,WM_COMMAND,TCPCLIENT_OnCommand);
      HANDLE_MSG(hWnd,WM_DESTROY,TCPCLIENT_OnDestroy);
      HANDLE_MSG(hWnd,WM_RBUTTONDOWN,TCPCLIENT_OnRButtonDown);
      HANDLE_MSG(hWnd,WM_NCRBUTTONUP,TCPCLIENT_OnNCRButtonUp);
      HANDLE_MSG(hWnd,WM_DISPLAYCHANGE,TCPCLIENT_OnDisplayChange);
//    HANDLE_MSG(hWnd,WM_QUERYENDSESSION,TCPCLIENT_OnQueryEndSession);
//    HANDLE_MSG(hWnd,WM_ENDSESSION,TCPCLIENT_OnEndSession);
      case WSA_EVENT:
            TCPCLIENT_OnEvent(hWnd, wParam, lParam);
            break;
      default:          // Passes it on if unproccessed
        	return (DefWindowProc(hWnd, message, wParam, lParam));
        }
      return (0);
}

//Message cracker OnDestroy which is for WM_DESTROY
//
//This routine is used to handle the exit condition for
//the application
//
void  TCPCLIENT_OnDestroy(HWND hWnd)
{
      PostQuitMessage(0);
      FORWARD_WM_DESTROY(hWnd, DefWindowProc);
}

//Message cracker OnCreate which is for WM_CREATE
//
//This routine is used to handle the startup condition for
//the application's main menu
//
BOOL  TCPCLIENT_OnCreate(HWND hWnd,CREATESTRUCT FAR *lpCreateStruct)
{

      HMENU hMenu     = GetMenu(hWnd);
      HMENU hMenuFile = GetSubMenu(hMenu,0);

      EnableMenuItem(hMenu,IDM_FILE_NEW, MF_BYCOMMAND | MF_GRAYED);
      EnableMenuItem(hMenu,IDM_FILE_OPEN, MF_BYCOMMAND | MF_GRAYED);
      EnableMenuItem(hMenu,IDM_FILE_SAVE, MF_BYCOMMAND | MF_GRAYED);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -