c - Custom Find/Replace dialog box -
first of all, please bear in mind i'm new windows programming. i'll try cover question in great detail, answer too.
a short introduction:
i'm copying writing notepad-like application using win32 api , pure c. of familiar petzold's windows programming, modified version of poppad program used describe common dialog boxes. i'm writing strictly educational purposes, please refrain posting comments "why using old technology, use .net", comments not me solve problem :).
description of problem:
petzold in poppad program used common dialog boxes write notepad-like application. used edit control provide functions of basic text editor. poppad, notepad, had find , replace dialog boxes could, well, find stuff , replace it! mind boggling, know.
so wanted test newly acquired knowledge reading past chapters, decided write own find , replace dialog box. granted, in simplest form possibly. how hard can be? have 1 text field enter text , have 1 fancy button says "find!" on it.
i'd remind once more i'm new windows programming, excuse me possibly newbie questions. also, i'd point out i'll focus solely on making find dialog box working, replace shouldn't hard implement then.
so played resource editor in visual studio, , few hours later got this:
(stackoverflow doesn't allows me post images, here's link below)
http://i.imgur.com/r98x4.png
i named dialog box "find" (with quotation marks), don't have use makeintresource
macro in program, per petzold's school of thought. changed caption of "ok" button "find next" , changed it's id idok
idc_find
. changed idcancel
idc_cancel
, single line edit control idc_find_find
.
now more serious things. in main program's windows procedure, have piece of code:
case idm_search_find: hdlgmodeless = createdialog (hinst, text ("find"), hwnd, finddlgproc) ; return 0 ;
idm_search_find
message identifier of menu item, when clicked should open find dialog box. createdialog function used create modeless dialog box , store it's handle global variable hdlgmodeless
. finddlgproc
name of dialog box procedure (i think) code of finding text should go.
so without further ado, here's code of find dialog box procedure:
bool callback finddlgproc (hwnd hdlg, uint message, wparam wparam, lparam lparam) { static tchar szfindwhat[max_string_len]; //text find static int ioffset ; //offset beginning of edit control result int ilength, ipos, isinglelength ; //length of main edit control , single line edit control ptstr pstrdoc, pstrpos ; switch (message) { case wm_initdialog: return true ; case wm_command: switch (loword (wparam)) { //if button "find next" has been pressed, process logic of finding text case idc_find: // text single-line edit control in find dialog box // , save in szfindwhat variable isinglelength = getwindowtextlength(getdlgitem(hdlg, ide_find_find)) ; getwindowtext(getdlgitem(hdlg, ide_find_find), szfindwhat, isinglelength) ; // text main edit control, allocate memory // , store in pstrdoc variable ilength = getwindowtextlength (hwndedit) ; if (null == (pstrdoc = (ptstr) malloc ((ilength + 1) * sizeof (tchar)))) return false ; getwindowtext (hwndedit, pstrdoc, ilength + 1) ; // search document find string pstrpos = _tcsstr (pstrdoc + ioffset, szfindwhat) ; free (pstrdoc) ; // return error code if string cannot found if (pstrpos == null) return false ; // find position in document , new start offset ipos = pstrpos - pstrdoc ; ioffset = ipos + lstrlen (szfindwhat) ; // select found text sendmessage (hwndedit, em_setsel, ipos, ioffset) ; sendmessage (hwndedit, em_scrollcaret, 0, 0) ; case idc_cancel: destroywindow (hdlg) ; hdlgmodeless = null ; break ; } break ; case wm_close: destroywindow (hdlg) ; hdlgmodeless = null ; break ; default: return false; } return false ; }
actual error here hwndedit
undeclared identifier. hwndedit
main edit control (not single-line in find dialog box). how handle hwndedit
while i'm in find dialog box procedure?
i'd point out i'm feeling bit on head here, please if i'm missing/doing wrong obvious. i'm pretty sure if fix error i'm getting, program still won't work. though concept of should doing sounds simple, programming seems quite difficult :)
this code above should do, in simplest form:
- text find dialog box wish search
- text main edit control
- substring search last offset (don't start beginning every time)
- find position of result , readjust offset
- select found text
i know haven't asked direct question here, guess direct question be: how make work? :) more importantly understand how works. i'd appreciate if can provide me elaborate answer. help!
it looks you're close, need hwndedit main window. passed main window's handle in parent dialog box, should able parent window of dialog box so:
hwnd hwndparent = getparent(hdlg);
after can edit control parent referencing edit control id in main window definition. (assuming control id idc_edit):
hwnd hwndedit = getdlgitem(hwndparent, idc_edit);
Comments
Post a Comment