国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

VC++ 自定義控件的建立及使用方法

2020-01-26 15:04:42
字體:
來源:轉載
供稿:網友

一、VC++定義自定義控件與delphi,VB有些差異。

delphi,vb在 file-new-other中建立。vc++在工具欄中就有自定義控件,但必須加入控件類型。

許多書籍都在類向導中建立。我這里介紹的是手動建立,其結果是一樣的。

二.建立過自定義控件類型:

   2.1、把工具欄上的自定義控件放入對話框中
   2.2、建立Mycontrol.h, Mycontrol.cpp文件
   2.3、Mycontrol.h中的定義是

#ifndef __MYCTROLTRL_H__  #define __MYCTROLTRL_H__  #define MYWNDCLASS "mycontrol"  #include <afxtempl.h>  class CMycontrol: public CWnd  {   private:   public:   static BOOL RegisterWndClass();   CMycontrol();   void customfun();//一個自定義方法   };  #endif

    2.4 Mycontrol.cpp中的實現部分

#include "StdAfx.h"  #include "mycontrol.h"  CMycontrol::CMycontrol()  { CMycontrol::RegisterWndClass();  }  //注冊控件RegisterWndClass格式是固定的不要記憶沒有那個必要直接拷貝粘貼就可以。   CMycontrol::RegisterWndClass()  {  WNDCLASS windowclass;  HINSTANCE hInst = AfxGetInstanceHandle();  //Check weather the class is registerd already  if (!(::GetClassInfo(hInst, MYWNDCLASS, &windowclass)))  {    //If not then we have to register the new class    windowclass.style = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW;    windowclass.lpfnWndProc = ::DefWindowProc;    windowclass.cbClsExtra = windowclass.cbWndExtra = 0;    windowclass.hInstance = hInst;    windowclass.hIcon = NULL;    windowclass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);    windowclass.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW);    windowclass.lpszMenuName = NULL;    windowclass.lpszClassName = MYWNDCLASS;    if (!AfxRegisterClass(&windowclass))    {      AfxThrowResourceException();      return FALSE;    }  }   return TRUE; } //自定義方法 void CMycontrol::customfun() { AfxMessageBox(_T("my control!")); }

三、使用自定義控件

    3.1.在類向導中綁定自定義控件時你是找不到剛才你定義的類型的,所以我采用手動加入代碼方法。
    3.2.在對話框.h文件中手動加入:public: CMycontrol m_mycontrol;
    3.3.在對話框.cpp文件中手動加入:DDX_Control(pDX,IDC_CUSTOM1,m_mycontrol);
    3.4.在對話框中加入Button 在點擊事件中加入測試代碼:

void CCustomcontrolDlg::OnButton1()  { // TODO: Add your control notification handler code here   m_mycontrol.customfun();  }

四、編譯運行vc++自定義控件的對話框窗體.編譯成功但運行什么也不顯示的解決

右鍵自定義控件->屬性->類型中填寫"mycontrol"再次允許OK!

到此VC++自定義控件就全部介紹完畢,你可以在類型中加入你要實現的方法。

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清水县| 长葛市| 黔西| 肃北| 邯郸市| 龙山县| 灵石县| 来凤县| 东乡| 镇安县| 莫力| 巴马| 渭南市| 山西省| 乌鲁木齐县| 桐庐县| 左贡县| 静宁县| 广饶县| 司法| 江安县| 呼和浩特市| 临江市| 安多县| 沧州市| 平阴县| 佛山市| 连州市| 华蓥市| 清新县| 成武县| 定结县| 梅州市| 平凉市| 闽侯县| 河间市| 保定市| 旬阳县| 乌兰浩特市| 堆龙德庆县| 米易县|