Filenames displayed in textbox

Hi there,
I am trying to make a program that can select a file name and display it in a text box. I have managed to get the path to display in the text box but I need just the filename to display. If anyone can if it is possible can they display it in the textbox without the extension.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//The include header file
#include "resource.h"

BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_INITDIALOG:
			// Default values
			SetDlgItemText(hwnd, IDC_TEXT, "Press the browse button and choose your map file...");
		break;
		
		case WM_COMMAND:
             switch(LOWORD(wParam))
			{
                case IDC_ADD:
                     OPENFILENAME ofn;
    char szFileName[MAX_PATH] = "";

    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = "Map Files (*.ctm)\0*.ctm\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "ctm";

    if(GetOpenFileName(&ofn))
    {
        
    }

            }
        break;
        case WM_CLOSE:
			EndDialog(hwnd, 0);
		break;
		default:
			return FALSE;
	}
	return TRUE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}


If you want to see the header or resource file just tell me

Thanks again.
Last edited on
I believe the lpstrTitleName member of the ofn struct is just the file name without the path. I don't believe any of the members are just the file without the extension. You would probably have to parse that off. I have lots of examples of this sort of thing around I could post for you, but I don't do Dialog Engine Windows, but rather straight Sdk stype CreateWindow() windows.
Here's an sdk style c program that does exactly what you want except the extension is included...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <windows.h>
#define  IDC_BUTTON    1500
#define  IDC_TEXTBOX   1502

typedef struct    WindowsEventArguments         //Package Window Procedure Parameters into structure
{
 HWND             hWnd;                         //Handle of Window
 WPARAM           wParam;                       //Window Parameter
 LPARAM           lParam;                       //Long Parameter
 HINSTANCE        hIns;                         //Instance Handle (Resolves To Process Address)
}WndEventArgs, *lpWndEventArgs;


long fnWndProc_OnCreate(lpWndEventArgs Wea)     //Called One Time During Window Creatiion
{                                               //This corresponds to Form_Load() in
 HWND hWnd;                                     //Visual Basic
 
 Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;     //lParam is pointer to CREATESTRUCT in WM_CREATE
 hWnd=CreateWindow("button","button #1",WS_CHILD|WS_VISIBLE,160,15,100,25,Wea->hWnd,(HMENU)(IDC_BUTTON),Wea->hIns,NULL);
 hWnd=CreateWindow("edit",0,WS_CHILD|WS_VISIBLE|WS_BORDER,10,60,400,25,Wea->hWnd,(HMENU)(IDC_TEXTBOX),Wea->hIns,NULL);

 return 0;
}


long btnOpen_Click(lpWndEventArgs Wea)                                            //Do Not Delete
{
 //static char szFilter[128]="C Files (*.C)\0*.C\0\0";                            //one filter and one kind of file
 //static char szFilter[128]="C Files (*.C)\0*.C\0plg Files (*.plg)\0*.plg\0\0";  //two filters for two kinds of files
 static char szFilter[128]="c files (*.c), plg files (*.plg)\0*.c;*.plg\0\0";     //one filter for two kinds of files
 static char szTitleName[_MAX_FNAME + _MAX_EXT];
 static char szFileName[_MAX_PATH];
 static OPENFILENAME ofn;
 char szBuffer[128];
  
 memset(&ofn,'\0',sizeof(OPENFILENAME));
 ofn.lStructSize=sizeof(OPENFILENAME);
 ofn.lpstrFilter=szFilter;
 GetCurrentDirectory(128,szBuffer);
 ofn.lpstrInitialDir=szBuffer;
 ofn.nMaxFile=_MAX_PATH;
 ofn.nMaxFileTitle=_MAX_FNAME+_MAX_EXT;
 ofn.lpstrFile=szFileName;
 ofn.lpstrFileTitle=szTitleName;
 ofn.lpstrDefExt = "C";
 GetOpenFileName(&ofn);
 SetWindowText(GetDlgItem(Wea->hWnd,IDC_TEXTBOX),szTitleName);

 return 0;
}


long fnWndProc_OnCommand(lpWndEventArgs Wea)
{
 if(LOWORD(Wea->wParam)==IDC_BUTTON)
    return btnOpen_Click(Wea);
 else
	return 0;
}


long fnWndProc_OnClose(lpWndEventArgs Wea)
{
 PostQuitMessage(0);
 return 0;
}


LRESULT CALLBACK fnWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 static WndEventArgs wea; 
  
 switch (message)
 {
  case WM_CREATE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnCreate(&wea);                  
  case WM_COMMAND:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
	   return fnWndProc_OnCommand(&wea);
  case WM_CLOSE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnClose(&wea);
 }

 return DefWindowProc(hwnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPrevIns,LPSTR lpszArgument,int iCmdShow)
{
 MSG messages;
 WNDCLASSEX wincl;
 HWND hMain;
 
 wincl.lpszClassName="Form5";                     wincl.lpfnWndProc=fnWndProc;
 wincl.style=CS_HREDRAW | CS_VREDRAW;             wincl.cbSize=sizeof(WNDCLASSEX);
 wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);      wincl.hInstance=hIns;     
 wincl.hCursor=LoadCursor(NULL,IDC_ARROW);        wincl.cbClsExtra=0;
 wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);    wincl.lpszMenuName=NULL;
 wincl.hbrBackground=(HBRUSH)COLOR_BACKGROUND;    wincl.cbWndExtra=0;
 RegisterClassEx(&wincl);
 hMain=CreateWindow("Form5","Caption=Form5",WS_OVERLAPPEDWINDOW,250,300,425,150,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hMain,iCmdShow);
 while(GetMessage(&messages, NULL, 0, 0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

 return messages.wParam;
}


I found this in my old Dev-C++ collection, and as I said, I compiled it in C. It might go in Cpp though. You'wd have to try it.
Topic archived. No new replies allowed.