Remove focus from SAVE button in OPENFILENAME win32? - c++

Problem:
If the user holds the "enter" keyboard button and opens OPENFILENAME Save As Dialog, it will automatically save the file - dialog only blinks.
Desired result:
The user holds the "enter" keyboard button, opens OPENFILENAME Save As Dialog, nothing happens. He needs to click on the Save button or click again the "enter" keyboard button to save a file.
My current code:
OPENFILENAME ofn;
TCHAR szFile[260] = { 't','e','s','t'}; // example filename
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
//Files like: (ALL - *.*), (Text - .TXT)
ofn.lpstrFilter = _T("All\0*.*\0Text\0*.TXT\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetSaveFileName(&ofn) == TRUE)
{
// file saved
}
Possible solution:
When ofn.lpstrFile is empty do nothing; Can't save file when there
is no filename
When ofn.lpstrFile has suggested filename then turn off focus on "Save" button or somehow ignore button enter holding.
I was trying to do that but failed, I am a beginner in CPP :(
Thanks for help

The easy solution to prevent data loss is to add the OFN_OVERWRITEPROMPT flag. This does not prevent the issue from happening if the suggested name does not already exist as a file.
To actually interact with the dialog you need OFN_ENABLEHOOK and a hook function. When you receive WM_NOTIFY, you can handle CDN_FILEOK to block the suggested name if not enough time has passed or maybe it is possible to change the focus in CDN_INITDONE.
Either way, you have to be mindful of the fact that you are changing how a common dialog works and this might anger some users.
Here is one way to do it. The actual delay to return the dialog to normal is something you have to decide for yourself.
const int btnid = 1337;
void CALLBACK resetsavedlgdefpush(HWND hWnd, UINT Msg, UINT_PTR idEvent, DWORD Time)
{
KillTimer(hWnd, idEvent);
HWND hDlg = GetParent(hWnd);
UINT id = LOWORD(SendMessage(hDlg, DM_GETDEFID, 0, 0));
if (id == btnid)
{
SendMessage(hDlg, DM_SETDEFID, IDOK, 0);
}
}
UINT_PTR CALLBACK mysavehook(HWND hWndInner, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (Msg == WM_NOTIFY)
{
OFNOTIFY*pOFN = (OFNOTIFY*) lParam;
if (pOFN->hdr.code == CDN_INITDONE)
{
HWND hDlg = GetParent(hWndInner);
CreateWindowEx(0, TEXT("BUTTON"), 0, BS_DEFPUSHBUTTON|BS_TEXT|WS_CHILD|WS_VISIBLE, 0, 0, 0, 0, hWndInner, (HMENU) btnid, 0, 0);
SendMessage(hDlg, DM_SETDEFID, btnid, 0);
PostMessage(hDlg, DM_SETDEFID, btnid, 0);
int keydelay = 0;
SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, &keydelay, 0);
SetTimer(hWndInner, 0, (250 * ++keydelay) * 5, resetsavedlgdefpush);
}
}
return 0;
}
...
ofn.Flags = OFN_PATHMUSTEXIST|OFN_EXPLORER|OFN_OVERWRITEPROMPT|OFN_ENABLESIZING|OFN_ENABLEHOOK;
ofn.lpfnHook = mysavehook;
MessageBox(ofn.hwndOwner, TEXT("Hold enter to test..."), 0, 0);
if (GetSaveFileName(&ofn) == TRUE) ...

Related

Is there a better way to implement a word replacement feature in the Win32 API?

I am creating a note pad application and I already have the basic stuff done (open file, save file, write to a file). I am now trying to add a word replacement feature just like the one in MS WORD which finds the inputted word and replaces all of its occurrences with the new word. Below is my code.
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define FILE_MENU_EXIT 2
#define FILE_MENU_BUILD 3
#define FILe_MENU_OPEN 4
#define FILE_MENU_SAVE 5
#define SEARCH_MENU_REPLACE 6
HMENU hMenu;
HMENU hDevMenu;
HWND hEdit,hCurrentPath;
HWND hOldWord,hNewWord;
int messagebox_id;
void replacerDialog(HINSTANCE);
void StillUnderConst();
void AddMenu(HWND);
void AddControls(HWND);
void AddDialogButton(HWND);
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst , LPSTR args, int ncmdshow)
{
WNDCLASSW wc = {0};
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = L"mywindowclass";
wc.lpfnWndProc = WindowProcedure;
if(!RegisterClassW(&wc))
return -1;
replacerDialog(hInst);
CreateWindowW(L"mywindowclass",L"Note Book",WS_VISIBLE|WS_MINIMIZEBOX|WS_SYSMENU,50,50,1000,650,NULL,NULL,NULL,NULL);
MSG msg = {0};
while(GetMessage(&msg,NULL,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void display_file(char* path)
{
FILE *file;
//this function helps us open and access the file passed in
file = fopen(path,"rb");
//this function helps as move to certain parts of the file thats why its a file pointer
fseek(file,0,SEEK_END);
//the ftell() function retuurn an integer type whic is stored in a var so that the file can later be read
//it is a needed to have the file size to be able to read fromt the file thats why we went for its size
int _size = ftell(file);
//this takes us back to the beginning of he file
rewind(file);
//this is thevar in which the files data will be kept
char *data = new char[_size+1];
//the fread function helps us to read from of specified file
//fread function requires us to know the file size to be able to read from it
fread(data,_size,1,file);
data[_size] ='\0';
SetWindowText(hEdit,data);
fclose(file);
}
void open_file(HWND hWnd)
{
OPENFILENAME ofn;
ZeroMemory(&ofn,sizeof(OPENFILENAME));
char file_name[100];
//the memory allocated to the the ofn struct
ofn.lStructSize = (sizeof(OPENFILENAME));
//the owner of the dialog
ofn.hwndOwner = hWnd;
//the path of the file being open
ofn.lpstrFile = file_name;
ofn.lpstrFile[0] = '\0';
//the max length being allocated to the path
ofn.nMaxFile = 100;
ofn.lpstrFilter = "All file\0*.*\0Text files\0*.txt\0C++ files\0*.cpp\0";
ofn.nFilterIndex = 1;
//creates an openfile dialog based on the kind of structure passed in as an argument
GetOpenFileName(&ofn);
SetWindowText(hCurrentPath,ofn.lpstrFile);
// MessageBox(NULL,ofn.lpstrFile,NULL,MB_OK);
display_file(ofn.lpstrFile);
}
void write_file(char* path)
{
FILE *file;
//this function helps us open and access the file passed in
file = fopen(path,"w");
int _size = GetWindowTextLength(hEdit);
char *data = new char[_size+1];
GetWindowText(hEdit,data,_size+1);
fwrite(data,_size+1,1,file);
fclose(file);
}
void save_file(HWND hWnd)
{
OPENFILENAME ofn;
ZeroMemory(&ofn,sizeof(OPENFILENAME));
char file_name[100];
//the memory allocated to the the ofn struct
ofn.lStructSize = (sizeof(OPENFILENAME));
//the owner of the dialog
ofn.hwndOwner = hWnd;
//the path of the file being open
ofn.lpstrFile = file_name;
ofn.lpstrFile[0] = '\0';
//the max length being allocated to the path
ofn.nMaxFile = 100;
ofn.lpstrFilter = "All file\0*.*\0Text files\0*.txt\0C++ files\0*.cpp\0";
ofn.nFilterIndex = 1;
//creates an openfile dialog based on the kind of structure passed in as an argument
GetSaveFileName(&ofn);
write_file(ofn.lpstrFile);
}
LRESULT CALLBACK DialogProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
switch(msg)
{
case WM_CREATE:
AddDialogButton(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,msg,wp,lp);
}
return 0;
}
void replacerDialog(HINSTANCE hInst)
{
WNDCLASSW dialog = {0};
dialog.hbrBackground = (HBRUSH)COLOR_WINDOW;
dialog.hCursor = LoadCursor(NULL,IDC_ARROW);
dialog.hInstance = hInst;
dialog.lpszClassName = L"mywindowdialog";
dialog.lpfnWndProc = DialogProcedure;
RegisterClassW(&dialog);
}
void displayDialog(HWND hWnd)
{
CreateWindowW(L"mywindowdialog",L"Replacer",WS_VISIBLE|WS_MINIMIZEBOX|WS_SYSMENU,50,50,300,300,hWnd,NULL,NULL,NULL);
}
void replacer(HWND hWnd)
{
char oword[100];
char nword[100];
wchar_t data[100000];
GetWindowText(hOldWord,oword,100);
GetWindowText(hNewWord,nword,100);
GetWindowText(hEdit,data,100000);
size_t startpos = data.find(oword);
data.replace(startpos,oword.length(),nword);
}
void AddDialogButton(HWND hWnd)
{
CreateWindowW(L"Static",L"Please enter the old word:",WS_CHILD|WS_VISIBLE,2,2,250,25,hWnd,NULL,NULL,NULL);
hOldWord = CreateWindowW(L"Edit",L"",WS_CHILD|WS_VISIBLE|WS_BORDER,10,20,250,30,hWnd,NULL,NULL,NULL);
CreateWindowW(L"Static",L"Please enter the new word:",WS_CHILD|WS_VISIBLE,2,100,250,25,hWnd,NULL,NULL,NULL);
hNewWord = CreateWindowW(L"Edit",L"",WS_CHILD|WS_VISIBLE|WS_BORDER,10,125,250,30,hWnd,NULL,NULL,NULL);
CreateWindowW(L"Button",L"Replace",WS_VISIBLE|WS_CHILD,220,220,70,40,hWnd,NULL,NULL,NULL);
}
LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
switch(msg)
{
case WM_KEYDOWN:
if (wp == VK_ESCAPE)
{
MessageBoxW(NULL,L"You just left clicked", NULL,MB_OK);
}
//The WM_COMMAND is sent to the windowproc when a button or menu is clicked
case WM_COMMAND:
//WPARAM is an argument that represents the IDs of menus and button
switch(wp)
{
case SEARCH_MENU_REPLACE:
displayDialog(hWnd);
break;
case FILE_MENU_SAVE:
save_file(hWnd);
break;
case FILe_MENU_OPEN:
open_file(hWnd);
break;
case FILE_MENU_EXIT:
messagebox_id = MessageBoxW(NULL,L"Are you sure?",L"Attention",MB_YESNO|MB_ICONEXCLAMATION);
if(messagebox_id == IDYES)
{
DestroyWindow(hWnd);
}
break;
}
break;
//WM_CREATE function is called when evr the window is created ,this is to initiate all of the components such as buttons and menus
case WM_CREATE:
AddControls(hWnd);
AddMenu(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd,msg,wp,lp);
}
}
void AddMenu(HWND hWnd)
{
hMenu = CreateMenu();
HMENU hFileMEnu = CreateMenu();
HMENU hSearchMenu =CreateMenu();
AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hFileMEnu,"FILE");
AppendMenu(hMenu,MF_STRING,NULL,"EDIT");
AppendMenu(hMenu,MF_STRING,NULL,"VIEW");
AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hSearchMenu,"SEARCH");
AppendMenu(hMenu,MF_STRING,(UINT_PTR)FILE_MENU_BUILD,"BUILD");
AppendMenu(hMenu,MF_STRING,NULL,"HELP");
//sub menus under the file menu
AppendMenu(hFileMEnu,MF_STRING,NULL,"NEW");
AppendMenu(hFileMEnu,MF_STRING,(UINT_PTR)FILe_MENU_OPEN,"OPEN");
AppendMenu(hFileMEnu,MF_STRING,(UINT_PTR)FILE_MENU_SAVE,"SAVE");
AppendMenu(hFileMEnu,MF_STRING,FILE_MENU_EXIT,"EXIT");
AppendMenu(hSearchMenu,MF_STRING,NULL,"FIND");
AppendMenu(hSearchMenu,MF_STRING,(UINT_PTR)SEARCH_MENU_REPLACE,"REPLACE");
AppendMenu(hSearchMenu,MF_STRING,NULL,"GO TO");
//this is used to declare where and what menu is to cretaed
//SetMenu(handler(usually window handler),what menu is being set);
SetMenu(hWnd,hMenu);
}
void AddControls(HWND hWnd)
{
hCurrentPath = CreateWindowW(L"Static",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,750,2,200,20,hWnd,NULL,NULL,NULL);
hEdit = CreateWindowW(L"Edit",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|ES_MULTILINE,5,25,980,550,hWnd,NULL,NULL,NULL);
}
I tried using the find() function to find the starting position of the word to be replaced then used the replace() function to replace the old word with the new word but all am getting is errors.
Below is the block for the replacer.
void replacer()
{
char oword[100];
char nword[100];
wchar_t data[100000];
GetWindowText(hOldWord,oword,100);
GetWindowText(hNewWord,nword,100);
GetWindowText(hEdit,data,100000);
size_t startpos = data.find(oword);
data.replace(startpos,oword.length(),nword);
}
I modified the code, and it runs well like this:
void replacer(HWND hWnd)
{
char nword[100];
std::string data;
std::string oword;
GetWindowText(hOldWord, (LPSTR)oword.c_str(), 100);
GetWindowText(hNewWord, nword, 100);
GetWindowText(hEdit, (LPSTR)data.c_str(), 100000);
size_t startpos = data.find(oword);
data.replace(startpos, oword.length(), nword);
}

