0

i m currently working on visual c++ 2008 express edition.. in my project i have a picturebox which contains an image, now i have to draw a rectangle to enaable the user to select a part of the image.. I have used the "MouseDown" event of the picturebox and the below code to draw a rectangle:

Void pictureBox1_MouseDown(System::Object^ sender, Windows::Forms::MouseEventArgs^  e)   
                 {  
             Graphics^ g = pictureBox1->CreateGraphics();  
             Pen^ pen = gcnew Pen(Color::Blue);  
             g->DrawRectangle( pen , e->X ,e->Y,width,ht);           
         }

now in the "DrawRectangle" the arguments "width" and "ht" are static, so the above code results in the drawing of a rectangle at the point where the user presses the mouse button on the image in picturebox... i want to allow the user to be able to drag the cursor and draw a rectangle of size that he wishes to.. Plz help me on this.. Thanx..

4

1 に答える 1

0

イベント ハンドラーでウィンドウに直接描画しないでください。すべての描画は Paint イベント ハンドラーで行う必要があります。

うまく機能させるためにできることは他にもたくさんありますが、必要なテクニックの核心は次のとおりです。

ユーザーがマウスをドラッグするときに四角形を移動するには、Mouse Moved イベントを処理する必要があります。取得するたびに、Invalidate() を実行してウィンドウを再描画する必要があります。Paint ハンドラーで、マウス ボタンが押されている場合は、マウス ポインターの位置を取得し、その位置に四角形を描画します。

これで開始できますが、いくつかの問題があります。ウィンドウが絶えず再描画されるため、ウィンドウがちらつき、少し遅くなる可能性があります。

これを改善するためにできることは他にもあります。

  • 必要なフォームの部分のみを無効にします (古い四角形を消去する必要がある場所と、新しい四角形を描画する必要がある場所)。

  • Invalidate() の後、すぐに Update() を呼び出して、できるだけ早く再描画が行われるようにします。

  • 「ダブル バッファリング」レンダリング、および/またはウィンドウ コンテンツをオフスクリーン ビットマップに保存することで、四角形を上にして背景をより迅速にちらつきなく再レンダリングできます。(しかし、これはかなり高度なテクニックです)。

于 2010-03-19T07:18:21.760 に答える