Frost Moon Project

Win32APIでリッチエディットを実装する際の注意点 - Frost Moon Project アクセスランキング

Home > Tips > Win32APIでリッチエディットを実装する際の注意点

Win32APIでリッチエディットを実装する際の注意点

  1. 問題1
  2. 問題1に対する解決方法
  3. 問題2
  4. 問題2に対する解決方法
  5. スクリーンショット
  6. ダウンロード
  7. ソース閲覧
    1. main.cpp
    2. resource.rc
    3. resource.h
    4. manifest.xml

問題1
Code::BlocksでダイアログベースのWin32アプリケーションを作成し、
ResEditでリッチエディットをぽとぺた。
それをReleaseでビルドすると、エラーは出ませんが、
実行してもウインドウが一向に表示されません。


Debugでビルドしても同じで、実行してみるとどうやら起動時にエラーで終了している様子。


問題1に対する解決方法
To create the Richedit control you must call the LoadLibrary function on the appropriate Richedit dynamic-
link library (DLL) somewhere prior to attempting to create the control. Doing this allows the control to
register its class name. For example, to use a Richedit version 2.0 or version 3.0 control in a dialog box,
you must call LoadLibrary("riched20.dll") prior to attempting to load and initialize the dialog box resource.
How To Create Richedit Control Version 2.0 and 3.0 with the Visual Studio Resource Editorより引用しました。
つまり、リッチエディットコントロールが作成される前に、
LoadLibrary("riched20.dll")して下さいね!!と言うことです。

問題1解決!!
1
2
3
4
5
6
7
8
9
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){
    hInst=hInstance;
 
    hRichDll=LoadLibrary("Riched20.dll");
 
    // The user interface is a modal dialog box
    return DialogBox(hInstance,MAKEINTRESOURCE(DLG_MAIN),NULL,(DLGPROC)DialogProc);
}
        


LoadLibrary()したらFreeLibrary()しないといけないので、いつもの様にWM_CLOSEに記述。
問題2発生...
1
2
3
4
5
case WM_CLOSE:
    EndDialog(hwndDlg,0);
    FreeLibrary(hRichDll);
    return TRUE;


問題2
上記で解決かと思いきや、XP環境で実行すると終了時に、
お馴染みの「問題が発生したため~」が表示されてしまいます。
問題2に対する解決方法
原因はFreeLibrary()の位置でした。
WM_CLOSEではだめで、WinMain()のDialogBox()より後の行に記述しなければいけません。
これはダイアログの場合ですが、通常のウインドウ場合、WM_DESTROYに記述すると「問題が発生したため~」が表示されると思います。
そんなわけで以下のように修正。
問題2解決!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){
    hInst=hInstance;
 
    hRichDll=LoadLibrary("Riched20.dll");
 
    // The user interface is a modal dialog box
    int iResult=DialogBox(hInstance,MAKEINTRESOURCE(DLG_MAIN),NULL,(DLGPROC)DialogProc);
 
    if(hRichDll!=NULL)FreeLibrary(hRichDll);
 
    return iResult;
}
        


スクリーンショット


ダウンロード
[Source&Binary]RichEdit.zip(120412)
size: 7.04KB
md5: 7f56f63c94f5ddc3a2255efdf16cbd86
ソース閲覧
main.cpp
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
#define WIN32_LEAN_AND_MEAN
 
#include <windows.h>
#include <richedit.h>
 
#include "resource.h"
 
HINSTANCE hInst;
HINSTANCE hRichDll;
 
BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam){
    switch(uMsg){
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
            CHARFORMAT cfm;
 
            memset(&cfm,0,sizeof(CHARFORMAT));
            cfm.cbSize=sizeof(CHARFORMAT);
            cfm.dwMask=CFM_COLOR;
            cfm.crTextColor=RGB(255,0,0);
            cfm.dwEffects=0;
 
            SendMessage(GetDlgItem(hwndDlg,IDC_RICHEDIT1),EM_SETCHARFORMAT,(WPARAM)SCF_SELECTION,(LPARAM)&cfm);
            SendMessage(GetDlgItem(hwndDlg,IDC_RICHEDIT1),EM_REPLACESEL,(WPARAM)FALSE,(LPARAM)"リリカル・トカレフ・キルゼムオール♪");
            return TRUE;
 
        case WM_CLOSE:
            EndDialog(hwndDlg,0);
            return TRUE;
 
        case WM_COMMAND:
            switch(LOWORD(wParam)){
                /*
                 * TODO: Add more control ID's, when needed.
                 */
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg,0);
                    return TRUE;
 
                case IDC_BTN_TEST:
                    MessageBox(hwndDlg,"You clicked \"Test\" button!","Information",MB_ICONINFORMATION);
                    return TRUE;
 
                default:
                    break;
            }
    }
 
    return FALSE;
}
 
 
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){
    hInst=hInstance;
 
    hRichDll=LoadLibrary("Riched20.dll");
 
    // The user interface is a modal dialog box
    int iResult=DialogBox(hInstance,MAKEINTRESOURCE(DLG_MAIN),NULL,(DLGPROC)DialogProc);
 
    if(hRichDll!=NULL)FreeLibrary(hRichDll);
 
    return iResult;
}
        


resource.rc
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
// Generated by ResEdit 1.5.10
// Copyright (C) 2006-2012
 
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"
 
 
 
 
//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOGEX 6, 5, 194, 106
STYLE DS_3DLOOK | DS_CENTER | DS_SETFONT | WS_CAPTION | WS_VISIBLE | WS_GROUP | WS_THICKFRAME | WS_SYSMENU
CAPTION "Code::Blocks Template Dialog App"
FONT 8, "Tahoma", 0, 0, 1
{
    PUSHBUTTON      "&Test", IDC_BTN_TEST, 138, 5, 46, 15
    PUSHBUTTON      "&Quit", IDC_BTN_QUIT, 138, 29, 46, 15
    CONTROL         "", IDC_RICHEDIT1, RICHEDIT_CLASS, WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 9, 5, 120, 80
}
 
 
 
//
// Manifest resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
1                  RT_MANIFEST    ".\\manifest.xml"
    


resource.h
1
2
3
4
5
6
7
8
9
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
 
#define DLG_MAIN                                101
#define IDC_RICHEDIT1                           1000
#define IDC_BTN_TEST                            1001
#define IDC_BTN_QUIT                            1002
    


manifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
    </dependentAssembly>
  </dependency>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="asInvoker"
          uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>
    

Copyright(C) 2007-2025 Frost Moon Project


Home > Tips > Win32APIでリッチエディットを実装する際の注意点