WinApi - How to obtain SHELLDLL_DefView

I am trying to obtain handle to SHELLDLL_DefView.
So, I have this code.
HWND hProgman = FindWindow(L"Progman", NULL);
HWND hWnd = FindWindowEx(hProgman, 0, L"SHELLDLL_DefView", NULL);
Eveyrtihing works OK, until I change in Windows desktop brackground to slideshow. Then when I search with spy++ hierarchy of the windows, than SHELLDLL_DefView has another parent. Now it is #32769 (Desktop) -> WorkerW -> SHELLDLL_DefView. So I can't find it. Problem is that when I try
HWND desktop = GetDesktopWindow();
HWND hWnd = FindWindowEx(desktop , 0, L"WorkerW", NULL);
HWND hWnd = FindWindowEx(hWnd, 0, L"SHELLDLL_DefView", NULL);
Than SHELLDLL_DefView is not found. WorkerW yes.
Can anybody help?
Your code only works on some Windows versions as "SHELLDLL_DefView" can be found under "WorkerW" or "Progman" and as you discovered there can be many windows under the "WorkerW" class (normal in Win7).
Microsoft Docs report EnumWindows() is more reliable than calling GetWindow()/FindWindowEx() functions in loops, so more universal code (tested on Windows 98/Windows 7) would look like this (say you want to refresh the desktop):
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
HWND hNextWin;
hNextWin = FindWindowExA(hwnd, 0, "SHELLDLL_DefView", 0);
if ( hNextWin ) {
// The correct desktop shell window under Progman/WorkerW will have only 1 child window!
if ( GetNextWindow(hNextWin, GW_HWNDNEXT) || GetNextWindow(hNextWin, GW_HWNDPREV) )
return true;
// We found correct handle
PostMessageA(hNextWin, WM_KEYDOWN, VK_F5, 0);
return false;
}
return true;
}
void main() {
EnumWindows(&EnumWindowsProc, 0);
}
I found the answer. Need to iterate through all WorkerW.
HWND destop = GetDesktopWindow();
HWND hWorkerW = NULL;
HWND hShellViewWin = NULL;
do
{
hWorkerW = FindWindowEx(destop, hWorkerW, L"WorkerW", NULL);
hShellViewWin = FindWindowEx(hWorkerW, 0, L"SHELLDLL_DefView", 0);
} while (hShellViewWin == NULL && hWorkerW != NULL);

