Last active
May 19, 2018 10:51
-
-
Save snipsnipsnip/7707017ae8f9b605566f2bacd5b1146a to your computer and use it in GitHub Desktop.
MFCの部品をWindows.Forms.Formに埋め込む ref: https://qiita.com/snipsnipsnip/items/19045907ae96121e8310
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** WinFormsのhwndにattachし、newしたcwndを返す */ | |
extern "C" __declspec(dllexport) CWnd* PASCAL attach(HWND hwnd) { | |
auto w = std::make_unique<CWnd>(); | |
if (!w->Attach(hwnd)) { return nullptr; } | |
// ... wに好きな部品を置く ... | |
return w.release(); | |
} | |
/** attachで作ったcwndをdetachとdeleteする */ | |
extern "C" __declspec(dllexport) void PASCAL detach(Pair *p) { | |
auto w = std::unique_ptr<CWnd>(cwnd); | |
w->Detach(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GuestControl : Control | |
{ | |
/// <summary>アタッチしたMFCのCWnd</summary> | |
private IntPtr CWnd { get; } | |
[DllImport("attach")] | |
private static extern IntPtr attach(IntPtr hwnd); | |
[DllImport("attach")] | |
private static extern void detach(IntPtr hwnd); | |
public GuestControl() | |
{ | |
CWnd = attach(Handle); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
detach(CWnd); | |
} | |
base.Dispose(disposing); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment