Jump to content
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble
Strawberry Orange Banana Lime Leaf Slate Sky Blueberry Grape Watermelon Chocolate Marble

MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, register and become a site sponsor/subscriber and ads will be disabled automatically. 


Sign in to follow this  
BenoitRen

I want to make a Notepad alternative in C

Recommended Posts

However, it seems to be not nessesary here. That's according to the documentation which says you'll only need InitCommonControls() when you want to use XP theme's in your rich edit. But on the other hand, it stated you'll only need to call LoadLibrary() to find out which version of rich edit you have, which seems to be untrue.

I did some Googling on this part and ran across a comment that if using MFC LoadLibrary is not required, if using pure Win32 API then LoadLibrary(...) is required.

Does that sound about right?

Share this post


Link to post
Share on other sites

I haven't given up on this yet. In fact, I finally started on it. I've already got a basic notepad that can open and save files using the RichEdit control. That's the easy part, I guess.

Share this post


Link to post
Share on other sites

I can't do better than that. TheGun is coded in Microsoft Assembler, while I'm coding in C. No way I'll be able to make an alternative that small.

If anyone else is reading, I'm struggling to implement features. I have a Windows API reference, but it leaves some things to be desired. For most functions I need to allocate an array of chars that is big enough to contain the result. But I don't know how I would poll for the size needed.

Share this post


Link to post
Share on other sites

You'll have to be a bit more specific. There are serveral different ways to get the needed buffer size. Sometimes there's you can provide a null-pointer in which case the function returns the needed buffersize. Sometimes there's a seperate function to ask for the needed buffersize. Sometimes the only way is polling. (Provide a buffer, and while the system tells it's not enough, enlarge it.)

Share this post


Link to post
Share on other sites

For instance, the EM_GETSELTEXT message needs a buffer that is big enough to store the selected text in. Do I use the EM_EXGETSEL message, and calculate the size of the selection from that, or something?

Share this post


Link to post
Share on other sites

Yes, AFAIK that is the most appropriate way. To ensure the buffer is big enough you could also use GetWindowTextLength(), since the selected text cannot be bigger than the total text. Of course this can be inefficient when you only want to copy a few bytes.

As you may have noticed, EM_GETSELTEXT is a dangerous function, since you can't provide the size of the buffer you're using. So I would avoid using it, and use a combination of EM_EXGETSEL and EM_GETTEXTRANGE

Share this post


Link to post
Share on other sites

Great... My own application created a looping BSOD. I had to press Ctrl+Alt+Del to get out of it, which rebooted my system.

I'm trying to implement the Edit menu. But the way I did it is probably wrong. On MSDN it's totally different. Here's the code:

	case ID_EDIT_COPY: {
HWND hRichEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
CHARRANGE crSel;
SendMessage(hRichEdit, EM_EXGETSEL, 0, (LPARAM) &crSel);
if(crSel.cpMin != crSel.cpMax) {
TEXTRANGE trSel;
LPSTR pszText;
DWORD dwBufferSize = crSel.cpMax - crSel.cpMin + 1;
pszText = GlobalAlloc(GPTR, dwBufferSize);
trSel.chrg.cpMin = crSel.cpMin;
trSel.chrg.cpMax = crSel.cpMax;
trSel.lpstrText = pszText;
SendMessage(hRichEdit, EM_GETTEXTRANGE, 0, (LPARAM) &trSel);
if(OpenClipboard(hwnd)) {
EmptyClipboard();
SetClipboardData(CF_TEXT, pszText);
CloseClipboard();
GlobalFree(pszText);
}
}
break;
}

What I copy to the clipboard is random garbage. And sometimes it creates the aforementioned BSOD.

Edited by BenoitRen

Share this post


Link to post
Share on other sites

You can replace your code with just one WM_COPY. Although, if you're planning to support multiple clipboard formats (the right thing to do), i can't see a problem in your code. Except maybe RichEdit is not in ANSI mode?

Share this post


Link to post
Share on other sites

Thanks, that worked! And it made it easy to also implement Cut, Paste, Clear and Undo. I also could implement Select All thanks to the experience I got earlier. :)

That'll do for now.

I still would like to know where my previous code went wrong, though.

Edited by BenoitRen

Share this post


Link to post
Share on other sites

if(crSel.cpMin != crSel.cpMax) {
DWORD dwBufferSize = crSel.cpMax - crSel.cpMin + 1;
pszText = GlobalAlloc(GPTR, dwBufferSize);

If the cpMin and cpMax members are equal, the range is empty. The range includes everything if cpMin is 0 and cpMax is –1.

Maybe cpMax is -1? In that case you alloc 0 bytes to contain all text.

Share this post


Link to post
Share on other sites

When I tested my function, I never selected the entire range.

Next challenge is the Search dialog. I'll probably make it a resource.

Share this post


Link to post
Share on other sites

I made a window with Resource Workshop. Problem is that even though the Windows platform target is Win32, it still looks like a Win 3.1 window. Eww. I'm guessing it's because of the compiler, Borland C++ 4.51, because the resource seems fine:

#include <resource.h>

DLG_SEARCH DIALOG 8, 21, 207, 95
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Zoeken"
FONT 8, "MS Sans Serif"
{
DEFPUSHBUTTON "&Volgende zoeken", ID_SEARCHNEXT, 135, 22, 63, 14
PUSHBUTTON "Annuleren", ID_CANCEL, 135, 40, 63, 14
LTEXT "&Zoeken naar:", -1, 4, 7, 45, 12
EDITTEXT IDC_SEARCHTEXT, 52, 6, 146, 13, WS_BORDER | WS_TABSTOP
CONTROL "Heel &woord", IDC_FULLWORD, "BorCheck", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 5, 27, 51, 13
CONTROL "&Identieke hoofdletters/kleine letters", IDC_CASESENSITIVE, "BorCheck", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 5, 43, 128, 11
GROUPBOX "Richting", IDC_DIRECTION, 5, 60, 92, 30, BS_GROUPBOX
CONTROL "&Omhoog", IDC_SEARCHUP, "BorRadio", BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 11, 73, 58, 10
CONTROL "Om&laag", IDC_SEARCHDOWN, "BorRadio", BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 54, 73, 39, 10
}

Share this post


Link to post
Share on other sites

According to Wikipedia, version 4.52 is the first to support W95. That *could* mean that you are creating 16 bit software. When you are creating 32 bit software, look if you can target for Windows 4.0. (That should be a linker option). If you can't, try to add the DS_3DLOOK flag after STYLE.

Share this post


Link to post
Share on other sites

I'm pretty sure I'm targeting Win32. Anyway, when I add the flag, it complains about an incomplete expression.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...