我用SOCKET编写了一个服务器(对话框),有两个线程。问题如下:
1.这个服务器在关闭的时候总会报错(没有连接客户端也会),是怎么回事呢?
2.如何屏蔽回车和ESC信号?
3.对话框的关闭按钮(就是右上角的八叉),怎么修改这个函数呢?呵呵,一个关于服务器的、另两个是关于对话框的问题,都是一个程序里的,就一起问了。欢迎大家踊跃发言!

解决方案 »

  1.   

    小白胡说一下
    第一个问题不好说,
    第二个问题如果是MFC的话可以重载PreTranslateMessage来截取ESC和回车
    第三个问题相应WM_CLOSE消息就可以
    不知道是不是你说的问题.
      

  2.   

    小白胡说一下
    第一个问题不好说,
    第二个问题如果是MFC的话可以重载PreTranslateMessage来截取ESC和回车
    第三个问题相应WM_CLOSE消息就可以
    不知道是不是你说的问题.
      

  3.   

    1.这个服务器在关闭的时候总会报错(没有连接客户端也会),是怎么回事呢?主进程
    HANDLE thread;
    HANDLE eventExit = CreateEvent( ... );
    thread = CreateThread(...., (LPARAM)eventExit);线程中DOWRD ThreadProc(LPARAM lParam)
    {
       HANDLE eventExit = (HANDLE)lParam;
       while(TRUE)
       {
          int ret = WaitForSingleObject(eventExit....); //设置合适的超时时间
          if(ret == WAIT_OBJECT_0) //要求线程退出
              break;
          //DoSomeThing; 
       }
       //做清理
    }主进程退出时
    SetEvent(eventExit);
    WaitForSingleObject(thread);
    CloseHandle(eventExit);
    CloseHandle(thread);2. 不懂你的意思3. 响应WM_SYSCOMMAND消息,WPARAM参数为SC_CLOSE
      

  4.   

    1.这个服务器在关闭的时候总会报错(没有连接客户端也会),是怎么回事呢?
    具体是报什么错?调试看看是哪里出问题。2.如何屏蔽回车和ESC信号?
    重载对话框的PreTranslateMessage函数:
    if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE))
    return TRUE;
    或者用自己的消息循环,不调用IsDialogMessage函数。3.对话框的关闭按钮(就是右上角的八叉),怎么修改这个函数呢?
    响应WM_CLOSE消息。
      

  5.   

    2 MFC的话重载OnOK,OnCancel,并在处理WM_CLOSE消息
    void Cdlg1Dlg::OnOK()
    {
    // TODO: 在此添加专用代码和/或调用基类 //CDialog::OnOK();去掉这句
    }void Cdlg1Dlg::OnCancel()
    {
    // TODO: 在此添加专用代码和/或调用基类 //CDialog::OnCancel();去掉这句
    }void Cdlg1Dlg::OnClose()
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    // CDialog::OnClose();改为CDialog::OnOK();
           CDialog::OnOK();
    }
      

  6.   

    第一个问题:退出时出错,可能是资源多次释放或者线程退出问题,资源的话检查一下或者将资源部分注释掉看看退出还有没有错误,线程退出问题的话,在对话框程退出前用GetExitCodeThread检查一下子线程是不是已经退出了,如果参数返回STILL_ACTIVE表示还有线程程没有结束,这个时候突出对话框的话可能会出问题.希望可以解决问题,GOOD LUCK!!~ ~
      

  7.   

    // 服务器测试Dlg.cpp : implementation file
    //#include "stdafx.h"
    #include "服务器测试.h"
    #include "服务器测试Dlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyDlg dialogCMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CMyDlg)
    m_port = _T("5005");
    m_show = _T("服务器未打开");
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    //m_brush1.CreateSolidBrush(RGB(0,0,0));
    // m_brush2.CreateSolidBrush(RGB(100,50,89));
    m_brush.CreateSolidBrush(RGB(100,50,89));
    }void CMyDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMyDlg)
    DDX_Text(pDX, IDC_EDIT1, m_port);
    DDX_Text(pDX, IDC_STATIC_SHOW, m_show);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    //{{AFX_MSG_MAP(CMyDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BTN_SET, OnBtnSet)
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_WM_CTLCOLOR()
    ON_WM_CLOSE()
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyDlg message handlersBOOL CMyDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CMyDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CMyDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }
    SOCKET m_sockconn;
    SOCKET m_socket;
    bool m_a=1;
    bool m_b=0;//以下为线程
    //不断接收accept函数的线程
    DWORD WINAPI   RecvProc(PVOID pParam)

    SOCKADDR_IN addrCliect;
    int len=sizeof(SOCKADDR); while (m_a)
    {
        m_sockconn=accept(m_socket,(SOCKADDR *)&addrCliect,&len);
    }
    return 0;
    }//不断接收数据的线程
    DWORD WINAPI   OnRecv(PVOID pParam)
    {
    while(m_a)
    {
    MSG   msg;  
            if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))     
    {   
    TranslateMessage(&msg);     
    DispatchMessage(&msg);     
    }   
    //接收代码段
    char recvBuf[6];
    CString tempBuf;

    memset(recvBuf,0,6);
    if(recv(m_sockconn,recvBuf,6,0)> 0)
    {
    tempBuf=recvBuf;
    if ( tempBuf.Find("at\r\n",0)>=0 )
    {
    m_b=1;

    else 
    m_b=0;
    AfxGetApp()->m_pMainWnd->Invalidate();
    }
    }
    return FALSE;
    }
    //两个线程到此结束//打开服务器,创建Socket
    void CMyDlg::OnBtnSet() 
    {
    // TODO: Add your control notification handler code here
    UpdateData();
    m_socket=socket(AF_INET,SOCK_STREAM,0);
    if(INVALID_SOCKET==m_socket)
    {
    MessageBox("套接字创建失败!");
    return ;
    }
    // IP地址
    DWORD dwIP;
    ((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);
    unsigned   char   *pIP;   
    CString   strIP;    
    pIP   =   (unsigned   char*)&dwIP;   
    strIP.Format("%u.%u.%u.%u",*(pIP+3),   *(pIP+2),   *(pIP+1),   *pIP);
    //建立
    int port;
    port=atoi(m_port);
    SOCKADDR_IN addrSock;
    addrSock.sin_family=AF_INET;
    addrSock.sin_port=htons(port);
    addrSock.sin_addr.S_un.S_addr=htonl(dwIP);
    //绑定
    int retval;
    retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
    if(SOCKET_ERROR==retval)
    {
    closesocket(m_socket);
    MessageBox("绑定失败!");
    m_show="服务器打开失败";
    UpdateData(FALSE);
    return ;
    }
    //显示
    m_show="服务器已打开";
    m_show+="  IP地址: "+strIP;
    m_show+="  端口号: "+m_port;
    UpdateData(FALSE);
    listen(m_socket,5);    //线程的定义
    HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)NULL,0,NULL);
    CloseHandle(hThread); 
        HANDLE hThread2=CreateThread(NULL,0,OnRecv,(LPVOID)NULL,0,NULL);
    CloseHandle(hThread2); 
    }//发送数据Button
    void CMyDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
        send(m_sockconn,"hello!",5,0);
    }//改变对话框中一个静态框的颜色
    HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here

    // TODO: Return a different brush if the default is not desired
    if(pWnd->GetDlgCtrlID()==IDC_EDIT2)
    {
    if (m_b)
    {
    pDC->SetBkMode(RGB(255,0,0));
    return   (HBRUSH)  m_brush.GetSafeHandle(); 
    }
    else
    {
    // pDC->SetBkMode(RGB(0,255,0));
    return hbr;
    }
    }
    return hbr;
    }// 退出程序时修改一个bool变量,用这个变量的改变来结束线程
    void CMyDlg::OnClose() 
    {
    // TODO: Add your message handler code here and/or call default
    m_a=0;
    CDialog::OnOK();
    }//退出程序的Button
    void CMyDlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here
    m_a=0;
    CDialog::OnOK();
    }// 屏蔽回车和ESC
    BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
    {
    if(WM_KEYDOWN == pMsg->message ) 

    UINT nKey = (int) pMsg->wParam; 
    if( VK_RETURN == nKey || VK_ESCAPE == nKey ) 
    return TRUE ; 
    }
    return CDialog::PreTranslateMessage(pMsg);
    }对话框的代码,现在有问题如下:
    1.关闭对话框是、会报错。我在OnClose函数里,改变了while的选择变量值,但是没有生效,大家有没有好的退出程序的办法?
    2.我尝试定义两个CBrush,但是会报错。编译正常,但是运行错误,错误断点在CMyApp的初始化函数内,请问大家是什么原因?
      

  8.   

    // 服务器测试Dlg.cpp : implementation file
    //#include "stdafx.h"
    #include "服务器测试.h"
    #include "服务器测试Dlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyDlg dialogCMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CMyDlg)
    m_port = _T("5005");
    m_show = _T("服务器未打开");
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    //m_brush1.CreateSolidBrush(RGB(0,0,0));
    // m_brush2.CreateSolidBrush(RGB(100,50,89));
    m_brush.CreateSolidBrush(RGB(100,50,89));
    }void CMyDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMyDlg)
    DDX_Text(pDX, IDC_EDIT1, m_port);
    DDX_Text(pDX, IDC_STATIC_SHOW, m_show);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    //{{AFX_MSG_MAP(CMyDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BTN_SET, OnBtnSet)
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_WM_CTLCOLOR()
    ON_WM_CLOSE()
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyDlg message handlersBOOL CMyDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CMyDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CMyDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }
    SOCKET m_sockconn;
    SOCKET m_socket;
    bool m_a=1;
    bool m_b=0;//以下为线程
    //不断接收accept函数的线程
    DWORD WINAPI   RecvProc(PVOID pParam)

    SOCKADDR_IN addrCliect;
    int len=sizeof(SOCKADDR); while (m_a)
    {
        m_sockconn=accept(m_socket,(SOCKADDR *)&addrCliect,&len);
    }
    return 0;
    }//不断接收数据的线程
    DWORD WINAPI   OnRecv(PVOID pParam)
    {
    while(m_a)
    {
    MSG   msg;  
            if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))     
    {   
    TranslateMessage(&msg);     
    DispatchMessage(&msg);     
    }   
    //接收代码段
    char recvBuf[6];
    CString tempBuf;

    memset(recvBuf,0,6);
    if(recv(m_sockconn,recvBuf,6,0)> 0)
    {
    tempBuf=recvBuf;
    if ( tempBuf.Find("at\r\n",0)>=0 )
    {
    m_b=1;

    else 
    m_b=0;
    AfxGetApp()->m_pMainWnd->Invalidate();
    }
    }
    return FALSE;
    }
    //两个线程到此结束//打开服务器,创建Socket
    void CMyDlg::OnBtnSet() 
    {
    // TODO: Add your control notification handler code here
    UpdateData();
    m_socket=socket(AF_INET,SOCK_STREAM,0);
    if(INVALID_SOCKET==m_socket)
    {
    MessageBox("套接字创建失败!");
    return ;
    }
    // IP地址
    DWORD dwIP;
    ((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);
    unsigned   char   *pIP;   
    CString   strIP;    
    pIP   =   (unsigned   char*)&dwIP;   
    strIP.Format("%u.%u.%u.%u",*(pIP+3),   *(pIP+2),   *(pIP+1),   *pIP);
    //建立
    int port;
    port=atoi(m_port);
    SOCKADDR_IN addrSock;
    addrSock.sin_family=AF_INET;
    addrSock.sin_port=htons(port);
    addrSock.sin_addr.S_un.S_addr=htonl(dwIP);
    //绑定
    int retval;
    retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
    if(SOCKET_ERROR==retval)
    {
    closesocket(m_socket);
    MessageBox("绑定失败!");
    m_show="服务器打开失败";
    UpdateData(FALSE);
    return ;
    }
    //显示
    m_show="服务器已打开";
    m_show+="  IP地址: "+strIP;
    m_show+="  端口号: "+m_port;
    UpdateData(FALSE);
    listen(m_socket,5);    //线程的定义
    HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)NULL,0,NULL);
    CloseHandle(hThread); 
        HANDLE hThread2=CreateThread(NULL,0,OnRecv,(LPVOID)NULL,0,NULL);
    CloseHandle(hThread2); 
    }//发送数据Button
    void CMyDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
        send(m_sockconn,"hello!",5,0);
    }//改变对话框中一个静态框的颜色
    HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here

    // TODO: Return a different brush if the default is not desired
    if(pWnd->GetDlgCtrlID()==IDC_EDIT2)
    {
    if (m_b)
    {
    pDC->SetBkMode(RGB(255,0,0));
    return   (HBRUSH)  m_brush.GetSafeHandle(); 
    }
    else
    {
    // pDC->SetBkMode(RGB(0,255,0));
    return hbr;
    }
    }
    return hbr;
    }// 退出程序时修改一个bool变量,用这个变量的改变来结束线程
    void CMyDlg::OnClose() 
    {
    // TODO: Add your message handler code here and/or call default
    m_a=0;
    CDialog::OnOK();
    }//退出程序的Button
    void CMyDlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here
    m_a=0;
    CDialog::OnOK();
    }// 屏蔽回车和ESC
    BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
    {
    if(WM_KEYDOWN == pMsg->message ) 

    UINT nKey = (int) pMsg->wParam; 
    if( VK_RETURN == nKey || VK_ESCAPE == nKey ) 
    return TRUE ; 
    }
    return CDialog::PreTranslateMessage(pMsg);
    }对话框的代码,现在有问题如下:
    1.关闭对话框是、会报错。我在OnClose函数里,改变了while的选择变量值,但是没有生效,大家有没有好的退出程序的办法?
    2.我尝试定义两个CBrush,但是会报错。编译正常,但是运行错误,错误断点在CMyApp的初始化函数内,请问大家是什么原因?
      

  9.   

    你的程序退出问题除了资源释放外,可能在OnButton2()里也会引起错误,在执行OnOK的时候其他线程可能还没有结束,退出时有可能会出错.
    你可以用WaitForSingleObject也可以休眠当前线程让其他线程先结束,下面修改了一下,我是菜虫,可能有地方不妥,希望对楼主有帮助.嘿嘿...
    void CMyDlg::OnButton2() 

    m_a=0; 
    DWORD dwExitCode;
    do 
    {
     Sleep(100);
     GetExitCodeThread(线程句柄,&dwExitCode);
    } while (dwExitCode==STILL_ACTIVE);
    CDialog::OnOK(); 
    } 另外一个在设置控件背景色的代码里pDC->SetBkMode(RGB(255,0,0)); ?????? MSDN里好像是这个里面的参数应该是TRANSPARENT   
    设置背景色的应该是SetBkColor()吧 ,还是说新版本的VS可以这么用么?????
      

  10.   


    你说的上述方法 我试了 可行  谢谢!
    pDC->SetBkColor(RGB(255,0,0));  //这句代码是修改编译框的文字部分的背景色
    return   (HBRUSH)  m_brush.GetSafeHandle(); //返回的这句 是修改非文字部分的背景色现在就剩下一个问题了:VC能不能定义两个以上的CBrush brush1;CBrush brush2;
      

  11.   

    主线程退出时,可以用事件(Event)来通知其他线程退出。比如在主线程中
    {
       初始化:
             {
             Handle g_hExitEvent = CreateEvent(NULL,TRUE,FALSE,NULL); 创建一个事件;
             }
       退出时:
             {
    if(Handle!=NULL);
             SetEvent(hExitEvent )激活事件
            }
    }
    子线程:
    {
    ret = WaitForSingleObject(g_hExitEvent,0);
    if(ret == WAIT_OBJECT_0)
    {          

    break; //线程退出
    }
    }
      

  12.   

    do 

    Sleep(100); 
    GetExitCodeThread(线程句柄,&dwExitCode); 
    } while (dwExitCode==STILL_ACTIVE); 我测试的结果是,程序一直在运行这个循环,没有跳出!可能,这时程序并没有关闭线程
      

  13.   

    我弄明白了,是我程序的CMyApp的初始化函数错了,也不知道是VC安装的问题还是其他,在我家的电脑上就是好的,在公司就出问题。呵呵!
    过会儿我结分,谢谢大家的热情参与!
      

  14.   

    错误 1 error C2440: “static_cast”: 无法从“UINT (__thiscall CCoolBar::* )(CPoint)”转换为“LRESULT (__thiscall CWnd::* )(CPoint)” e:\workspace\ids\nids\网络入侵检测系统\down.liehuo.net\nids\scbarg.cpp 49错误 2 error C2440: “static_cast”: 无法从“UINT (__thiscall CSizingControlBar::* )(CPoint)”转换为“LRESULT (__thiscall CWnd::* )(CPoint)” e:\workspace\ids\nids\网络入侵检测系统\down.liehuo.net\nids\sizecbar.cpp 44
    两个错误出处一样都是来自
    #define ON_WM_NCHITTEST() \
    { WM_NCHITTEST, 0, 0, 0, AfxSig_l_p, \
    (AFX_PMSG)(AFX_PMSGW) \
    (static_cast< LRESULT (AFX_MSG_CALL CWnd::*)(CPoint) > (&ThisClass :: OnNcHitTest)) },
     
    这个要怎么修改!!