雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

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

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

許多書籍都在類向?qū)е薪ⅰN疫@里介紹的是手動建立,其結(jié)果是一樣的。

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

   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中的實現(xiàn)部分

#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.在類向?qū)е薪壎ㄗ远x控件時你是找不到剛才你定義的類型的,所以我采用手動加入代碼方法。
    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++自定義控件就全部介紹完畢,你可以在類型中加入你要實現(xiàn)的方法。

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

上一篇:C++中Cbitmap,HBitmap,Bitmap區(qū)別及聯(lián)系

欄    目:C語言

下一篇:c++學(xué)習(xí)之構(gòu)造函數(shù)

本文標(biāo)題:VC++ 自定義控件的建立及使用方法

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3028.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有