Click here to Skip to main content
15,887,683 members
Everything / Desktop Programming / MFC

MFC

MFC

Great Reads

by Hans Dietrich
XQueue implements a shared-memory first-in first-out (FIFO) queue, based on memory-mapped files, that works on all versions of Windows.
by Paul Vickery
Code to add a message bar to virtually any existing Windows control.
by Hans Dietrich
This series of articles is a step-by-step guide to reading files stored in your program's resources. Along the way I will present some non-MFC classes to assist you in reading text, binary, zip, and even encrypted files that have been compiled into your program as resources.
by Paul M Watt
Guide to understanding how to create and use regions with the WIN32 SDK

Latest Articles

by Ștefan-Mihai MOGA
Task Manager shows you the programs, processes, and services that are currently running on your computer. You can use Task Manager to monitor your computer’s performance or to close a program that is not responding.
by Ștefan-Mihai MOGA
This article is about the IntelliFile application which is a free alternative Windows version to Total Commander and uses many components that have been published on CodeProject.
by Ștefan-Mihai MOGA
An alternative Windows version to XML Sitemap online generators
by PJ Arends
An MFC CWnd derived grid of user definable tiles

All Articles

Sort by Updated

MFC 

U 24 Apr 2024 by merano99
To display an image with the correct aspect ratio on a button, you must first calculate a common scaling factor, rescale the image with it and finally crop it. The following function performs these steps. The image can be a file or a resource...
N 22 Apr 2024 by Richard MacCutchan
Something like this should do it. m_Btn1.ModifyStyle(0, BS_BITMAP); UINT style = m_Btn1.GetButtonStyle(); style &= ~BS_OWNERDRAW; // mask with all bits except owner draw m_Btn1.SetButtonStyle(style);
N 22 Apr 2024 by Member 14594285
if (PathFileExists(str_path)) { m_Btn1.ShowWindow(SW_SHOW); m_Btn1.ModifyStyle(0, BS_BITMAP); } else { m_Btn1.ModifyStyle(0, BS_OWNERDRAW); m_Btn1.Set_Art(m_pArt, 0); ...
U 22 Apr 2024 by Member 14594285
CBitmap bitmap; bitmap.Attach(image.Detach()); HBITMAP hBitmap6 = (HBITMAP)bitmap.GetSafeHandle(); m_Btn1.SetBitmap(hBitmap6); but image isn't proportionate to size of button but image of CButton isn't proportionate to size of my button ...
N 22 Apr 2024 by mbue
You called: if (dlg.DoModal() == IDOK) after that call you set pEditCtrl->SetWindowText(CString(pBuffer, nFileSize)); this must be done before (!) the dialog will be shown (DoModal).
U 21 Apr 2024 by Maryala Shreya
void CFileView::OnDoubleClick(NMHDR* pNMHDR, LRESULT* pResult) { NMITEMACTIVATE* pNMItem = (NMITEMACTIVATE*)pNMHDR; int nItem = pNMItem->iItem; if (nItem != -1) { ITEMINFO* pItem = (ITEMINFO*)GetListCtrl().GetItemData(nItem);...
N 21 Apr 2024 by Andre Oosthuizen
Your code seems to be correct for loading an image and setting it as the bitmap for your button button. As Rick stated, first check if the path you use is correct and if it is returning something, Add error handling and debugging statements to...
U 19 Apr 2024 by Member 14594285
I wrote: CImage image; image.Load(str_path); // just change extension to load jpg CBitmap bitmap; bitmap.Attach(image.Detach()); m_Btn1.SetBitmap(bitmap); m_Btn1.ShowWindow(SW_SHOW); str_path is the path of...
N 19 Apr 2024 by Rick York
I would guess it is because the image is not accessible to your program. Either give the image file name an explicit path or run your program in the directory where the image file is located. Actually, the first thing you should do is check the...
N 19 Apr 2024 by KarstenK
The conversion is as easy as already answered but you need to take care when making it to a COLORREF again. You need the same byte order and the same color depth. When you mismatch it the colors are weirdly changed. To better learn about working...
N 16 Apr 2024 by Richard MacCutchan
COLORREF (Windef.h) - Win32 apps | Microsoft Learn[^]. As you can see it is defined as a DWORD (32 bits) so itis already an integer value.
N 16 Apr 2024 by Member 14594285
I tried to convert COLORREF to int but I don't find the correct way.. What I have tried: I tried to search on internet, but I don't find the correct way
U 15 Apr 2024 by Pete O'Hanlon
Looking at your code - you are using Sql which is expecting you to use a parameterised query (as far as I can tell). In your particular case here, using hardcoded values, you should be using SqlStatement instead. As others point out though, you...
U 15 Apr 2024 by Member 14594285
I tried my query in SQLite Expert Personal and it's correct, but if I write query in mfc c++ my database isn't update.. I wrote: in .h: Kompex::SQLiteDatabase *m_pDB; Kompex::SQLiteStatement *m_pStmt; m_pDB = new...
N 15 Apr 2024 by OriginalGriff
Your code doesn't include any actual SQL query, much less an UPDATE or INSERT query with actual data for the DB to accept. Go back to your working code, and look more closely at exactly what is going on: the code you show us creates an empty...
13 Apr 2024 by Andre Oosthuizen
You need to use the 'CComboBox::InsertString' method instead of 'CComboBox::AddString' method, see - MS Learn | CComboBox Class[^] Your code should be - for (INT_PTR nInd = 0; nInd
13 Apr 2024 by Vicente Flich
I think the Richard's solution is probably the correct, but other possibilities: 1. How do you construct and initialize the m_adEdit array?. If you created it with new and not initialize it, maybe they contain some chunk. Initialize it with...
12 Apr 2024 by Pete O'Hanlon
The flag you are looking for is CBS_SORT. This sets the combobox up to automatically sort the strings. You can find bout more here[^].
12 Apr 2024 by Member 14594285
I have a comboBox and in properties I put sort to false..but elements in comboBox are in alphabetical order, my code: for (INT_PTR nInd = 0; nInd
11 Apr 2024 by merano99
The MFC uses CWnd::GetDlgItem and uses an existing pointer to the parent window or dialog as a member of CWnd. see: https://learn.microsoft.com/en-us/cpp/mfc/reference/cwnd-class?view=msvc-170#getdlgitem Unfortunately, it is not clear from the...
11 Apr 2024 by KarstenK
You can also open the RC file with a text editor and search for the control. IMPORTANT: each control must have an unique ID in your app. So check whether each control has an own identifier and the identifier are resolved in the resource.h to...
10 Apr 2024 by CPallini
Try to closely mimic the behaviour this MSDN piece of code[^]. Namely, DO NOT create (and destroy) brushes (or other GDI object) inside the OnCtlColor handler. On the contrary create the brushes once, in your initialization code, and release them...
10 Apr 2024 by CPallini
You could try (not tested) HWND hwnd = GetSafeHwnd(); LONG id = GetWindowLong(hwnd, GWL_ID);
10 Apr 2024 by Member 14594285
I created a derived class of CStatic, and I must find id of my CStatic but I don't know how I can do IMPLEMENT_DYNAMIC(CDerStatic, CStatic) CDerStatic::CDerStatic() { int n_color; RGBTRIPLE rgb; double red; double green; double blue;...
9 Apr 2024 by Member 14594285
When I resize my window software crashes because hbrush objcet isn't destoyed, I wrote this: HBRUSH CDlgEst::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); //HBRUSH hbrN = HBRUSH...
9 Apr 2024 by Pete O'Hanlon
Something to look at in your code. You are returning early from your switch statements, so the brushes aren't removed. You are leaking resources in that code. You are leaking a lot of resources. What's even more interesting is - if you somehow...
8 Apr 2024 by Vasily Androshchook
As the debugger showed a protected member of CFile::m_strFileName became empty for the closed file. So, derive your class from CFile and add a function bool MyFile::isClosed () const {return m_strFileName.IsEmpty ();}
8 Apr 2024 by Ștefan-Mihai MOGA
Task Manager shows you the programs, processes, and services that are currently running on your computer. You can use Task Manager to monitor your computer’s performance or to close a program that is not responding.
4 Apr 2024 by Tony Dean 2022
I may have found a solution... First, using the resource editor, I set the "Control" and "Control Parent" properties of the child dialog to TRUE. Then, when creating the child dialog, I used SetWindowPos() and set pWndInsertAfter to point to...
4 Apr 2024 by Pete O'Hanlon
You shouldn't need to hack around programatically to get this working. While I haven't done this in a while, I remember that I used to have to call ::SetParent to set the parent dialog, this including the child into the normal flow. Something...
4 Apr 2024 by Tony Dean 2022
I have an MFC dialog which contains an embedded, borderless child dialog which itself contains some additional edit controls (similar to using frames in Visual Basic). While MFC manages the tab order of controls within the dialog itself, controls...
30 Mar 2024 by Ștefan-Mihai MOGA
This article is about the IntelliFile application which is a free alternative Windows version to Total Commander and uses many components that have been published on CodeProject.
20 Mar 2024 by Member 14594285
I have a array of CEdit*, if this array has elements that aren't NULL I have to delete these..I wrote: if (m_arEdit.GetSize() > 0) { for (int j = 0; j
20 Mar 2024 by Pete O'Hanlon
Building on Richard's answer, I would make a minor change.int size = m_arEdit.GetSize(); for (int j = size - 1; j >= 0; j--) { if(NULL != m_arEdit[j]) { delete m_arEdit[j]; m_arEdit[j] = NULL; } }
19 Mar 2024 by Richard MacCutchan
I am assuming that you are calling this code more than once, and the second time you are trying to delete an entry that has already been deleted. You should change the code to: for (int j = 0; j
19 Mar 2024 by OriginalGriff
It's been a long time since I used MFC ... but your problem is simpler than you think: delete is used to return memory to the heap, it won't remove text from a CEdit control. And that's why your app crashes: the chunk of memory you are trying to...
16 Mar 2024 by Rick York
I usually disable the window also when hiding it. Make sure you do that and you might need to invalidate the control you are hiding so that it is redrawn.
16 Mar 2024 by Dave G11.
I am trying to hide various controls on a Windows MFC Dialog Box App. I am using: m_MyVariable.ShowWindow(SW_HIDE); This will hide the parts of the control that are underneath another control, but the parts in the clear are still visible. ...
14 Mar 2024 by Ștefan-Mihai MOGA
An alternative Windows version to XML Sitemap online generators
10 Mar 2024 by Pete O'Hanlon
There is not enough information in this post for us to be able to help. We cannot see what your other code looks like, which would include providing a sample showing that RiempiEdit is being called. What I would do, if I were you, would be to set...
5 Mar 2024 by PJ Arends
An MFC CWnd derived grid of user definable tiles
5 Mar 2024 by CPallini
I strongly suggest you to read a good book on the C/C++ programming languages. Also, reading some introductory material on pointers. As an example, for instance C Pointers (With Examples)[^], could be a start.
4 Mar 2024 by Richard Deeming
No. If you have two pointers pointing to the same thing, then changes to the thing they point to will be visible to both. That would be like saying Alice and Bob live in the same house, and I want to demolish Bob's house without touching Alice's...
4 Mar 2024 by Member 14594285
I have this: char * mio; char * tuo; tuo = mio; but is there a way if I want to change only the content of one pointer? example: if mio = "ciao"; and tuo = "ciao"; can I change the content of only mio and not tuo? What I have tried: I...
4 Mar 2024 by Rick York
One small addition to Richard's post - here is a wrapper class around the ShFileOperation function : CShellFileOp - Wrapper for SHFileOperation[^]
3 Mar 2024 by Richard Deeming
Well, you obviously didn't look hard enough. From the documentation: RemoveDirectoryA function (fileapi.h) - Win32 apps | Microsoft Learn[^]: To recursively delete the files in a directory, use the SHFileOperation[^] function. Alternatively,...
3 Mar 2024 by Member 14594285
I saw that I can use RemoveDirectory but I read that I can use it for empty directory..and I don't find a function that I can use. What I have tried: I tried to search on internet, I found only RemoveDirectory
3 Mar 2024 by Shao Voon Wong
Lee Algorithm Mazesolver in MFC and Direct2D
27 Feb 2024 by Maxim Kartavenkov
In additional to solution 2, I can say following. In some cases conversion can be done easy with the ATL helper conversion macros which can be used inline: W2A, A2W and their variations. To use that macro you can call once in the view scope - for...
27 Feb 2024 by Maxim Kartavenkov
In practice to control releasing instances of the COM objects in the application you can use the helper template classes such as CComPtr CComQIPtr or CInterfaceList of the ATL. That allowing you to work with COM interfaces as with the regular C++...
24 Feb 2024 by KarstenK
Check that you release your own COM pointers in time, so best not in the destructor but earlier like at the begin at the WM_CLOSE handler or even earlier like when the user wants to exit. The COM stuff takes some time, so be sure that it gets its...
23 Feb 2024 by CPallini
With objects you do not control (e.g. COM objects internal to the libraries) there's nothing you can do. On the other hand, with objects you do control, you may check the return value of the Release method, at least to inspect the updated...
23 Feb 2024 by merano99
A simple solution would be to write your own function that also recognizes empty tokens. It could look like this: CString MyTokenize(CString& source, const LPCWSTR& delimeter, int& first) { CString token; int count, end =...
23 Feb 2024 by Saurabh Ashtaputre
I have a C++ MFC application which uses many COM pointers from some other libraries. Those pointers are fetched using QueryInterface calls. While closing my application, I get an exception log stating that some COM pointers are not yet released. ...
23 Feb 2024 by jeron1
Maybe take a look here. How to Parse Empty Tokens using CString::Tokenize()[^]
23 Feb 2024 by Member 14594285
I have this string: T;150;;cha;;22052024;Glo snc;;;; I have to tokenize but the first string is T, then 150 and then cha, But I want: T 150 and empty string and not cha..how must I do? i WROTE: CString strToken =...
23 Feb 2024 by Richard MacCutchan
Either use all UNICODE or all ASCII, to avoid such issues. And you can call FindFirstFileW which is the UNICODE version, to make it simpler. It is generally better not to use the A or W suffixes on system calls. The compiler will choose the...
23 Feb 2024 by Pete O'Hanlon
Something like this perhaps?#include LPCSTR ToLPCSTR(LPCWSTR wstr) { std::wstring ws(wstr); return ws.c_str(); } Be aware that it's not possible to convert every widestring character to a CStr, so you may end up with data loss.
23 Feb 2024 by Member 14594285
I must convert from LPCWSTR to LPCSTR. I need it for this code: WFilePath path; WIN32_FIND_DATAA fd; HANDLE h; h = FindFirstFileA(path.GetString(), &fd); What I have tried: ...
22 Feb 2024 by merano99
In addition to Palini's solution in which the pointer is encapsulated in a class or alternatively the use of smart pointers, it would also be possible to simply delete the invalid pointer from the vector before deleting it. Then it would no...
22 Feb 2024 by CPallini
The following code #include #include using namespace std; class Foo { int m_value; public: Foo(int i):m_value(i){} ~Foo(){ cout
22 Feb 2024 by OriginalGriff
Just to add to what 0x01AA has - very rightly - said ... Copying a pointer does just that - it makes a copy of the address of an item, not a copy of the item. And when you call delete on a pointer to a memory item the memory it was using is...
22 Feb 2024 by Member 14594285
m_vBol.push_back(pbol); delete pbol; with this code my vector has null pointer, I think that is why I delete pbol, but I must delete it, how can I do? What I have tried: I tried to search on internet because I don't solve it
21 Feb 2024 by Ștefan-Mihai MOGA
You can use IntelliPort to transfer large files from a computer onto your portable computer using a serial port rather than going through the process of setting up your portable computer on a network.
20 Feb 2024 by CHill60
Posting solution from comments to close off the question. Try using db.OpenEx("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=XXX"); Points to note: - The parameter pair Uid= and Pwd= are only required if the MS Access database is...
20 Feb 2024 by Ștefan-Mihai MOGA
A look at the URLDownloadToFile function and architecture of IntelliLink
20 Feb 2024 by Ștefan-Mihai MOGA
About the IntelliDisk app that uses many components published on CodeProject
20 Feb 2024 by NigelSHoult
I am trying to write a program that uses CDatabase to connect to a local Access database without setting up a DSN (it works fine when I do use a DSN but I don't want to have to create one for every database). I haven't found anything that works...
8 Feb 2024 by Petrov Vladimir
Build up our own AVI editing application and explore some fun techniques using simple code additions
31 Jan 2024 by Richard MacCutchan
See Directory Management Functions - Win32 apps | Microsoft Learn[^].
31 Jan 2024 by jeron1
Perhaps take a look at one or more of these. MFC: How do you check if a directory exists before creating?[^] How to check whether the directory(folder) exists or not in vc++?[^] Creating directory in vc++[^]
31 Jan 2024 by Member 14594285
With mfc c++ I must see if a directory exists and if it doesn't exist I must create a directory What I have tried: I tried to search on google but there isn't what I search
29 Jan 2024 by Ștefan-Mihai MOGA
How to implement the missing CMFCListView class
27 Jan 2024 by merano99
Since the system time is already available in a structure, you can simply output it in a C-String using snprintf(). The minimum length for a 4-digit year should be 38 characters. // SYSTEMTIME lt; const char* days[] = { "Sunday", ... }; const...
26 Jan 2024 by Richard MacCutchan
See SYSTEMTIME (minwinbase.h) - Win32 apps | Microsoft Learn[^] and GetTimeFormatA function (datetimeapi.h) - Win32 apps | Microsoft Learn[^].
25 Jan 2024 by Member 14594285
I must convert to SYSTEMTIME to CString in this format: monday 4 september 2014, 12:00:34 how must I do? What I have tried: I searched on internet but I don't find with this format
24 Jan 2024 by Maxim Kartavenkov
To figure out the first issue under compiler settings on C/C++ Advanced tab choose "show includes" and it display during build the includes sequence - this way you can find is the NTDDI_VERSION defined at all before the file you interested in, or...
23 Jan 2024 by peterboulton
I am using the latest Visual Studio 2022 / C++ / MFC, Windows SDK 10.0, Platform toolset 10.0 (latest installed version), C++ Language Standard ISO C++ 17 Standard and building 64 bit. I started with the standard MDI template, added some stuff...
12 Jan 2024 by Richard MacCutchan
See LVM_GETITEMRECT message (Commctrl.h) - Win32 apps | Microsoft Learn[^].
12 Jan 2024 by Member 14594285
I'm searching to fid a method to find the Rect of a cell of CListCtrl in mfc c++, can I use GetItemRect? What I have tried: I used GetItemRect but I think isn't right
11 Jan 2024 by KarstenK
Use must fill the first column to use the ListControl. Use some graphic or some white picture. Else redesign your app and try some other solution like text outside of the ListControl to provide the extra information.
11 Jan 2024 by Member 14594285
I must fill my list but I don't want to fill the first column, how can I write? for (int nTagli = 0; nTagli
11 Jan 2024 by Rick York
I recommend that you take a look at this article and especially the code : Another Report List Control[^]. That is an owner-drawn list control in MFC and I use this class all the time. It is quite good I think.
11 Jan 2024 by Richard MacCutchan
You probably need to use the LVS_OWNERDRAWFIXED style which allows you to draw the item yourself. See List-View Window Styles (CommCtrl.h) - Win32 apps | Microsoft Learn[^] and also this article: Using virtual lists[^].
4 Jan 2024 by KarstenK
A lot of interesting details with drawing in Listcontrols is descripted in the outstanding article Neat Stuff to Do in List Controls Using Custom Draw. So you may find not only some solution, but also some further inspiration.
4 Jan 2024 by Richard MacCutchan
If you are painting a single item yourself then (according to my sample code) you need to also return CDRF_NEWFONT when processing CDDS_ITEMPREPAINT | CDDS_SUBITEM. This is also shown in the Microsoft example at Using Custom Draw - Win32 apps |...
3 Jan 2024 by Member 14594285
I created a derived class of CListCtrl, but I have a problem..when I click on a row the row doesn't become blue..and I don't understand why..my derived class: #include "stdafx.h" #include "Domino6.h" #include "MyListCtrl.h" #include...
21 Dec 2023 by Member 14594285
void CDlgCorrection::RowSelected() { m_bInizio = false; for (int i = 0; i
13 Dec 2023 by M Imran Ansari
See to convert from wchar to cstring https://stackoverflow.com/questions/24541933/mfc-how-to-convert-wchar-to-cstring[^]
12 Dec 2023 by KarstenK
You may searched the wrong places. Use the std:wstring class. It has some cstring function. It is always a good idea to learn the basics, like the std-library.
12 Dec 2023 by Member 14594285
I want to convert from WChar to CString but I don't know how I can do What I have tried: I'm searching on internet but I don't find..I found conversion from WCHAR and w_char but I don't find WChar
30 Nov 2023 by Maxim Kartavenkov
.NET string type constructor accepts the raw character array as an arguments. So, along with marshal_as you can do it next way: std::string std_string = "Some text"; String^ s = gcnew String(std_string.c_str()); Regards, Maxim
30 Nov 2023 by Andre Oosthuizen
To convert a 'std::string' to a C# 'System::String' in C++, you can use 'msclr::interop::marshal_as' This header gives you utilities to use for converting between managed and native types, more on this here - Managed C++ to C# Conversion[^] ...
30 Nov 2023 by Member 14594285
void CMFCwithCSharpDlg::OnBnClickedBtnAdd() { CSharp::CSharpMath^ mathClass = gcnew CSharp::CSharpMath(); int result = mathClass->Add(2, 6); char buf[200]; sprintf_s(buf, "result: %d", result); CStringA cstr1("Hello"); CString str =...
30 Nov 2023 by Richard MacCutchan
Since you are using C++/CLI then you should also use String Class (System) | Microsoft Learn[^].
24 Nov 2023 by Saurabh Ashtaputre
I have a C# WPF User Control. I want to load that on C++ MFC Dialog. But the catch is, I do not want to use ActiveX Control. I want the C# WPF User Control to be COM component and that COM component should be loaded on my other C++ MFC Dialog. ...
17 Nov 2023 by Richard MacCutchan
See Console Functions - Windows Console | Microsoft Learn[^]. A better starting point is probably at Consoles – Windows Desktop - Windows Console | Microsoft Learn[^].
17 Nov 2023 by Code_bro
Hi, i'm able to load the console at run time but it comes in a separate window. I like to have this window inside my SDI project and skip the view, which a don't have any use for. I have split the may view into two, one is a FormView second...
17 Nov 2023 by Dave Kreskowiak
As far as I can remember, you cannot embed an actual console window in your application. You can, however, create a normal edit control with fonts and colors that looks like a console window, and just pass the input and output to a hidden console...