2011-09-12 55 views
3

我在使用SendMessageA(句柄,WM_SETTEXT,0,(LPARAM)文本)時遇到問題;如果在構造函數以外的地方使用,則會導致崩潰。Windows API SendMessage + WM_SETTEXT導致崩潰

MWC.h

#include <Windows.h> 
#include <iostream> 
#include <string> 

class MWC{ 
private: 
    MSG msg; 
public: 
    MWC(); 
    ~MWC(); 
    int mLoop(); 
    int mClose(UINT); 
    class System{ 
    public: 
     System(){ } 
     ~System(){ } 
     class Form{ 
     public: 
      HWND handle; // Need to access it in order to create other controls 
      Form(char*,int,int,int,int); 
      Form(char*,int,int); 
      ~Form(){ } 
      bool Show(); 
      HWND ReturnHandle(); 
     }; 
     class TextBox{ 
     protected: 
      HWND handle; 
     private: 
      int ID; 
     public: 
      TextBox(char* text,int width,int height,int x,int y,int id,Form* p); 
      TextBox(int width,int height,int x,int y,int id,Form* p); 
      ~TextBox(){ } 
      bool IsChanged(UINT,WPARAM); 
      bool Text(char *); 
      int GetId(){ return ID; } 
     }; 

     class messageBox{ 
     public: 
      messageBox(){ }; 
      ~messageBox(){ }; 
      bool Show(LPCWSTR,LPCWSTR,UINT); 
      bool ShowA(char*,char*,UINT); 
      bool ShowA(std::string,std::string,UINT); 
     }; 
    }; 
}; 

MWC.cpp

#include "MWC.h" 

MWC::MWC(){ } 
MWC::~MWC(){ } 
int MWC::mLoop(){ 
    while(GetMessage(&msg,NULL,0,0)) { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return 1; 
} 
int MWC::mClose(UINT _msg){ 
    if(_msg == WM_CLOSE){PostQuitMessage(0);return 1;} 
    return 0; 
} 
MWC::System::Form::Form(char* Title,int width,int height,int x,int y){ 
    handle = CreateWindowEx(0,WC_DIALOG,Title,WS_OVERLAPPEDWINDOW | WS_VISIBLE,x,y,width,height,NULL,NULL,NULL,NULL); 
} 
MWC::System::Form::Form(char* Title,int width,int height){ 
    handle = CreateWindowEx(0,WC_DIALOG,Title,WS_OVERLAPPEDWINDOW | WS_VISIBLE,400,100,width,height,NULL,NULL,NULL,NULL); 
} 

bool MWC::System::Form::Show(){ 
    return false; 
} 

HWND MWC::System::Form::ReturnHandle(){ 
    return handle; 
} 

MWC::System::TextBox::TextBox(char* text,int width,int height,int x,int y,int id,Form* p){ 
    ID = id; 
    handle = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",text,WS_VISIBLE | WS_CHILD,x,y,width,height,p->handle,(HMENU)id,NULL,NULL); 
} 
MWC::System::TextBox::TextBox(int width,int height,int x,int y,int id,Form* p){ 
    ID = id; 
    handle = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_VISIBLE | WS_CHILD,x,y,width,height,p->handle,(HMENU)id,NULL,NULL); 
    SendMessageA(handle,WM_SETTEXT,0,(LPARAM)"It works here!"); 
} 
bool MWC::System::TextBox::IsChanged(UINT message, WPARAM wParam){ 
    if(message==WM_COMMAND && HIWORD(wParam)==EN_CHANGE && LOWORD(wParam)==ID){ 
     return true; 
    } 
    return false; 
} 

bool MWC::System::TextBox::Text(char* text){ 
    if(handle == NULL){ 
     return 0; 
    } 
    else{ 
     //SendMessageA(handle,WM_SETTEXT,0,(LPARAM)text); // It doesn't work here! 
     return 1; 
    } 
    return 1; 
} 


bool MWC::System::messageBox::Show(LPCWSTR text,LPCWSTR caption,UINT id){ 
    ::MessageBoxW(NULL,text,caption,id); 
    return true; 
} 

bool MWC::System::messageBox::ShowA(char* text,char* caption,UINT id){ 
    ::MessageBoxA(NULL,(LPCSTR)text,(LPCSTR)caption,id); 
    return true; 
} 

bool MWC::System::messageBox::ShowA(std::string text,std::string caption,UINT id){ 
    ::MessageBoxA(NULL,(LPCSTR)text.c_str(),(LPCSTR)caption.c_str(),id); 
    return true; 
} 

Main.cpp的

#include "MWC.h" 

MWC mwc = MWC(); 
MWC::System::messageBox mbox = MWC::System::messageBox(); 
MWC::System::Form form1 = MWC::System::Form("Window",600,600); 
MWC::System::TextBox textbox1 = MWC::System::TextBox(600,500,0,0,156,&form1); 

BOOL WINAPI myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    mwc.mClose(message);  

    //check if text in textbox has been changed by user 
    textbox1.Text("Test"); 
    if(textbox1.IsChanged(message,wParam) == true){ 

    } 
    return false; 
} 





int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
{ 
    SetWindowLong(form1.ReturnHandle(), DWL_DLGPROC, (long)myProc); // to be added in the class 
    mwc.mLoop(); 
    return 0; 
} 

的事情是.. SendMessage函數工作正常時(MWC.cpp):

MWC::System::TextBox::TextBox(int width,int height,int x,int y,int id,Form* p){ 
    ID = id; 
    handle = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_VISIBLE | WS_CHILD,x,y,width,height,p->handle,(HMENU)id,NULL,NULL); 
    SendMessageA(handle,WM_SETTEXT,0,(LPARAM)"It works here!"); // TESTING 
} 

但如果用在這裏結果發送到崩潰:

bool MWC::System::TextBox::Text(char* text){ 
    if(handle == NULL){ 
     return 0; 
    } 
    else{ 
     //SendMessageA(handle,WM_SETTEXT,0,(LPARAM)text); // It doesn't work here! 
     return 1; 
    } 
    return 0; 
} 

順便說一句,我只是一個業餘誰纏繞與winapi :)。隨時評論你碰巧找到的任何錯誤!

+0

是你的'文本'null終止? – Indy9000

+0

您忽略了重要的部分:您如何調用'Text'方法? –

+0

我不認爲終結者很重要,我試着添加它,但它不斷崩潰。 – Christian

回答

3

你的對話過程看起來都是錯誤的。每當對話過程運行時,您都試圖發送WM_SETTEXT!這可能會設置一個無限遞歸和其他各種其他問題。

您的SendMessage調用是絕對正常的,每次對話過程運行時都無法使其調用。

我不明白IsChanged()功能是什麼。你的框架的設計很奇怪。我建議你先看一些其他框架(例如ATL),以獲得關於如何在Win32上編寫框架的一些想法。或者,更好的是,只使用現有的框架。

+0

哦,上帝,我不小心添加了BOOL WINAPI myProc而不是WinMain的函數調用,非常感謝大衛! – Christian