|
■No45941 (やじゅ さん) に返信
情報ありがとうございます。
RegisterClassW 問題が解決しました。
原因は、WndProc関数のアドレス指定方法でした。
以下の部分を修正しました。
[StructLayout(LayoutKind.Sequential)]
struct WNDCLASS
{
public uint style;
[MarshalAs(UnmanagedType.FunctionPtr)]
public WndProcDelegate lpfnWndProc; -----> public IntPtr lpfnWndProc;
:
}
wc.lpfnWndProc = wProc; ------> wc.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(wProc);
しかし、新たな問題が発生しています。
メッセージキューからメッセージを取り出す GetMessageW が必ず 0(WM_QUIT)で返ってくるのです。
ウィンドウスタイル指定が原因かも知れません。(組み合わせ色々試していますが原因不明です。)
以下ソースになります。
--------------------------------------------------------------------
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr CreateWindowExW(
WindowExStyles dwExStyle,
[MarshalAs(UnmanagedType.LPWStr)]
string lpClassName,
[MarshalAs(UnmanagedType.LPWStr)]
string lpWindowName,
WindowStyles dwStyle,
uint x,
uint y,
uint nWidth,
uint nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);
[StructLayout(LayoutKind.Sequential)]
public struct MSG
{
public IntPtr hwnd;
public uint message;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public Point pt;
}
[DllImport("coredll.dll")]
private static extern int GetMessageW(MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
[DllImport("coredll.dll")]
private static extern int TranslateMessage(ref MSG lpMsg);
[DllImport("coredll.dll")]
static extern IntPtr DispatchMessage(ref MSG lpmsg);
:
:
if (RegisterClassW(ref wc) != 0)
{
// CreateWindowExW コール
IntPtr hWnd = CreateWindowExW(0, className, string.Empty, 0,
0, 0, 100, 200,
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (hWnd != IntPtr.Zero)
{
// Message ループ
MSG msg = new MSG();
int ret;
while ((ret = GetMessageW(msg, IntPtr.Zero, 0, 0)) != 0)
{
if (ret == -1)
{
break;
}
else
{
TranslateMessage(ref msg);
DispatchMessage(ref msg);
}
}
}
}
--------------------------------------------------------------------
|