I'm a complete noob with Visual Studio, it's probably a stupid mistake.
I'm only trying to instantiate a Cdialog class, I'm making a dll that in the end will need to had some activeX commands in the Window.
I'm tried several ways to instantiate, but I always getting an assertion error.
So in my calldllclass.h I had:
#pragma once
__declspec(dllexport) long init();
in my class that extend Cdialog:
//{{AFX_INCLUDES()
//}}AFX_INCLUDES
class CMyclass: public CDialog{
#include "resource.h"
public:
CMyclass(CWnd* pParent = NULL)
: CDialog(100, pParent){
//{{AFX_DATA_INIT(CMyclass)
//}}AFX_DATA_INIT
}; // standard constructor
virtual ~CMyclass(){};
long calc();
// Dialog Data
//{{AFX_DATA(CMyclass)
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyclass)
protected:
//}}AFX_VIRTUAL
protected:
// Generated message map functions
//{{AFX_MSG(CMyclass)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
I created an Resource Class:
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE MOVEABLE PURE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE MOVEABLE PURE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE MOVEABLE PURE
BEGIN
"#define _AFX_NO_SPLITTER_RESOURCES\r\n"
"#define _AFX_NO_OLE_RESOURCES\r\n"
"#define _AFX_NO_TRACKER_RESOURCES\r\n"
"#define _AFX_NO_PROPERTY_RESOURCES\r\n"
"\r\n"
"#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
"#ifdef _WIN32\r\n"
"LANGUAGE 9, 1\r\n"
"#pragma code_page(1252)\r\n"
"#endif //_WIN32\r\n"
"#include ""res\\SimpleVC6SampleEvent.rc2"" // non-Microsoft Visual C++ edited resources\r\n"
"#include ""afxres.rc"" // Standard components\r\n"
"#endif\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 0, 0
STYLE DS_MODALFRAME
FONT 8, "MS Sans Serif"
BEGIN
END
#endif // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////
and finally in my cpp I had:
// SIMPLEDIALOGDLL.cpp : Defines the exported functions for the DLL application.
//
#pragma once
#include "stdafx.h"
#include "myclass.h"
#include "resource.h"
#include "dll2export.h"
BEGIN_MESSAGE_MAP(CMyclass, CDialog)
//{{AFX_MSG_MAP(CMyclass)
//}}AFX_MSG
END_MESSAGE_MAP()
long init(){
AfxEnableControlContainer();
CoInitializeEx(NULL,COINIT_MULTITHREADED);
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CMyclass * obj = new CMyclass();
//obj->ShowWindow(SW_HIDE);
obj->ShowWindow(SW_SHOW);
long res = obj->calc();
return (long) obj;
}
long CMyclass::calc(){
return 1+1;
}
When obj->ShowWindow(SW_SHOW); is called I get an
Debug Assertion Failed!
winocc.cpp Line 329
BOOL CWnd::ShowWindow(int nCmdShow)
{
ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));
if (m_pCtrlSite == NULL)
return ::ShowWindow(m_hWnd, nCmdShow);
else
return m_pCtrlSite->ShowWindow(nCmdShow);
}
a little update I put after showWindow it returns 1400
DWORD dw = GetLastError();
add answer sugenstion
I had this to init method
AfxEnableControlContainer();
CoInitializeEx(NULL,COINIT_MULTITHREADED);
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
CMyclass *obj = new CMyclass();
if(obj->Create(100)){
obj->ShowWindow(SW_HIDE);
DWORD dw = GetLastError();
}else{
DWORD dw = GetLastError();
}
Now I'm having in obj->Create(100)); objcore.cpp Line: 40
#ScottMcP-MVP is right, modeless dialog boxes need a call to the Create method.
Also, according to the MSDN docs on the constructor,
To construct a modeless dialog box, use the protected form of the
CDialog constructor. The constructor is protected because you must
derive your own dialog-box class to implement a modeless dialog box.
Construction of a modeless dialog box is a two-step process. First
call the constructor; then call the Create member function to create a
resource-based dialog box...
So declare your class like CMyclass() : CDialog() {...} and use the following code to display it:
CMyclass * obj = new CMyclass();
if (obj->Create(100))
obj->ShowWindow(SW_SHOW);
else
{ << error >>
}
P.S.: don't forget to delete the variable at the end of your program!
My code was right,
after many many hours, I went to change the compiler options of the Visual Studio and in Debug Information Format put Program Database (/Zi)
That fix it for me
Related
I want to display an image from Resource.rc file when I'm using Dear ImGui framework.
I have my code here.
HRSRC resource = FindResourceW(GCM(), MAKEINTRESOURCE(IDB_PNG1), MAKEINTRESOURCE("PGN"));
HANDLE image = LoadResource(GCM(), resource);
int imageWidth = 810;
int imageHeight = 701;
ImGui::Image(image, ImVec2(imageWidth, imageHeight));
inline HMODULE GCM() {
HMODULE hmodule = NULL;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)GCM, &hmodule);
return hmodule;
}
But when I display my image, it's all black. The data type of HANDLE is void* and the first argument of ImGui::Image want to have the void pointer data type as well.
I have added two files into my Resource.rc file.
The resource.h header file looks like this.
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Resource.rc
//
#define IDB_PNG1 101
#define IDI_ICON1 102
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
And the Resource.rc file looks like this
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Swedish (Sweden) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_SVE)
LANGUAGE LANG_SWEDISH, SUBLANG_SWEDISH
#pragma code_page(1252)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// PNG
//
IDB_PNG1 PNG "pinmap.png"
/////////////////////////////////////////////////////////////////////////////
//
// Icon
//
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON1 ICON "logo.ico"
#endif // Swedish (Sweden) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
Question:
How can I display an image by loading it from the resources and include that image in ImGui framwork?
i can't understand why findresource return null. i add new resources and import driver.sys.looked at all possible materials for studying this error, but found nothing to solve it. in all of the cases, zero is returned, what can i do wrong?
MAIN.CPP :
using namespace std;
#pragma warning(disable: 6387)
class Resource
{
public:
struct Parameters
{
std::size_t size_bytes = 0;
void* ptr = nullptr;
};
private:
HRSRC hResource = nullptr;
HGLOBAL hMemory = nullptr;
Parameters p;
public:
Resource(int resource_id, const std::string& resource_class) {
hResource = FindResource(nullptr, MAKEINTRESOURCEA(resource_id), resource_class.c_str());
hMemory = LoadResource(nullptr, hResource);
p.size_bytes = SizeofResource(nullptr, hResource);
p.ptr = LockResource(hMemory);
}
auto GetResourceString() const
{
std::string_view dst;
if (p.ptr != nullptr)
dst = std::string_view(reinterpret_cast<char*>(p.ptr), p.size_bytes);
return dst;
}
};
void GetFile() {
Resource very_important(IDR_DRIVER1, "BINARY");
auto dst = very_important.GetResourceString();
}
int main()
{
Resource res(IDR_DRIVER1, "BINARY");
system("pause");
return 0;
}
RESOURCE.H
//{{NO_DEPENDENCIES}}
// Включаемый файл, созданный в Microsoft Visual C++.
// Используется windowsD resources.rc
//
#define IDR_DRIVER1 101
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
$(ProjectName).rc :
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Русский (Россия) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_RUS)
LANGUAGE LANG_RUSSIAN, SUBLANG_DEFAULT
#pragma code_page(1251)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// DRIVER
//
IDR_DRIVER1 DRIVER "driver.sys"
#endif // Русский (Россия) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
IMG folder "driver.sys"/"DRIVER"
You are defining a resource whose ID is 101 and type is DRIVER:
#define IDR_DRIVER1 101
IDR_DRIVER1 DRIVER "driver.sys"
But, you are then asking FindResource() to find a resource whose ID is 101 and type is BINARY rather than DRIVER:
Resource very_important(IDR_DRIVER1, "BINARY");
Resource res(IDR_DRIVER1, "BINARY");
You need to change that to this:
Resource very_important(IDR_DRIVER1, "DRIVER");
Resource res(IDR_DRIVER1, "DRIVER");
The code below compiles and runs. When I debug it, however, the resource is NULL and the size is zero. I imported "Resources\title.display.png" using the wizard, so I have to believe the path to the image is right. No error is thrown. These are the three files that comprise the project. I have wittled it down to this and can't figure out what has gone wrong. Any iseas?
sample.rc:
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// PNG
//
IDB_PNG1 PNG "Resources\\title.display.png"
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
resource.h:
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by sample.rc
//
#define IDB_PNG1 101
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
sample.cpp:
#include <iostream>
#include <windows.h>
#include "resource.h"
int main(int argc, char** argv)
{
HMODULE module = GetModuleHandle(NULL);
HRSRC resource = FindResource(NULL, MAKEINTRESOURCE(IDB_PNG1), RT_RCDATA);
int resource_size = SizeofResource(module, resource);
return 0;
}
You are storing your image resource as a PNG resource, not an RCDATA resource. As such, when calling FindResource(), you need to change RT_RCDATA to TEXT("PNG"):
HRSRC resource = FindResource(NULL, MAKEINTRESOURCE(IDB_PNG1), TEXT("PNG"));
RT_DATA should have been L"PNG".
I'm trying to add a .wav file as a resource into my C++ game and play it on runtime. Here's my code in my main class:
PlaySound(IDR_WAVE1, GetModuleHandle(NULL), SND_FILENAME);
My resource.h file:
#define IDR_WAVE1 104
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 105
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
And my .rc file:
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// English (Australia) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENA)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_AUS
/////////////////////////////////////////////////////////////////////////////
//
// WAVE
//
IDR_WAVE1 WAVE "C:\\Users\\zjf\\Desktop\\phil.wav"
#endif // English (Australia) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
The issue is, I keep getting an error when trying to play this file from the resource.
argument of type int is incompatible with parameter of type lpcwstr
I've included the resource.h header, and I've tried adding quotation marks in
PlaySound(IDR_WAVE1, GetModuleHandle(NULL), SND_FILENAME);
So it would end up being:
PlaySound("IDR_WAVE1", GetModuleHandle(NULL), SND_FILENAME);
However, this only made the Windows machine beep once.
I would appreciate any help, as I've never worked with resource files.
Instead of:
PlaySound(IDR_WAVE1, GetModuleHandle(NULL), SND_FILENAME);
change to:
PlaySound(MAKEINTRESOURCE(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE);
**FileEnumTreeControl.cpp file**
// FileEnumTreeControl.cpp : Defines the class behaviors for the
application.
//
#include "stdafx.h"
#include "FileEnumTreeControl.h"
#include "FileEnumTreeControlDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CFileEnumTreeControlApp
BEGIN_MESSAGE_MAP(CFileEnumTreeControlApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CFileEnumTreeControlApp construction
CFileEnumTreeControlApp::CFileEnumTreeControlApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CFileEnumTreeControlApp object
CFileEnumTreeControlApp theApp;
// CFileEnumTreeControlApp initialization
BOOL CFileEnumTreeControlApp::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
CFileEnumTreeControlDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
**FileEnumTreeControlDlg.cpp file**
// FileEnumTreeControlDlg.cpp : implementation file
//
#include "stdafx.h"
#include <string>
#include <iostream>
#include "FileEnumTreeControl.h"
#include "FileEnumTreeControlDlg.h"
#include<windows.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CFileEnumTreeControlDlg dialog
CFileEnumTreeControlDlg::CFileEnumTreeControlDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileEnumTreeControlDlg::IDD, pParent)
, m_strTree(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFileEnumTreeControlDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_STATIC_TXT, m_strTree);
DDX_Control(pDX, IDC_TREE1, m_treeCtrl);
}
BEGIN_MESSAGE_MAP(CFileEnumTreeControlDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CFileEnumTreeControlDlg message handlers
BOOL CFileEnumTreeControlDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
WIN32_FIND_DATA data;
HTREEITEM hItem, hCar;
HANDLE find=FindFirstFile(L"D:\\*",&data);
hItem = m_treeCtrl.InsertItem(L"D", TVI_ROOT);
if(find!=INVALID_HANDLE_VALUE)
{
do
{
hCar = m_treeCtrl.InsertItem(data.cFileName, hItem);
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
WIN32_FIND_DATA data2;
wchar_t* dir=L"D:\\";
wcsncat(dir,data.cFileName,wcslen(data.cFileName)-4);
wcsncat(dir,L"\\*",3);
HANDLE find2=FindFirstFile(dir,&data2);
if(find2!=INVALID_HANDLE_VALUE)
{
do{
m_treeCtrl.InsertItem(data2.cFileName,hCar);
}while(FindNextFile(find2,&data2));
}
}
}while(FindNextFile(find,&data));
FindClose(find);
}
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CFileEnumTreeControlDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the
user drags
// the minimized window.
HCURSOR CFileEnumTreeControlDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
**FileEnumTreeControl.h : main header file for the PROJECT_NAME
application**
//
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
// CFileEnumTreeControlApp:
// See FileEnumTreeControl.cpp for the implementation of this class
//
class CFileEnumTreeControlApp : public CWinApp
{
public:
CFileEnumTreeControlApp();
// Overrides
public:
virtual BOOL InitInstance();
// Implementation
DECLARE_MESSAGE_MAP()
};
extern CFileEnumTreeControlApp theApp;
FileEnumTreeControlDlg.h : header file
//
#pragma once
#include "afxcmn.h"
// CFileEnumTreeControlDlg dialog
class CFileEnumTreeControlDlg : public CDialog
{
// Construction
public:
CFileEnumTreeControlDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_FILEENUMTREECONTROL_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
CString m_strTree;
CTreeCtrl m_treeCtrl;
};
FileEnumTreeControl.h : main header file for the PROJECT_NAME application
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
// CFileEnumTreeControlApp:
// See FileEnumTreeControl.cpp for the implementation of this class
//
class CFileEnumTreeControlApp : public CWinApp
{
public:
CFileEnumTreeControlApp();
// Overrides
public:
virtual BOOL InitInstance();
// Implementation
DECLARE_MESSAGE_MAP()
};
extern CFileEnumTreeControlApp theApp;
This program is created in mfc and everything working just fine but i am not able to expand inner directories as unable to give complete path to FindFirstFile() please tell me how to pass dynamic path to FindFirstFile()
Let me guess, its going wrong in this line:
wchar_t* dir=L"D:\\";
wcsncat(dir,data.cFileName,wcslen(data.cFileName)-4);
That's because you have defined dir to be wchar* that points to a static block of memory that contains "D:\" and doesn't have enough free space after to have the rest of the string concatenated into it.
The easiest way to fix it it to replace the definition of dir to be a stack-allocated array such as
wchar_t dir[MAX_PATH]
that allocates a long block of empty space that you can then copy the directory name into. It'll also have the advantage that it will be automatically deallocated when the program exits the code block. You should also be using the 3rd parameter to wcsncat to be the size of the buffer, not the size of the string you want copied. Read the function documentation carefully.
If you're new to C/C++, you need to read up on memory allocation, strings and arrays and buffers before going further or you'll make big mistakes.