stringdlg.h

来自「The source code samples for chapter 2, 4」· C头文件 代码 · 共 120 行

H
120
字号
// stringdlg.h

#pragma once
#include "resource.h"

class CStringDlg : public CDialogImpl<CStringDlg> {
public:
  CStringDlg() { *m_sz = 0; }

BEGIN_MSG_MAP(CStringDlg)
  MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
  COMMAND_ID_HANDLER(IDOK, OnOK)
  COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
  COMMAND_HANDLER(IDC_STRING, EN_CHANGE, OnStringChange)
END_MSG_MAP()

  enum { IDD = IDD_SET_STRING };
  TCHAR m_sz[64];

private:
  void CheckValidString() {
    // Check the length of the string
    int cchString = m_edit.GetWindowTextLength();

    // Enable the OK button only if the string is of non-zero length
    m_ok.EnableWindow(cchString ? TRUE : FALSE);
  }

  LRESULT OnInitDialog(UINT, WPARAM, LPARAM, BOOL&) {
    CenterWindow();

    // Cache HWNDs
    m_edit.Attach(GetDlgItem(IDC_STRING));
    m_ok.Attach(GetDlgItem(IDOK));

    // Copy the string from the data member to the child control (DDX)
    m_edit.SetWindowText(m_sz);

    // Check the string length (DDV)
    CheckValidString();

    return 1; // Let dialog manager set initial focus
  }

  LRESULT OnStringChange(WORD, UINT, HWND, BOOL&) {
    // Check the string length each time it changes (DDV)
    CheckValidString();

    return 0;
  }

  LRESULT OnOK(WORD, UINT, HWND, BOOL&) {
    // Copy the string from the child control to the data member (DDX)
    m_edit.GetWindowText(m_sz, lengthof(m_sz));

    EndDialog(IDOK);
    return 0;
  }

  LRESULT OnCancel(WORD, UINT, HWND, BOOL&) {
    EndDialog(IDCANCEL);
    return 0;
  }

private:
  CWindow m_edit;
  CWindow m_ok;
};

/*
class CStringDlg : public CDialogImpl<CStringDlg> {
public:
  CStringDlg() { *m_sz = 0; }

BEGIN_MSG_MAP(CStringDlg)
  MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
  COMMAND_ID_HANDLER(IDOK, OnOK)
  COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
END_MSG_MAP()

  enum { IDD = IDD_SET_STRING };
  TCHAR m_sz[64];

private:
  bool CheckValidString() {
    // Check the length of the string
    int cchString = ::GetWindowTextLength(GetDlgItem(IDC_STRING));
    return cchString ? true : false;
  }

  LRESULT OnInitDialog(UINT, WPARAM, LPARAM, BOOL&) {
    CenterWindow();

    // Copy the string from the data member to the child control (DDX)
    SetDlgItemText(IDC_STRING, m_sz);

    return 1; // Let dialog manager set initial focus
  }

  LRESULT OnOK(WORD, UINT, HWND, BOOL&) {
    // Complain if the string is of zero length (DDV)
    if( !CheckValidString() ) {
      MessageBox("Please enter a string", "Hey!");
      return 0;
    }

    // Copy the string from the child control to the data member (DDX)
    GetDlgItemText(IDC_STRING, m_sz, lengthof(m_sz));

    EndDialog(IDOK);
    return 0;
  }

  LRESULT OnCancel(WORD, UINT, HWND, BOOL&) {
    EndDialog(IDCANCEL);
    return 0;
  }
};
*/

⌨️ 快捷键说明

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