Trying to get text from tooltips not working

I've been trying for a couple of hours to interrogate tooltips to give up the text they contain to no avail. I've found How to get tooltip text for a given HWND? and tried that without success.
This shouldn't be that hard. I'm just not sure what I'm doing wrong. Here's a section of my code:
BOOL CALLBACK EnumWindowsProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
)
{
TCHAR className[200];
GetClassName(hwnd, className, _countof(className));
ASSERT(IsWindow(hwnd));
if (_tcscmp(className, _T("tooltips_class32")) == 0)
{
TOOLINFO ti = { 0 };
ti.cbSize = sizeof(TOOLINFO);
TCHAR text[500] = { 0 };
ti.lpszText = text;
ti.hwnd = GetParent(hwnd);
IsWindow(ti.hwnd);
ti.uId = GetDlgCtrlID(hwnd);
int result = SendMessage(hwnd, TTM_GETTEXT, _countof(text), (LPARAM)&ti);
CString info;
info.Format(_T("%p: %s \"%s\"\r\n"), hwnd, className, ti.lpszText);
CString& output = *(CString*)lParam;
output += info;
}
return 1;
}
void CTooltipVerifyDlg::OnTimer(UINT_PTR nIDEvent)
{
m_output = "";
VERIFY(EnumWindows(EnumWindowsProc, (LPARAM)&m_output));
SYSTEMTIME systemTime;
GetLocalTime(&systemTime);
CString text;
text.Format(_T("%02u:%02u:%02u.%03u\r\n"), systemTime.wHour, systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds);
m_output = text + m_output;
this->UpdateData(FALSE);
CDialogEx::OnTimer(nIDEvent);
}
CTooltipVerifyDlg is a dialogue with a text box which I communicate to with m_output, a CString that is bound to the text box.
When the SendMessage call is done, something on my desktop (or even the desktop manager) crashes. Any ideas why it would be crashing and not giving me the text that I desire?

Load an animated cursor at runtime from memory

I want to load an animated cursor stored in .ani format, which is described to be a RIFF archive/container, from memory without writing the memory to a temporary file. Thus far I am able to parse the .ani file structure and load the individual frames as a normal icon with the aid of CreateIconFromResourceEx LookupIconIdFromDirectoryEx
One of the problems that proofs difficult is the actual composition of these frames and the animation data (jiffy-rate etc) as there appears to be no entries in the Windows API to do so. Documentation or written knowledge over the subject seems to be limited to loading non-animated icons/cursors from memory.
Similar questions such as 'Load an embedded animated Cursor from the Resource' express the desire to load an animated cursor from an embeddable resource. However i am not able to reproduce a workable solution from that either. Partly because the resource compiler in visual studio 2008 & 2010 doesn't support .ani (only ico and cur) files and therefor embedding it simply results in a 1:1 copy of the bytes as they were in the original file as opposed to .cur and .ico files which get decomposed into multiple resources. The subsequent call to CreateIconFromResource as shown in both answers does not work because the data it expects is the icon/cursor data of a single directive in a icon archive and not a RIFF-based file structure.
To summarize I'm interested in any information regarding the structures of animated cursors (in memory) or otherwise relevant pointers that could assist me in pursuing my goal.
After re-evaluating loading an animated cursor from an embeddable resource as pointed out by mfc I have found a solution that allows me to load the cursors from an arbitrary memory address.
Data from an embeddable resource and data read from a file into memory are exactly identical. Therefor it suggests that CreateIconFromResource should work on normal regular memory as well. However there is one fundamental difference. Memory of the embeddable resources reside in special sections in the executable which are often padded to the nearest 4096-byte boundary. Memory allocated at runtime contains garbage values.
Now the solution that I found working exploits this by simply allocating a guard-band of zero-filled bytes. In my own test cases I have found that 8 is the minimum which also happens to be the size of a chunk in the riff container. Coincidence? What i suspect is that this is a bug and the algorithm happens to work for embeddable resources due to it's alignment restrictions within the dll/executable.
const int guardbandSize = 8;
FILE* fs = fopen("action.ani", "rb");
fseek(fs, 0,SEEK_END); int dwSize = ftell(fs); fseek(fs, 0,SEEK_SET);
char* memory = new char[dwSize + guardbandSize];
fread(memory, 1, dwSize, fs); memset(memory + dwSize, 0, guardbandSize);
fclose(fs);
cursor = (HCURSOR)CreateIconFromResource((PBYTE)memory,dwSize,FALSE,0x00030000);
delete memory;
Here is an overview of various ways to load an animated cursors.
#include <Windows.h>
#include <stdio.h>
#include "resource2.h"
void* hWnd;
bool visible = true;
bool running = true;
LRESULT CALLBACK WndProcInternal( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) ;
HCURSOR cursor = 0;
void main()
{
//Setup configuration
const int Width = 640, Height = 480;
const int Method = 4;
//Setup window class
WNDCLASS wcd;
wcd.style = CS_PARENTDC | CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcd.lpfnWndProc = (WNDPROC)WndProcInternal;
wcd.cbClsExtra = 0;
wcd.cbWndExtra = 0;
wcd.hInstance = GetModuleHandle(NULL);
wcd.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wcd.hCursor = LoadCursor(NULL, IDC_ARROW);
wcd.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wcd.lpszMenuName = NULL;
wcd.lpszClassName = TEXT("AnimatedIcon");
//Register the window class
if(!RegisterClass(&wcd))
{
MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"),MB_ICONEXCLAMATION | MB_OK);
FatalExit(-1);
}
//Create a window
if (!(hWnd=CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("AnimatedIcon"), TEXT("AnimatedIcon"),
WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX | WS_THICKFRAME | WS_MAXIMIZEBOX | WS_SYSMENU,
0, 0, Width, Height, NULL, NULL, NULL, NULL)))
{
MessageBoxA(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
FatalExit(-1);
}
if( Method == 1 )
{
//Method 1: Load cursor directly from a file
cursor = LoadCursorFromFileA("action.ani");
}
if( Method == 2 )
{
//Method 2: Load cursor from an resource section in the executable.
cursor = LoadCursor(0, MAKEINTRESOURCE(IDR_ANICURSORS1));
}
if( Method == 3 )
{
//Method 3: Manually locate the resource section in the executable & create the cursor from the memory.
HINSTANCE hInst=GetModuleHandle(0);
HRSRC hRes=FindResourceA(hInst,MAKEINTRESOURCEA(IDR_ANICURSORS1),"ANICURSORS");
DWORD dwSize=SizeofResource(hInst,hRes);
HGLOBAL hGlob=LoadResource(hInst,hRes);
LPBYTE pBytes=(LPBYTE)LockResource(hGlob);
cursor = (HCURSOR)CreateIconFromResource(pBytes,dwSize,FALSE,0x00030000);
}
if( Method == 4 )
{
//Method 4: Load the cursor from a file into memory & create the cursor from the memory.
const int guardbandSize = 8;
FILE* fs = fopen("action.ani", "rb");
fseek(fs, 0,SEEK_END); int dwSize = ftell(fs); fseek(fs, 0,SEEK_SET);
char* memory = new char[dwSize + guardbandSize];
fread(memory, 1, dwSize, fs); memset(memory + dwSize, 0, guardbandSize);
fclose(fs);
cursor = (HCURSOR)CreateIconFromResource((PBYTE)memory,dwSize,FALSE,0x00030000);
delete memory;
}
//Set the cursor for the window and display it.
SetClassLong((HWND)hWnd, GCL_HCURSOR, (LONG)cursor);
while (running)
{
MSG wmsg;
if (PeekMessage(&wmsg, (HWND)hWnd, 0, 0, PM_REMOVE))
{
TranslateMessage(&wmsg);
DispatchMessage(&wmsg);
}
}
}
LRESULT CALLBACK WndProcInternal( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if( uMsg == WM_DESTROY )
{
PostQuitMessage(1);
running = false;
}
return (long)DefWindowProc((HWND)hWnd,uMsg,(WPARAM)wParam,(LPARAM)lParam);
}
Import your animated cursor as a custom resource.
Give it a text name, eg "MyType".
Then load the cursor with:
HCURSOR hCursor = LoadAnimatedCursor(IDR_MYTYPE1, _T("MyType"));
/* ======================================================== */
HCURSOR LoadAnimatedCursor(UINT nID, LPCTSTR pszResouceType)
{
HCURSOR hCursor = NULL;
HINSTANCE hInstance = AfxGetInstanceHandle();
if (hInstance)
{ HRSRC hResource = FindResource(hInstance, MAKEINTRESOURCE(nID), pszResouceType);
DWORD dwResourceSize = SizeofResource(hInstance, hResource);
if (dwResourceSize>0)
{ HGLOBAL hRsrcGlobal = LoadResource(hInstance, hResource);
if (hRsrcGlobal)
{ LPBYTE pResource = (LPBYTE)LockResource(hRsrcGlobal);
if (pResource)
{ hCursor = (HCURSOR)CreateIconFromResource(pResource, dwResourceSize, FALSE, 0x00030000);
UnlockResource(pResource);
}
FreeResource(hRsrcGlobal);
}
}
}
return hCursor;
}

MFC Open Folder Dialog

In MFC, is there an Open Folder Dialog? That is, rather than choosing a filename, it chooses a folder name? Ideally, I'd like it to be the way Visual Studio does it when navigating for a "Project Location" (when creating a new project), which looks very much like a normal file dialog. But I could make do with one of the vertical tree sort of interfaces if the former doesn't exist.
This code will get you a open folder dialog (this was taken from somewhere on the web but I don't really know where).
CString szSelectedFolder = _T("");
// This is the recommended way to select a directory
// in Win95 and NT4.
BROWSEINFO bi;
memset((LPVOID)&bi, 0, sizeof(bi));
TCHAR szDisplayName[_MAX_PATH];
szDisplayName[0] = '\0';
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = _T("Select a folder");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
// Set the callback function
bi.lpfn = BrowseCallbackProc;
LPITEMIDLIST pIIL = ::SHBrowseForFolder(&bi);
TCHAR szReturnedDir[_MAX_PATH];
BOOL bRet = ::SHGetPathFromIDList(pIIL, (TCHAR*)&szReturnedDir);
if (bRet)
{
if (szReturnedDir != _T(""))
{
szSelectedFolder = szReturnedDir;
}
LPMALLOC pMalloc;
HRESULT HR = SHGetMalloc(&pMalloc);
pMalloc->Free(pIIL);
pMalloc->Release();
}
you'll also have to implement this callback function:
TCHAR szInitialDir[_MAX_PATH];
// Set the initial path of the folder browser
int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
// Look for BFFM_INITIALIZED
if (uMsg == BFFM_INITIALIZED)
{
SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)szInitialDir);
}
return 0;
}